https://github.com/andizzle/rwdb
Database wrapper that manage read write connections
https://github.com/andizzle/rwdb
Last synced: 4 months 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 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-08T09:10:17.000Z (about 8 years ago)
- Last Synced: 2024-07-31T20:49:53.878Z (over 1 year 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-cn - rwdb
- 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)
- awesome-go-plus - rwdb - rwdb provides read replica capability for multiple database servers setup.  (Database / Database Tools)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- fucking-awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go - rwdb - | - | - | (Database / Advanced Console UIs)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. - :arrow_down:0 - :star:8 (Database / Advanced Console UIs)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go-cn - rwdb
- awesome-go-cn - rwdb
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go - rwdb - rwdb为多个数据库服务器设置提供了只读副本功能。 (<span id="数据库-database">数据库 Database</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go-cn - rwdb
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Advanced Console UIs)
- awesome-go-with-stars - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Database Tools)
- awesome-Char - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Advanced Console UIs)
- awesome-go - rwdb - rwdb provides read replica capability for multiple database servers setup. (Database / Advanced Console UIs)
README
# rwdb
[](https://travis-ci.org/andizzle/rwdb)
[](https://godoc.org/github.com/andizzle/rwdb)
[](https://coveralls.io/github/andizzle/rwdb?branch=master)
[](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 main
import "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 action
d.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)