Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andizzle/rwdb
Database wrapper that manage read write connections
https://github.com/andizzle/rwdb
Last synced: 7 days ago
JSON representation
Database wrapper that manage read write connections
- Host: GitHub
- URL: https://github.com/andizzle/rwdb
- Owner: andizzle
- License: mit
- Created: 2017-10-04T03:55:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-08T09:10:17.000Z (about 7 years ago)
- Last Synced: 2024-07-31T20:49:53.878Z (5 months ago)
- Language: Go
- Size: 33.2 KB
- Stars: 19
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go - rwdb - Database wrapper that manage read write connections - ★ 9 (Database)
- awesome-go-extra - rwdb - 10-04T03:55:29Z|2017-11-08T09:10:17Z| (Generators / Database Tools)
README
# rwdb
[![Build Status](https://travis-ci.org/andizzle/rwdb.svg?branch=master)](https://travis-ci.org/andizzle/rwdb)
[![GoDoc](https://godoc.org/github.com/andizzle/rwdb?status.svg)](https://godoc.org/github.com/andizzle/rwdb)
[![Coverage Status](https://coveralls.io/repos/github/andizzle/rwdb/badge.svg?branch=master)](https://coveralls.io/github/andizzle/rwdb?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/andizzle/rwdb)](https://goreportcard.com/report/github.com/andizzle/rwdb)Database wrapper that manage read write connections
## Install
```
go get github.com/andizzle/rwdb
```## Create connections
```go
package mainimport "github.com/andizzle/rwdb"
var conns = []string{
"tcp://user:pass@write/dbname",
"tcp://user:pass@read1/dbname",
"tcp://user:pass@read2/dbname",
"tcp://user:pass@read3/dbname",
}// unable to open write will cause an error
db, err := rwdb.Open("driver", conns...)
```## Rotation read and Sticky read
Query a database rotate the use of database connections
### Basic usage
```go
db, err := rwdb.Open("driver", conns...)// Use the first connection
db.QueryContext(ctx)// Use the next connection
db.QueryContext(ctx)
```Execute a statement will cause all subsequent queries to use the write connection (sticky connection). This is to allow the
immediate reading of records that have been written to the database during the current request cycle.```go
db, err := rwdb.Open("driver", conns...)// Use the next connection
db.QueryContext(ctx)// Use the write conenction
db.ExecContext(ctx)// Use the write connection
db.Query()
```Sticky can be turned off
```go
db.SetSticky(false)
```### A more practical example
The db is marked as modified if there's a successful `Write` to the databse, which turns on the sticky logic.
However, the real world usecase would require `modified` value to be reset on each request session.Here's what we can do:
```go
db, err := rwdb.Open("driver", conns...)func RecordUserLogin() {
d := db.New() // This will make sure the following read are not affected by
// other sessions' write actiond.Query("SELECT * from `users` where id = ?")
...
d.Exec("UPDATE `users` set last_login_at = now();")
d.Query(...) // Connection is set to the writer
}
```## License
[License](https://github.com/andizzle/rwdb/blob/master/LICENSE)