{"id":13776202,"url":"https://github.com/astaxie/beedb","last_synced_at":"2025-04-13T06:28:37.943Z","repository":{"id":3236441,"uuid":"4272815","full_name":"astaxie/beedb","owner":"astaxie","description":"beedb is a go ORM,support database/sql interface，pq/mysql/sqlite","archived":false,"fork":false,"pushed_at":"2014-12-21T13:02:23.000Z","size":1033,"stargazers_count":709,"open_issues_count":25,"forks_count":172,"subscribers_count":64,"default_branch":"master","last_synced_at":"2024-02-15T07:34:17.051Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/astaxie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-05-09T14:58:22.000Z","updated_at":"2023-11-23T18:49:51.000Z","dependencies_parsed_at":"2022-09-17T04:50:19.884Z","dependency_job_id":null,"html_url":"https://github.com/astaxie/beedb","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/astaxie%2Fbeedb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astaxie%2Fbeedb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astaxie%2Fbeedb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astaxie%2Fbeedb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astaxie","download_url":"https://codeload.github.com/astaxie/beedb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674130,"owners_count":21143651,"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-08-03T18:00:19.082Z","updated_at":"2025-04-13T06:28:37.921Z","avatar_url":"https://github.com/astaxie.png","language":"Go","funding_links":[],"categories":["ORM"],"sub_categories":["Middlewares","Advanced Console UIs"],"readme":"Beedb\n=====\n\n:exclamation: **IMPORTANT:** Beedb is being deprecated in favor of [Beego.orm](https://github.com/astaxie/beego/tree/master/orm) :exclamation:\n\nBeedb is an ORM for Go. It lets you map Go structs to tables in a database. It's intended to be very lightweight, doing very little beyond what you really want. For example, when fetching data, instead of re-inventing a query syntax, we just delegate your query to the underlying database, so you can write the \"where\" clause of your SQL statements directly. This allows you to have more flexibility while giving you a convenience layer. But beedb also has some smart defaults, for those times when complex queries aren't necessary.\n\nRight now, it interfaces with Mysql/SQLite/PostgreSQL/DB2/MS ADODB/ODBC/Oracle. The goal however is to add support for other databases in the future, including maybe MongoDb or NoSQL?\n\nRelationship-support is not implemented, for this we will recommend you to use [Beego.orm](https://github.com/astaxie/beego/tree/master/orm).\n\nAll in all, it's not entirely ready for advanced use yet, but it's getting there.\n\nDrivers for Go's sql package which support database/sql includes:\n\nMysql:[github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)`[*]`\n\nMysql:[github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL)`[*]`\n\nPostgreSQL:[github.com/bmizerany/pq](https://github.com/bmizerany/pq)`[*]`\n\nSQLite:[github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)`[*]`\n\nDB2: [bitbucket.org/phiggins/go-db2-cli](https://bitbucket.org/phiggins/go-db2-cli)\n\nMS ADODB: [github.com/mattn/go-adodb](https://github.com/mattn/go-adodb)`[*]`\n\nODBC: [bitbucket.org/miquella/mgodbc](https://bitbucket.org/miquella/mgodbc)`[*]`\n\nOracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8)\n\nDrivers marked with a `[*]` are tested with Beedb\n\n### API Interface\n[wiki/API-Interface](https://github.com/astaxie/beedb/wiki/API-Interface)\n\n### Installing Beedb\n    go get github.com/astaxie/beedb\n\n### How do we use it?\n\nOpen a database link(may be will support ConnectionPool in the future)\n\n```go\ndb, err := sql.Open(\"mymysql\", \"test/xiemengjun/123456\")\nif err != nil {\n\tpanic(err)\n}\norm := beedb.New(db)\n```\n\nwith PostgreSQL,\n\n```go\norm := beedb.New(db, \"pg\")\n```\n\nOpen Debug log, turn on the debug\n\n```go\nbeedb.OnDebug=true\n```\n\nModel a struct after a table in the db\n\n```go\ntype Userinfo struct {\n\tUid\t\tint\t`beedb:\"PK\" sql:\"UID\" tname:\"USER_INFO\"` //if the table's PrimaryKey is not \"Id\", use this tag\n\tUsername\tstring `sql:\"USERNAME\"`\n\tDepartname\tstring `sql:\"DEPARTNAME\"`\n\tCreated\t\ttime.Time `sql:\"CREATED\"`\n}\n```\n\n###***Caution***\nThe structs Name 'UserInfo' will turn into the table name 'USER_INFO', as defined by the **tname** tag.\nIf the key 'UserName' will turn into the select colum 'USERNAME' because of the **sql** tag.\n\n\nCreate an object and save it\n\n```go\nvar saveone Userinfo\nsaveone.Username = \"Test Add User\"\nsaveone.Departname = \"Test Add Departname\"\nsaveone.Created = time.Now()\norm.Save(\u0026saveone)\n```\n\nSaving new and existing objects\n\n```go\nsaveone.Username = \"Update Username\"\nsaveone.Departname = \"Update Departname\"\nsaveone.Created = time.Now()\norm.Save(\u0026saveone)  //now saveone has the primarykey value it will update\n```\n\nFetch a single object\n\n```go\nvar user Userinfo\norm.Where(\"uid=?\", 27).Find(\u0026user)\n\nvar user2 Userinfo\norm.Where(3).Find(\u0026user2) // this is shorthand for the version above\n\nvar user3 Userinfo\norm.Where(\"name = ?\", \"john\").Find(\u0026user3) // more complex query\n\nvar user4 Userinfo\norm.Where(\"name = ? and age \u003c ?\", \"john\", 88).Find(\u0026user4) // even more complex\n```\n\nFetch multiple objects\n\n```go\nvar allusers []Userinfo\nerr := orm.Where(\"id \u003e ?\", \"3\").Limit(10,20).FindAll(\u0026allusers) //Get id\u003e3 limit 10 offset 20\n\nvar tenusers []Userinfo\nerr := orm.Where(\"id \u003e ?\", \"3\").Limit(10).FindAll(\u0026tenusers) //Get id\u003e3 limit 10  if omit offset the default is 0\n\nvar everyone []Userinfo\nerr := orm.FindAll(\u0026everyone)\n```\n\nFind result as Map\n\n```go\n//Original SQL Backinfo resultsSlice []map[string][]byte\n//default PrimaryKey id\na, _ := orm.SetTable(\"userinfo\").SetPK(\"uid\").Where(2).Select(\"uid,username\").FindMap()\n```\n\nUpdate with Map\n\n```go\nt := make(map[string]interface{})\nvar j interface{}\nj = \"astaxie\"\nt[\"username\"] = j\n//update one\norm.SetTable(\"userinfo\").SetPK(\"uid\").Where(2).Update(t)\n```\n\nUpdate batch with Map\n```go\norm.SetTable(\"userinfo\").Where(\"uid\u003e?\", 3).Update(t)\n```\n\nInsert data with Map\n\n```go\nadd := make(map[string]interface{})\nj = \"astaxie\"\nadd[\"username\"] = j\nj = \"cloud develop\"\nadd[\"departname\"] = j\nj = \"2012-12-02\"\nadd[\"created\"] = j\norm.SetTable(\"userinfo\").Insert(add)\n```\n\nInsert batch with map\n\n```go\naddslice := make([]map[string]interface{})\nadd:=make(map[string]interface{})\nadd2:=make(map[string]interface{})\nj = \"astaxie\"\nadd[\"username\"] = j\nj = \"cloud develop\"\nadd[\"departname\"] = j\nj = \"2012-12-02\"\nadd[\"created\"] = j\nj = \"astaxie2\"\nadd2[\"username\"] = j\nj = \"cloud develop2\"\nadd2[\"departname\"] = j\nj = \"2012-12-02\"\nadd2[\"created\"] = j\naddslice =append(addslice, add, add2)\norm.SetTable(\"userinfo\").Insert(addslice)\n```\n\nJoin Table\n\n```go\na, _ := orm.SetTable(\"userinfo\").Join(\"LEFT\", \"userdeatail\", \"userinfo.uid=userdeatail.uid\").Where(\"userinfo.uid=?\", 1).Select(\"userinfo.uid,userinfo.username,userdeatail.profile\").FindMap()\n```\n\nGroup By And Having\n\n```go\na, _ := orm.SetTable(\"userinfo\").GroupBy(\"username\").Having(\"username='astaxie'\").FindMap()\n```\n\nNesting Models (inline)\n\n```go\ntype SQLModel struct {\n\tId       int       `beedb:\"PK\" sql:\"id\"`\n\tCreated  time.Time `sql:\"created\"`\n\tModified time.Time `sql:\"modified\"`\n}\ntype User struct {\n\tSQLModel `sql:\",inline\"`\n\tName     string `sql:\"name\" tname:\"fn_group\"`\n\tAuth     int    `sql:\"auth\"`\n}\n// the SQL table has the columns: id, name, auth, created, modified\n// They are marshalled and unmarshalled automatically because of the inline keyword\n```\n\n## LICENSE\n\n BSD License\n [http://creativecommons.org/licenses/BSD/](http://creativecommons.org/licenses/BSD/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastaxie%2Fbeedb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastaxie%2Fbeedb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastaxie%2Fbeedb/lists"}