modelq

ModelQ is a code generator for creating Golang codes/models to access RDBMS database/tables

  • 所有者: mijia/modelq
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

ModelQ

ModelQ is a code generator for creating Golang codes/models to access RDBMS database/tables (only MySQL/PostgresQL supported for now).

Updates

  1. Add the template support for generated codes, e.g. examples/custom.tmpl, you can define you own code segments for each part and don't need to define all of them, the generated code is listed in examples/custom/*.go

Simple Idea

Read the schema from MySQL/PostgresQL database (the whole database or only some tables), and Bang! The go models are there. I embrace the "SQL First" for modeling the business, then use the ModelQ to generate corresponding models for accessing. ModelQ is concerning about two aspects:

  1. Easy CRUD interface and query builder but without the golang reflection involved.
  2. Facilitate the Go compiler for the correctness (I think this is very important.)

A simple example could be found under ./examples, it is about a blog model contains Users and Articles. So the database can be set up using the examples/blog.mysql.sql or examples/blog.pq.sql, then run

$ modelq -db="root@/blog" -pkg=mysql -tables=user,article -driver=mysql -schema=blog

or

$ modelq -db="dbname=blog sslmode=disable" -pkg=postgres -tables=user,article -driver=postgres -schema=public

Then the models for User and Article would be generated in the directory of "./examples".

CLI Usage

-db="": Target database source string: e.g. root@tcp(127.0.0.1:3306)/test?charset=utf-8
-dont-touch-timestamp=false: Should touch the datetime fields with default value or on update
-driver="mysql": Current supported drivers include mysql, postgres
-p=4: Parallell running for code generator
-pkg="": Go source code package for generated models
-schema="": Schema for postgresql, database name for mysql
-tables="": You may specify which tables the models need to be created, e.g. "user,article,blog"
-template="": Passing the template to generate code, or use the default one

You can embed this CLI command in go generate tools

API

Please check the ./examples/model_test.go to take a glance. Some basic queries are supported, and to take advantage of the compiler, have to use many type guarded funcs defined for the model, e.g.

objs := models.UserObjs
users, err := objs.Select("Id", "Name", "Age").
                   Where(objs.FilterAge(">=", 15).Or(objs.FilterAge("IN", 8, 9, 10))).
                   OrderBy("-Age", "Name").Limit(1, 20).List(db)

The Age of User model is a int, so go compiler will complain if a string is sent in like objs.FilterAge(">", "15"). ModelQ will generate all the filters for each field/column of each model then the type requirements would be in the func signatures.

To support different drivers, modelq have to use gmq.Open and gmq.Beginx for gmq.Db and gmq.Tx objects, like

db, err := gmq.Open("postgres", "dbname=blog sslmode=disable")
tx, err := db.Beginx()
gmq.WithinTx(db, func(tx *gmq.Tx) error {...})

Can't do so far

This is only a early rough implementation, missing a lot of things so far.

  • distinct, sum, average and etc. Definitely will get those.
  • Joins and Unions. Those seems very likely to the count/distinct/sum and etc. Complicated data structure may be needed.
  • The generated models rely on the modelq/gmq package, I am not sure if this would be OK, or could this be changable and plugable, no idea so far.
  • No relations for complicated modeling (maybe will never consider this)
  • Only MySQL, PostgresQL supported
  • Seems github.com/lib/pq has problems to support time.Time scan

But I just want to release it early and get the feedbacks early. So ideas and pull requests would be really welcomed and appreciated!

License

MIT

主要指標

概覽
名稱與所有者mijia/modelq
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2014-12-03 06:46:35
推送於2020-02-09 04:19:32
最后一次提交2020-02-09 12:19:30
發布數0
用户参与
星數291
關注者數12
派生數40
提交數104
已啟用問題?
問題數19
打開的問題數11
拉請求數25
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?