{"id":13412121,"url":"https://github.com/andizzle/rwdb","last_synced_at":"2025-08-14T04:32:51.184Z","repository":{"id":53581803,"uuid":"105729699","full_name":"andizzle/rwdb","owner":"andizzle","description":"Database wrapper that manage read write connections","archived":false,"fork":false,"pushed_at":"2017-11-08T09:10:17.000Z","size":34,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:49:53.878Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andizzle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-04T03:55:29.000Z","updated_at":"2024-07-19T18:32:42.000Z","dependencies_parsed_at":"2022-09-19T18:01:34.522Z","dependency_job_id":null,"html_url":"https://github.com/andizzle/rwdb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andizzle%2Frwdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andizzle%2Frwdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andizzle%2Frwdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andizzle%2Frwdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andizzle","download_url":"https://codeload.github.com/andizzle/rwdb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229800386,"owners_count":18126028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:21.202Z","updated_at":"2024-12-15T09:15:33.508Z","avatar_url":"https://github.com/andizzle.png","language":"Go","readme":"# rwdb\n\n[![Build Status](https://travis-ci.org/andizzle/rwdb.svg?branch=master)](https://travis-ci.org/andizzle/rwdb)\n[![GoDoc](https://godoc.org/github.com/andizzle/rwdb?status.svg)](https://godoc.org/github.com/andizzle/rwdb)\n[![Coverage Status](https://coveralls.io/repos/github/andizzle/rwdb/badge.svg?branch=master)](https://coveralls.io/github/andizzle/rwdb?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/andizzle/rwdb)](https://goreportcard.com/report/github.com/andizzle/rwdb)\n\nDatabase wrapper that manage read write connections\n\n## Install\n\n```\ngo get github.com/andizzle/rwdb\n```\n\n## Create connections\n\n```go\npackage main\n\nimport \"github.com/andizzle/rwdb\"\n\nvar conns = []string{\n        \"tcp://user:pass@write/dbname\",\n        \"tcp://user:pass@read1/dbname\",\n        \"tcp://user:pass@read2/dbname\",\n        \"tcp://user:pass@read3/dbname\",\n}\n\n\n// unable to open write will cause an error\ndb, err := rwdb.Open(\"driver\", conns...)\n```\n\n## Rotation read and Sticky read\n\nQuery a database rotate the use of database connections\n\n### Basic usage\n\n```go\ndb, err := rwdb.Open(\"driver\", conns...)\n\n// Use the first connection\ndb.QueryContext(ctx)\n\n// Use the next connection\ndb.QueryContext(ctx)\n```\n\nExecute a statement will cause all subsequent queries to use the write connection (sticky connection). This is to allow the \nimmediate reading of records that have been written to the database during the current request cycle. \n\n\n```go\ndb, err := rwdb.Open(\"driver\", conns...)\n\n// Use the next connection\ndb.QueryContext(ctx)\n\n// Use the write conenction\ndb.ExecContext(ctx)\n\n// Use the write connection\ndb.Query()\n```\n\nSticky can be turned off\n```go\ndb.SetSticky(false)\n```\n\n### A more practical example\n\nThe db is marked as modified if there's a successful `Write` to the databse, which turns on the sticky logic. \nHowever, the real world usecase would require `modified` value to be reset on each request session.\n\nHere's what we can do:\n\n```go\ndb, err := rwdb.Open(\"driver\", conns...)\n\nfunc RecordUserLogin() {\n        d := db.New()       // This will make sure the following read are not affected by \n                            // other sessions' write action\n\n        d.Query(\"SELECT * from `users` where id = ?\")\n        ...\n        d.Exec(\"UPDATE `users` set last_login_at = now();\")\n        d.Query(...)        // Connection is set to the writer\n}\n```\n\n## License\n\n[License](https://github.com/andizzle/rwdb/blob/master/LICENSE)\n","funding_links":[],"categories":["Database","数据库","Generators","Uncategorized","\u003cspan id=\"数据库-database\"\u003e数据库 Database\u003c/span\u003e","数据库  `go语言实现的数据库`","Data Integration Frameworks"],"sub_categories":["Database Tools","Advanced Console UIs","数据库工具","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandizzle%2Frwdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandizzle%2Frwdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandizzle%2Frwdb/lists"}