Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/duythinht/dbml-go
DBML parser and tools for Go
https://github.com/duythinht/dbml-go
dbml dbml-go golang
Last synced: 30 days ago
JSON representation
DBML parser and tools for Go
- Host: GitHub
- URL: https://github.com/duythinht/dbml-go
- Owner: duythinht
- License: mit
- Created: 2020-05-13T19:48:40.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-11T17:33:18.000Z (over 1 year ago)
- Last Synced: 2024-08-03T17:17:31.905Z (4 months ago)
- Topics: dbml, dbml-go, golang
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 120
- Watchers: 4
- Forks: 26
- Open Issues: 5
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
- go-awesome - dbml-go - [DBML](https://github.com/holistics/dbml) Analysis (Open source library / Database)
README
# DBML parser for Go
DBML-go is a Go parser for [DBML](https://www.dbml.org) syntax.
## Installation
Go get
```bash
go get github.com/duythinht/dbml-go/...
```## Quick start
```go
package mainimport (
"fmt"
"os""github.com/duythinht/dbml-go/parser"
"github.com/duythinht/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 model generate
```bash
go get github.com/duythinht/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")
--recursive recursive search directory. Only applied if "from" is a directory
--remember-alias should remember table alias. Only applied if "from" is a directory
```#### Example:
input:
```dbml
// 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:
```bash
mkdir -p model
dbml-gen-go-model -f database.dbml -o model -p model
```output: model/users.table.go
```go
// Code generated by dbml-gen-go-model. DO NOT EDIT.
// Supported by duythinht@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
}```