为什么我们需要一个新的 orm?
实际上有一些不错的 orm 可用,但它们只是不能满足我的需求。那我想要什么?
高性能架构即代码静态输入和生成的 api 代码生成的文件较少简单实用简单的 rawsql 使用连接关系时单个数据库命中过滤器
目前我还不能说企业在我提到的所有事情上都做得很好。但它的目的就是这样做。
那么让我们看看企业是做什么的。
高性能
立即学习“go语言免费学习笔记(深入)”;
您可以在此处查看基准测试源代码。
https://github.com/mrsametburgazoglu/go-orm-benchmarks/tree/enterprise
如您所见,企业的性能非常出色。
架构即代码
像许多其他企业一样,企业从您的代码生成数据库模型。
这是一个小例子
// db_models/account.gopackage db_modelsimport ( "github.com/mrsametburgazoglu/enterprise/models" "github.com/google/uuid")func account() *models.table { idfield := models.uuidfield("id").defaultfunc(uuid.new) tb := &models.table{ fields: []models.fieldi{ idfield, models.stringfield("name"), models.stringfield("surname"), models.uuidfield("testid").setnillable(), }, relations: []*models.relation{ models.manytoone(testname, idfield.dbname, "test_id"), models.manytomany(groupname, "account_id", "group_id", "id", accountgroupname), }, } tb.settablename(accountname) tb.setidfield(idfield) return tb}
登录后复制
// generate/generate.gopackage mainimport ( "example/db_models" "github.com/mrsametburgazoglu/enterprise/generate")func main() { generate.models( db_models.test(), db_models.account(), db_models.group(), )}
登录后复制
当您执行上面的脚本时,它将创建一个名为 models 的包,并为每个名为 model.go 和 model_predicates.go 的表放置两个文件。并且会有一个client.go用于使用db。
静态输入和生成的 api 代码
自动生成模型后,您可以创建并获取模型。
import "/your/project/models" // your auto-generated models packagefunc main() { db, err := models.newdb(dburl) if err != nil { panic(err) } ctx := context.background() account := models.newaccount(ctx, db) account.setname("name") account.setsurname("surname") err = account.create()//row added to table if err != nil { log.fatal(err) }}
登录后复制
import "/your/project/models" // your auto-generated models packagefunc main() { db, err := models.newdb(dburl) if err != nil { panic(err) } ctx := context.background() account := models.newaccount(ctx, db) account.where(account.isidequal(uuid.new())) err = account.get()//row variables set to account struct if err != nil { log.fatal(err) }}
登录后复制
生成的文件较少
就像我之前说过的,企业为每个表生成 2 个文件,以及一个使用所有这些表的客户端文件。它在自己的包中处理大多数情况,这样您将拥有更干净的结构。
简单实用
enterprise 的目标是与数据库字段进行简单且功能性的交互。为此,字段具有辅助函数。
假设表上有一个名为face_id 的可为空的uuid,并且用*uuid 表示它。 enterprise 将生成一个辅助函数来用字符串设置它。这样你就不需要获取该变量的指针。
func (t *account) setfaceidvalue(v uuid.uuid)
登录后复制
如果你有一个 uuid 字段,它将创建一个解析器助手。
func (t *account) parsefaceid(v string) error
登录后复制
对于某些值类型,它将有 in 子句。
func (t *account) faceidin(v ...uuid.uuid) boolfunc (t *account) faceidnotin(v ...uuid.uuid) bool
登录后复制
对于 time.time 它将创建这些辅助函数。
func (t *account) formatcreatedat(v string) stringfunc (t *account) parsecreatedat(layout, value string) error
登录后复制
简单的 rawsql 用法
企业可以创建复杂的查询,但始终需要 rawsql。因此,您可以使用 models.idatabase 与 pgx 交互。如果需要,我们计划将原始 sql 结果扫描到您的数据库模型或您使用关系创建的自定义结构。
连接关系时单个数据库命中过滤器
enterprise 与其他产品的最重要区别之一是可以连接关系并通过单个查询过滤它们。
一个例子是这样的。让我们来看看学生答错的测试题,并且测试分数高于 80。
s := models.NewStudent(ctx, db)s.Where(s.IsIDEqual(studentID))s.WithTestList(func(testList *models.TestList) { testList.Where( testList.IsPointGreaterThan(80), ) testList.Order(models.CreatedAtField) testList.WithQuestionList(func(questionList *models.QuestionList){ questionList.Where( questionList.IsAnswerEqual(False), ) })})err = s.Get()//row variables set to model and its relations struct.if err != nil { log.Fatal(err)}
登录后复制
对于存储库:https://github.com/mrsametburgazoglu/enterprise
如需文档:https://mrsametburgazoglu.github.io/enterprise_docs/
以上就是适用于 Golang 的新 PostgreSQL ORM:企业版的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2311138.html