A flexsible and powerful command line tool to convert database to codes
Go to file
fuhui 7054d8a3a9 支持包的 import 调用
- 引入了 src 参数用于直接传递字节流的配置
2021-07-28 17:43:08 +08:00
cmd 支持包的 import 调用 2021-07-28 17:43:08 +08:00
example refactor: improve flexibility and project structure (#28) 2021-06-14 22:17:18 +08:00
models refactor: improve flexibility and project structure (#28) 2021-06-14 22:17:18 +08:00
pkg 支持包的 import 调用 2021-07-28 17:43:08 +08:00
testdata init project 2019-12-28 16:54:49 +08:00
vendor refactor: improve flexibility and project structure (#28) 2021-06-14 22:17:18 +08:00
.drone.yml add macOS release 2020-10-25 19:39:33 +08:00
.gitignore fix lint 2020-01-19 16:16:31 +08:00
go.mod refactor: improve flexibility and project structure (#28) 2021-06-14 22:17:18 +08:00
go.sum refactor: improve flexibility and project structure (#28) 2021-06-14 22:17:18 +08:00
LICENSE init project 2019-12-28 16:54:49 +08:00
main.go fix lints 2020-01-19 16:11:21 +08:00
README_CN.md Fix docs 2020-12-21 16:10:19 +08:00
README.md Fix docs 2020-12-21 16:10:19 +08:00

中文

Build Status

Reverse

A flexsible and powerful command line tool to convert database to codes.

Installation

go get xorm.io/reverse

Usage

reverse -f example/custom.yml

Configuration File

How does the simplest configuration file look like?

kind: reverse
name: mydb
source:
  database: sqlite3
  conn_str: '../testdata/test.db'
targets:
- type: codes
  language: golang
  output_dir: ../models

A language defines some default configuration items, also you can define all yourselves.

kind: reverse
name: mydb
source:
  database: sqlite
  conn_str: ../testdata/test.db
targets:
- type: codes
  include_tables: # tables included, you can use **
    - a
    - b
  exclude_tables: # tables excluded, you can use **
    - c
  table_mapper: snake # how table name map to class or struct name
  column_mapper: snake # how column name map to class or struct field name
  table_prefix: "" # table prefix
  multiple_files: true # generate multiple files or one
  language: golang
  template: | # template for code file, it has higher perior than template_path
    package models

    {{$ilen := len .Imports}}
    {{if gt $ilen 0}}
    import (
      {{range .Imports}}"{{.}}"{{end}}
    )
    {{end}}

    {{range .Tables}}
    type {{TableMapper .Name}} struct {
    {{$table := .}}
    {{range .ColumnsSeq}}{{$col := $table.GetColumn .}}	{{ColumnMapper $col.Name}}	{{Type $col}} `{{Tag $table $col}}`
    {{end}}
    }

    func (m *{{TableMapper .Name}}) TableName() string {
    	return "{{$table.Name}}"
    }
    {{end}}
  template_path: ./template/goxorm.tmpl # template path for code file, it has higher perior than template field on language
  output_dir: ./models # code output directory

Template Funcs

  • UnTitle: Convert first charator of the word to lower.
  • Upper: Convert word to all upper.
  • TableMapper: Mapper method to convert table name to class/struct name.
  • ColumnMapper: Mapper method to convert column name to class/struct field name.

Golang Template Funcs

  • Type: return column's golang type
  • Tag: return golang struct tag for column

Template Vars

  • Tables: All tables.
  • Imports: All imports needed.