DBML-go is a Go parser for DBML syntax.
Go get
go get github.com/vanhtuan0409/dbml-go/...
package main
import (
"fmt"
"os"
"github.com/vanhtuan0409/dbml-go/parser"
"github.com/vanhtuan0409/dbml-go/scanner"
)
func main() {
f, _ := os.Open("test.dbml")
s := scanner.NewScanner(f)
parser := parser.NewParser(s)
dbml, err := parser.Parse()
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
// process dbml here
}
go get github.com/vanhtuan0409/dbml-go/cmd/dbml-go-gen-model
Usage:
dbml-gen-go-model [flags]
Flags:
-E, --exclude string regex for exclude "from" files. Only applied if "from" is a directory
-t, --fieldtags stringArray go field tags (default [db,json,mapstructure])
-f, --from string source of dbml, can be https://dbdiagram.io/... | fire_name.dbml (default "database.dbml")
--gen-table-name should generate "TableName" function
-h, --help help for dbml-gen-go-model
-o, --out string output folder (default "model")
-p, --package string single for multiple files (default "model")
--parse-annotation Should parse annotations in "note" text
--recursive recursive search directory. Only applied if "from" is a directory
input:
// database.dbml
Table users as U {
id int [pk, unique, increment] // auto-increment
full_name varchar [not null, unique, default: 1]
created_at timestamp
country_code int
Note: 'This is simple note'
}
Run:
mkdir -p model
dbml-gen-go-model -f database.dbml -o model -p model
output: model/users.table.go
// Code generated by dbml-gen-go-model. DO NOT EDIT.
// Supported by vanhtuan0409@2020
package model
// User is generated type for table 'users'
type User struct {
ID int `db:"id" json:"id" mapstructure:"id"`
FullName string `db:"full_name" json:"full_name" mapstructure:"full_name"`
CreatedAt int `db:"created_at" json:"created_at" mapstructure:"created_at"`
CountryCode int `db:"country_code" json:"country_code" mapstructure:"country_code"`
}
// table 'users' columns list struct
type __tbl_users_columns struct {
ID string
FullName string
CreatedAt string
CountryCode string
}
// table 'users' metadata struct
type __tbl_users struct {
Name string
Columns __tbl_users_columns
}
// table 'users' metadata info
var _tbl_users = __tbl_users{
Columns: __tbl_users_columns{
CountryCode: "country_code",
CreatedAt: "created_at",
FullName: "full_name",
ID: "id",
},
Name: "users",
}
// GetColumns return list columns name for table 'users'
func (*__tbl_users) GetColumns() []string {
return []string{"id", "full_name", "created_at", "country_code"}
}
// T return metadata info for table 'users'
func (*User) T() *__tbl_users {
return &_tbl_users
}
DBML-go extends DBML with Annotation concepts. Annotation is the markup at the very beginning of note
and only available on Project
, Table
and Column
. Annotation serve the purpose of provide extra information for code generation.
Syntax of annotation as follow: @{key}({value})
. Multiple annotations are separated by comma ,
. Examples:
- Single annotation:
[note: '@gotype(github.com/foo/bar:CustomValue)']
- Multiple annotation:
[note: '@gotype(time:Time), @gopointer(true)']
- Mixed with data:
[note: '@gopointer(true) My example note']
Only these following annotation are understood by DBML-go
Annotate on DBML Column
. The annotated column will be generated with that Go Struct.
For example: you have a custom struct github.com/foo/bar:CustomValue
which implement sql.Scanner
and driver.Valuer
. This struct will be serialized into DB as varchar
. Then you can have as follow:
// database.dbml
Table users as U {
my_custom_value varchar [note:'@gotype(github.com/foo/bar:CustomValue)']
}
Then the generated struct is:
package model
// User is generated type for table 'users'
type User struct {
// @gotype(github.com/foo/bar:CustomValue)
MyCustomValue bar.CustomValue `gorm:"my_custom_value"`
}
Annotate on DBML Column
. The annotated column will be generated as pointer of the target struct. Annotation value must be "true"
For example:
// database.dbml
Table users as U {
my_custom_value varchar [note:'@gopointer(true)']
}
Then the generated struct is:
package model
// User is generated type for table 'users'
type User struct {
// @gopointer(true)
MyCustomValue *string `gorm:"my_custom_value"`
}