{"id":37127174,"url":"https://github.com/iamgoroot/dbie","last_synced_at":"2026-01-14T14:48:39.063Z","repository":{"id":41541254,"uuid":"497734979","full_name":"iamgoroot/dbie","owner":"iamgoroot","description":"Golang DB layer for lazy","archived":false,"fork":false,"pushed_at":"2023-12-18T22:54:26.000Z","size":239,"stargazers_count":6,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-14T15:57:48.530Z","etag":null,"topics":["bun","contract-driven-development","database","database-layer","generator","go","go-pg","gogenerate","golang","golang-tools","gorm","mongo"],"latest_commit_sha":null,"homepage":"","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/iamgoroot.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-29T23:47:11.000Z","updated_at":"2023-09-30T11:41:30.000Z","dependencies_parsed_at":"2024-06-21T17:40:06.325Z","dependency_job_id":null,"html_url":"https://github.com/iamgoroot/dbie","commit_stats":{"total_commits":127,"total_committers":3,"mean_commits":"42.333333333333336","dds":"0.055118110236220486","last_synced_commit":"1dcaae5713246e5fc7cc9bc8d299a3013642e598"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iamgoroot/dbie","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamgoroot%2Fdbie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamgoroot%2Fdbie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamgoroot%2Fdbie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamgoroot%2Fdbie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamgoroot","download_url":"https://codeload.github.com/iamgoroot/dbie/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamgoroot%2Fdbie/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28423994,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["bun","contract-driven-development","database","database-layer","generator","go","go-pg","gogenerate","golang","golang-tools","gorm","mongo"],"created_at":"2026-01-14T14:48:38.226Z","updated_at":"2026-01-14T14:48:39.044Z","avatar_url":"https://github.com/iamgoroot.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dbie\n\n[![codecov](https://codecov.io/gh/iamgoroot/dbie/branch/main/graph/badge.svg?token=HDGXEOT8BA)](https://codecov.io/gh/iamgoroot/dbie)\n\ndbie - (DB Interface Extension) Golang database layer for lazy gophers\n\n#### Long story short:\n\n* You [provide an interface](#define-repository-interface) of database layer using **models** from your orm\n  library ([Go-pg](https://github.com/go-pg/pg), [Gorm](https://gorm.io/), [Bun](https://bun.uptrace.dev/)\n  , [mongo](https://github.com/mongodb/mongo-go-driver) etc...)\n* dbie **generates an implementation of that interface** for matching methods\n* You can implement [custom method](#custom-methods) yourself if you need\n\n1. [Why it might be good?](#why-it-might-be-good)\n2. [Why not sqlc?](#why-not-sqlc?)\n3. [What's missing](#what's-missing?)\n4. [Getting started](#getting-started)\n    1. [Install](#install-generator-tool)\n    2. [Define contracts](#define-model)\n    3. [Usage](#Usage)\n5. [SelectBy*|FindBy*](#SelectBy*|FindBy*)\n6. [Sort order](#sort-order)\n7. [Custom methods](#custom-methods)\n\n## Why it might be good?\n\n* You do mostly brain-dead simple db queries ([Go-pg](https://github.com/go-pg/pg), [Gorm](https://gorm.io/)\n  , [Bun](https://bun.uptrace.dev/), [mongo](https://github.com/mongodb/mongo-go-driver) etc...)\n* It's a nice **addition to orm** you might already use - use pagination, sorting, filtering with\n* No query pieces all over your code - **go code first**\n* dbietool generates '**just enough**' code **to satisfy the interface** - for less clutter\n* Generate and forget - **maintain your models** and dbie will translate the rest\n\n## Why not sqlc?\n\nbdie is different in a few ways so might not be for you depending on your use case or preferences\n\n* **Go code first** approach\n* **MongoDB** support\n* dbie is **not a code generator**, it's a library - you don't necessarily have to generate any code (it's just your\n  convenience)\n\n## What's missing?\n\n* No **Transactions** - but you can them implement as [custom method](#custom-methods) with your orm library\n* No **Joins** - but [custom method](#custom-methods) again\n\n#### But... I want to define interfaces at layer where I use them!\n\n...And I encourage you to do so!\n\n* Use interface as blueprint for your dbie implementation in database layer\n* Define small interfaces in your service layer exactly where you need them (it's golang after all)\n\n## Getting started\n\n### Install generator tool\n\n```sh\n   go get -u github.com/iamgoroot/dbietool\n   go install github.com/iamgoroot/dbietool\n```\n\n### Define repository interface\n\nDefine methods you want implemented by using [naming convention](#Naming convention) and use\nwrappers for pagination (`dbie.Page` and `dbie.Paginated`)\n\n```golang \n//go:generate dbietool -core=Bun,Gorm,Pg -constr=factory\n\ntype User interface {\n\tdbie.Repo[model.User]\n\tInit() error\n\tSelectByName(string) ([]model.User, error)\n\tSelectByID(int) (model.User, error)\n\tFindByID(int) (model.User, error)\n\tSelectByGroupEq(string) ([]model.User, error)\n\tSelectByGroup(dbie.Page, string) (items dbie.Paginated[model.User], err error)\n\tSelectByGroupIn(dbie.Page, ...string) (items dbie.Paginated[model.User], err error)\n\tSelectByGroupNinOrderByGroupAsc(dbie.Page, ...string) (items dbie.Paginated[model.User], err error)\n\tSelectByGroupOrderByNameDescOrderByIDAsc(string) (model.User, error)\n}\n```\n\n### Define model\n\nAs usually in Bun, Gorm, go-pg or Mongo (tag `bson`):\n\n```golang\ntype User struct {\n\tID       int\n\tName     string\n\tGroup    string\n}\n```\n\n### Generate\n\nThat's it. generate code\n\n   ```sh\n   go generate ./...\n   ```\n\n### Usage\n\n```golang\nfunc main() {\n\t// instantiate (run dbietool with `-constr=func` parameter)\n\tuserRepo := repo.NewUser(context.Background())\n\t\n\t// insert user and handle error\n\terr := userRepo.Insert(model.User{Name: \"userName1\"})\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\t\n\t// select user using generated method and handle error\n\tuser, err := userRepo.SelectByName(\"userName1\")\n\tif err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\tlog.Println(user, err)\n}\n```\nRun dbietool with flag `-constr=factory` to generate factory objects instead of factory functions\n\n```golang\n   factory := repo.Bun[model.User]{DB: db}\n   userRepo := factory.NewUser(context.Background())\n```\n\n\n## SelectBy*|FindBy*\n\nCan be used to select items by some criteria.\n\n### Criteria\nFor now only one criteria is supported per method. \n\n* {ColumnName} - part of function name, specifically db column name but CamelCase instead of snake_case\n* {?Operator} - SQL operator. \n  * `dbie.Eq` if omitted. \n  * Possible values:\n  `\"Eq\" (default), \"Neq\", \"Gt\", \"Gte\", \"Lt\", \"Lte\", \"Like\", \"Ilike\", \"Nlike\", \"Nilike\", \"In\", \"Nin\", \"Is\", \"Not\"`\n* {columnName} - columnName in camelCase.\n* {columnType} - type of parameter as golang type\n* Supported return types: \n  * MODEL - returns one item \n  * []MODEL - returns slice of resulting items\n  * dbie.Paginated[MODEL] - returns paginated wrapper with resulting items\n* Each method returns error as second parameter\n\n```golang\nfunc SelectBy{ColumnName}({columnName} {columnType}) (MODEL, error) // returns one row or error \nfunc FindBy{ColumnName}({columnName} {columnType}) (MODEL, error) // same as above\nfunc SelectBy{ColumnName}{?Operator}( {columnName} {columnType} ) (MODEL, error) // returns one row or error \nfunc SelectBy{ColumnName}{?Operator}( {columnName} {columnType} ) ([]MODEL, error) // returns slice or error\nfunc SelectBy{ColumnName}{?Operator}( {columnName} {columnType} ) (dbie.Paginated[MODEL], error) // returns slice wrapper with pagination or error\n```\n\n### Sort order\n\n* {OrderColumnName} - ColumnName to order by in CamelCase.\n* {?SortOrder} - Asc or Desc\n* columnName and columnType as in previous example\n* composite sorting is supported\n```golang\nfunc SelectByColumnNameOrderBy{OrderColumnName}{?SortOrder}(columnName columnType) ([]MODEL, error)\nfunc SelectByColumnNameOrderBy{OrderColumnName}{?SortOrder}{ColumnName2}{?Order2}(columnName columnType) ([]MODEL, error)\n\n```\n\n# Custom methods\n\n1. Create separate file in same package as repo implementation\n2. Create method with desired signature that does start with SelectBy* or FindBy*\n\n# Docs and Links\n\nMongo:\n\n* https://github.com/mongodb/mongo-go-driver\n\nBun:\n\n* https://bun.uptrace.dev/guide/golang-orm.html\n* https://github.com/uptrace/bun\n\nGorm:\n\n* https://gorm.io/docs/\n* https://github.com/go-gorm/gorm\n\ngo-pg:\n\n* https://github.com/go-pg/pg\n* https://pg.uptrace.dev/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamgoroot%2Fdbie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamgoroot%2Fdbie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamgoroot%2Fdbie/lists"}