{"id":18418611,"url":"https://github.com/bsm/dbx","last_synced_at":"2025-04-13T05:54:55.357Z","repository":{"id":49183286,"uuid":"109961550","full_name":"bsm/dbx","owner":"bsm","description":"Useful extensions to stdlib's database/sql","archived":false,"fork":false,"pushed_at":"2021-06-24T16:14:03.000Z","size":18,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T05:54:52.751Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bsm.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-11-08T10:30:13.000Z","updated_at":"2021-06-24T16:13:32.000Z","dependencies_parsed_at":"2022-08-29T20:31:43.933Z","dependency_job_id":null,"html_url":"https://github.com/bsm/dbx","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fdbx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fdbx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fdbx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fdbx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsm","download_url":"https://codeload.github.com/bsm/dbx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670501,"owners_count":21142901,"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-11-06T04:14:07.669Z","updated_at":"2025-04-13T05:54:55.324Z","avatar_url":"https://github.com/bsm.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DBx\n\n[![Test](https://github.com/bsm/dbx/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/dbx/actions/workflows/test.yml)\n[![GoDoc](https://godoc.org/github.com/bsm/dbx?status.png)](http://godoc.org/github.com/bsm/dbx)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\nUseful extensions to stdlib's [database/sql](https://golang.org/pkg/database/sql).\n\n## Iterators\n\nA simple wrapper for [sql.Rows](https://golang.org/pkg/database/sql/#Rows) to iterate\nover structs:\n\n```go\nimport (\n  \"fmt\"\n\n  \"github.com/bsm/dbx\"\n)\n\nfunc main() {\n\t// Init a temp dir\n\tdir, err := os.MkdirTemp(\"\", \"dbx-example\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Create tables, seed some test data\n\tdb, err := setupTestDB(dir)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close()\n\n\t// Query rows\n\trows, err := db.Query(`SELECT id, title FROM posts ORDER BY id`)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Wrap rows in an iterator AND defer Close()\n\titer := dbx.NewIterator(rows, func(rs dbx.RowScanner) (interface{}, error) {\n\t\tpost := new(Post)\n\t\tif err := rs.Scan(\u0026post.ID, \u0026post.Title); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn post, nil\n\t})\n\tdefer iter.Close()\n\n\t// Iterate over records, print a few\n\tn := 0\n\tfor iter.Next() {\n\t\tpost := iter.Record().(*Post)\n\t\tif n++; n%321 == 0 {\n\t\t\tfmt.Printf(\"%+v\\n\", post)\n\t\t}\n\t}\n\n\t// Check for iterator errors\n\tif err := iter.Err(); err != nil {\n\t\tpanic(err)\n\t}\n\n}\n```\n\nLike above, just batching and with the ability to resolve (1:n) associations:\n\n```go\nimport (\n  \"fmt\"\n  \"strings\"\n\n  \"github.com/bsm/dbx\"\n)\n\nfunc main() {\n\t// Init a temp dir\n\tdir, err := os.MkdirTemp(\"\", \"dbx-example\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Create tables, seed some test data\n\tdb, err := setupTestDB(dir)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close()\n\n\t// Scan each Post row into a struct (callback)\n\tscanPost := func(rs dbx.RowScanner) (interface{}, error) {\n\t\tpost := new(Post)\n\t\tif err := rs.Scan(\u0026post.ID, \u0026post.Title); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn post, nil\n\t}\n\n\t// Scan each Comment row into a struct (callback)\n\tscanComment := func(rs dbx.RowScanner) (interface{}, error) {\n\t\tcomment := new(Comment)\n\t\tif err := rs.Scan(\u0026comment.ID, \u0026comment.PostID, \u0026comment.Message); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn comment, nil\n\t}\n\n\t// For every batch of Posts, fetch all the associated comments (callback)\n\ttransformBatch := func(recs []interface{}) error {\n\t\tpostMap := make(map[int64]*Post, len(recs))\n\t\tpostIDs := make([]interface{}, 0, len(recs))\n\n\t\tfor _, rec := range recs {\n\t\t\tpost := rec.(*Post)\n\t\t\tpostMap[post.ID] = post\n\t\t\tpostIDs = append(postIDs, post.ID)\n\t\t}\n\n\t\trows, err := db.Query(`SELECT id, post_id, message FROM comments WHERE post_id IN (?`+strings.Repeat(\",?\", len(postIDs)-1)+`)`, postIDs...)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdefer rows.Close()\n\n\t\tfor rows.Next() {\n\t\t\tv, err := scanComment(rows)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tcomment := v.(*Comment)\n\n\t\t\tpost := postMap[comment.PostID]\n\t\t\tpost.Comments = append(post.Comments, *comment)\n\t\t}\n\t\treturn rows.Err()\n\t}\n\n\t// Query rows\n\trows, err := db.Query(`SELECT id, title FROM posts ORDER BY id`)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Wrap rows in an iterator AND defer Close()\n\titer := dbx.NewBatchIterator(rows, 100, scanPost, transformBatch)\n\tdefer iter.Close()\n\n\t// Iterate over records, print a few\n\tn := 0\n\tfor iter.Next() {\n\t\tpost := iter.Record().(*Post)\n\t\tif n++; n%321 == 0 {\n\t\t\tfmt.Printf(\"%+v\\n\", post)\n\t\t}\n\t}\n\n\t// Check for iterator errors\n\tif err := iter.Err(); err != nil {\n\t\tpanic(err)\n\t}\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fdbx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsm%2Fdbx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fdbx/lists"}