{"id":30352341,"url":"https://github.com/wdaglb/korm","last_synced_at":"2025-08-19T00:20:25.924Z","repository":{"id":57626181,"uuid":"401552590","full_name":"wdaglb/korm","owner":"wdaglb","description":"golang orm","archived":false,"fork":false,"pushed_at":"2022-10-23T04:55:45.000Z","size":61,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-23T12:54:05.361Z","etag":null,"topics":["database","go","golang","mssql","mysql","orm","sqlserver"],"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/wdaglb.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":"2021-08-31T02:49:16.000Z","updated_at":"2022-10-31T01:15:20.000Z","dependencies_parsed_at":"2022-08-31T07:23:38.341Z","dependency_job_id":null,"html_url":"https://github.com/wdaglb/korm","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/wdaglb/korm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdaglb%2Fkorm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdaglb%2Fkorm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdaglb%2Fkorm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdaglb%2Fkorm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wdaglb","download_url":"https://codeload.github.com/wdaglb/korm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wdaglb%2Fkorm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271079229,"owners_count":24695597,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["database","go","golang","mssql","mysql","orm","sqlserver"],"created_at":"2025-08-19T00:20:23.033Z","updated_at":"2025-08-19T00:20:25.913Z","avatar_url":"https://github.com/wdaglb.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# korm\ngolang orm, 简单易用的orm, 支持嵌套事务\n\n特点：\n\n1. 事务不需要传tx连接发，只需要在Transaction内部的都会进行事务操作\n\n## 安装\n```\ngo get github.com/wdaglb/korm\ngo get github.com/go-sql-driver/mysql\n```\n\n## 支持数据库\n\n* mysql https://github.com/go-sql-driver/mysql\n* mssql https://github.com/denisenkom/go-mssqldb\n* ...其它未测\n\n## 连接mysql数据库\n\n```\nconn := NewConnect(Config{\n    DefaultConn: \"default\",\n    MaxOpenConns: 100, // 最大打开连接数\n    MaxIdleConns: 10,  // 最大空闲连接数\n    ConnMaxLifetime: 7200, // 保持连接时间\n})\nerr := conn.AddDb(DbConfig{\n    Conn: \"default\",\n    Driver: \"mysql\",\n    Host:   \"127.0.0.1\",\n    Port:   3306,\n    User:   \"root\",\n    Pass:   \"\",\n    Database: \"test\",\n})\nif err != nil {\n    log.Fatalf(\"connect fail: %v\", err)\n}\n```\n\n## 连接上下文\n数据库的读写操作都依托于Context类\nContext内部会自动维护db连接，不需要你自行管理Context实例，每次使用都建议实例一个新的Context\n```\n// 使用默认数据库\nctx := NewContext()\n\n// 使用指定数据库\nctx := UseContext(\"test\")\n```\n\n## 声明模型结构\n```\ntype Test struct {\n\tId int64 `db:\"id\"`\n\tUser string `db:\"user\"`\n}\n```\n\n## 查询一行数据\n```\nrow := \u0026Test{}\nif !ctx.Model(\u0026row).Where(\"Id\", 1).Find().Exist {\n    fmt.Println(\"记录不存在\")\n    return\n}\nfmt.Printf(\"id: %d\\n\", row.Id)\n```\n执行的sql\n```\nSELECT column... FROM test WHERE id=1\n```\n\n## 判断数据是否存在\n```\nif !ctx.Model(Test{}).Where(\"Id\", 1).Exist() {\n    fmt.Println(\"记录不存在\")\n    return\n}\nfmt.Printf(\"数据存在\\n\")\n```\n执行的sql\n```\nSELECT primaryKey FROM test WHERE id=1\n```\n\n## 查询多行数据\n```\nvar rows []Test\nif ctx.Model(\u0026rows).Select().Error != nil {\n    fmt.Println(\"查询错误\")\n    return\n}\nfmt.Printf(\"rows: %v\\n\", rows)\n```\n执行的sql\n```\nSELECT column... FROM test\n```\n\n## 忽略字段查询\n\n```\nmodel.IgnoreField(\"Content\")\n```\n\n## 创建数据\n模型插入会把已经赋值的数据插入的关联表\n```\ninsertData := \u0026Test{\n  User: \"test\",\n}\nif err := ctx.Model(\u0026insertData).Create(); err != nil {\n    fmt.Println(\"创建错误\")\n}\nfmt.Printf(\"insertData: %v\\n\", insertData)\n```\n执行的sql\n```\nINSERT INTO test (`user`) VALUES ('test')\n```\n\n## 更新数据\n模型更新会把关联已经加载的数据一并更新，未关联的不会更新\n```\nupdateData := \u0026Test{\n  Id: 1,\n  User: \"test\",\n}\nif err := ctx.Model(\u0026updateData).Update(); err != nil {\n    fmt.Println(\"更新错误\")\n}\nfmt.Printf(\"updateData: %v\\n\", updateData)\n```\n执行的sql\n```\nUPDATE test SET `user`='test' WHERE `id`=1\n```\n\n## 删除数据\n模型删除会把关联已经加载的数据一并删除，未关联的不会删除\n```\nif err := ctx.Model(\u0026Test{Id: 1}).Delete(); err != nil {\n    fmt.Println(\"删除错误\")\n}\nfmt.Printf(\"删除成功\\n\")\n```\n执行的sql\n```\nDELETE FROM test WHERE `id`=1\n```\n\n## 事务操作\n```\nctx.Transaction(func () error {\n    // 事务逻辑代码\n})\n```\n只需要把需要进行的事务，写到闭包函数里即可，支持嵌套事务\n注意：在同一个Context实例里的才会被事务影响\n\n## 一对一关联\n\n在模型定义声明字段\n使用*指针标识，默认值为nil，需要With(\"模型名\")手动加载关联数据\n\n未使用*指针标识，则默认自动加载关联数据\n\n```\ntype Test struct {\n\tId int64 `db:\"id\"`\n\tCate TestCate `pk:\"Id\" fk:\"TestId\"`\n\tCate2 *TestCate `pk:\"Id\" fk:\"TestId\"`\n}\ntype TestCate struct {\n\tId int64 `db:\"id\"`\n\tName string `db:\"name\"`\n\tTestId int `db:\"test_id\"`\n}\n```\n\n## 一对多关联\n\n在模型定义声明一个分片字段\n\n```\ntype Test struct {\n\tId int64 `db:\"id\"`\n\tCates []TestCate `pk:\"Id\" fk:\"TestId\"`\n}\ntype TestCate struct {\n\tId int64 `db:\"id\"`\n\tName string `db:\"name\"`\n\tTestId int `db:\"test_id\"`\n}\n```\n\n## 取消关联数据操作同步\nCates关联的数据不会被删除\n```\nctx.Model(\u0026deleteData).CancelTogether(\"Cates\").Delete()\n```\n\n## License\n@ King east, 2021-now\n\nReleased under the [MIT License](https://github.com/wdaglb/korm/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwdaglb%2Fkorm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwdaglb%2Fkorm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwdaglb%2Fkorm/lists"}