MCPcopy Index your code
hub / github.com/ezbuy/ezorm

github.com/ezbuy/ezorm @v2.8.14

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.8.14 ↗ · + Follow
1,059 symbols 2,613 edges 82 files 100 documented · 9%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ezorm.v2

parser e2e CodeQL

Why another ORM for Go ?

With many years Go development experience in ezbuy , we find that define db schema first and share this schema within the project members or DBAs is really an advanced idea , like Protobuf for API-oriented development.

And the idea seems not alone within the Go community , projects like ent-go prove that there is a way to make a best-practice for Go ORM(like) Programming.

ezorm was built with this key idea in mind , but we describe the database schema as YAML file (or raw SQL file) , and the builtin generator(also the compiler) can generate some safe and most-used database operating methods for us , which lets business developers to focus on the business logic , rather than the bored CRUD .

Support Database

  • MySQL
    • driver: db: mysql
    • driver: db: mysqlr
  • MongoDB (v4.2+)
  • ~~Redis(deprecated since v2)~~
  • ~~SQL Server(deprecated since v2)~~
  • 3rd customized plugin

Schema

See full list of schema in our doc .

YAML Schema

Schema is defined with YAML file like:

Blog:
  db: mongo
  fields:
    - Title: string
    - Hits: int32
    - Slug: string
      flags: [unique]
    - Body: string
    - User: int32
    - CreateDate: datetime
      flags: [sort]
    - IsPublished: bool
      flags: [index]
  indexes: [[User, IsPublished]]

Id field will be automatically included for mongo/mysql.

SQL Schema

SQL Schema is introduced in v2 , and tries to help with raw query , like table JOIN , which can not be handled properly by YAML schema.

Inspired by sqlc's great AST analysis , we can extract the AST of SQL like:

SELECT
  name
FROM test_user
WHERE name = "me";

and generates the following Go Code :

type GetUserResp struct {
    Name string `sql:"name"`
}

type GetUserReq struct {
    Name string `sql:"name"`
}

func (req *GetUserReq) Params() []any {
    var params []any

    params = append(params, req.Name)

    return params
}

const _GetUserSQL = "SELECT `name` FROM `test_user` WHERE `name`=?"

// GetUser is a raw query handler generated function for `example/mysql_people/sqls/get_user.sql`.
func (*sqlMethods) GetUser(ctx context.Context, req *GetUserReq) ([]*GetUserResp, error) {

    query := _GetUserSQL

    rows, err := db.MysqlQuery(query, req.Params()...)
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    var results []*GetUserResp
    for rows.Next() {
        var o GetUserResp
        err = rows.Scan(&o.Name)
        if err != nil {
            return nil, err
        }
        results = append(results, &o)
    }
    return results, nil
}

Usage

ezorm requires Go 1.18 or later.

    $ go install github.com/ezbuy/ezorm/v2
    $ ezorm gen -i blog.yaml -o .

To generate codes, for model like Blog, a blog manager will be generated, supporting ActiveRecord like:

```go p := blog.BlogMgr.NewBlog() p.Title = "I like ezorm" p.Slug = "ezorm" p.Save()

p, err := blog.BlogMgr.FindBySlug("ezorm") if err != nil { // handle error } fmt.Println("%v", p) page.PageMgr.RemoveByID(p.Id())

_, err = blog.BlogMgr.FindBySlug("ezorm") if err == nil { // handle error } ```

use ezorm -h for more help

Extension points exported contracts — how you extend this code

Unique (Interface)
(no doc) [8 implementers]
e2e/mysqlr/gen.orm.mysql.go
EzOrmObj (Interface)
(no doc) [4 implementers]
v2/pkg/orm/orm.go
Generator (Interface)
(no doc) [4 implementers]
internal/generator/gen.go
RawQueryParser (Interface)
RawQueryParser is a parser to extract metedata from sql query [1 implementers]
internal/parser/x/query/sql.go
SetupOptionFn (FuncType)
(no doc)
e2e/mongo/user/gen_mongo_config.go
RawQueryOptionHandler (FuncType)
(no doc)
e2e/mysql/gen_methods.go
SQL (Interface)
(no doc) [5 implementers]
e2e/mysqlr/gen.orm.mysql.go
DB (Interface)
(no doc) [2 implementers]
v2/pkg/orm/db.go

Core symbols most depended-on inside this repo

Close
called by 68
v2/pkg/orm/db.go
String
called by 45
internal/parser/x/query/sql.go
Get_UserMgr
called by 36
e2e/mongo/user/gen_User_struct.go
BlogDBMgr
called by 26
e2e/mysqlr/gen.mysqlr.blog.go
MgoSetup
called by 25
e2e/mongo/user/gen_mongo_config.go
Exec
called by 24
v2/pkg/orm/db.go
SQLFormat
called by 24
e2e/mysqlr/gen.orm.mysql.go
WithMockStub
called by 21
e2e/mongo/user/gen_mongo_config.go

Shape

Method 660
Function 247
Struct 112
TypeAlias 15
Interface 14
FuncType 11

Languages

Go100%

Modules by API surface

e2e/mysqlr/gen.mysqlr.blog.go77 symbols
e2e/mysqlr/gen.mysqlr.user.go53 symbols
e2e/mysqlr/gen.mysqlr.auto.blog.go52 symbols
e2e/mysql/gen_Blog_mysql_orm.go49 symbols
internal/parser/shared/field.go48 symbols
internal/parser/mysqlr/field.go48 symbols
internal/parser/shared/obj.go36 symbols
e2e/mysqlr/gen_methods.go35 symbols
e2e/mysql/gen_User_mysql_orm.go34 symbols
v2/pkg/orm/orm.go30 symbols
v2/pkg/orm/db.go27 symbols
e2e/mongo/user/gen_User_mongo_orm.go27 symbols

Datastores touched

(mongodb)Database · 1 repos
(mysql)Database · 1 repos

For agents

$ claude mcp add ezorm \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact