Code Monkey home page Code Monkey logo

reversex's Introduction

Reverse

A flexible and powerful tool for reverse database to model codes.
Implementation reference xorm.io/reverse

Optimization

  1. Optimized the speed of obtaining database table metadata.
  2. Support special field definition of orm framework.
  3. Support SqlType unsigned integer convert to uint for go language.
  4. Support SqlType tinyint(1) convert to bool for go language

Installation

go install github.com/njfanxun/reversex@latest

Usage

reversex gen -f {dir}/config.yaml   # Generate a struct entity from database
reversex language                   # Print reversex supported language and framework
reversex version                    # Print the version of reversex

Use "reversex help [command]" for more information on a command.

Configuration File

kind: reverse           # --required, only kind reverse
name: mydb              # --required,this config name
source:
  database: mysql       # --required, database driver name (mysql|sqlite3|postgres|mssql)
  conn_str: ""          # --required, database DSN:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...]
target:
  language: golang      # --required, development language(golang)
  include_tables:       # --optional, the include tables array list
  exclude_tables:       # --optional, the exclude tables array list
  mapper: snake         # --optional, table name to code class or struct mapping relationship(snake|gonic|same)
  table_prefix: ""      # --optional, struct name prefix
  orm_fields:           # --optional, xorm framework the special field identification, map[string]string
    created: created_at # key(orm field): value(database column)
    updated: updated_at
    version: version
  template_path: ""     # --optional, custom generate file template
  output_dir: ./models  # --required, output directory,the latest dir is package name

Default goland template

package {{Package}}

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

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

    func ({{TableMapper .Name}}) TableName() string {
    return "{{$table.Name}}"
    }

    func ({{TableMapper .Name}}) Cols() []string {

    return {{ Columns .}}
    }
{{end}}

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
  • Columns: return a table all columns to a array string
  • Imports: return a go file all imports
  • Package: return model files package

Dependencies

  • github.com/cockroachdb/errors
  • github.com/denisenkom/go-mssqldb
  • github.com/go-sql-driver/mysql
  • github.com/gobwas/glob
  • github.com/lib/pq
  • github.com/mattn/go-sqlite3
  • github.com/novalagung/gubrak/v2
  • github.com/pterm/pterm
  • github.com/sirupsen/logrus
  • github.com/spf13/cobra
  • github.com/spf13/viper
  • xorm.io/xorm

Todo

support for languages such as java(MyBatis), node(Sequelize), etc.

reversex's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

clyuz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.