Converts a database into gorm structs and RESTful api

gen

License GoDoc travis Go Report Card

The gen tool produces a CRUD (Create, read, update and delete) REST api project template from a given database. The gen tool will connect to the db connection string analyze the database and generate the code based on the flags provided.

By reading details from the database about the column structure, gen generates a go compatible struct type with the required column names, data types, and annotations.

It supports gorm tags and implements some usable methods. Generated data types include support for nullable columns sql.NullX types or guregu null.X types and the expected basic built in go types.

gen is based / inspired by the work of Seth Shelnutt's db2struct, and Db2Struct is based/inspired by the work of ChimeraCoder's gojson package gojson.

CRUD Generation

This is a sample table contained within the ./example/sample.db Sqlite3 database. Using gen will generate the following struct.

CREATE TABLE "albums"
(
    [AlbumId]  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Title]    NVARCHAR(160) NOT NULL,
    [ArtistId] INTEGER NOT NULL,
    FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])
		ON DELETE NO ACTION ON UPDATE NO ACTION
)

Transforms into

type Album struct {
	//[ 0] AlbumId                                        integer              null: false  primary: true   auto: true   col: integer         len: -1      default: []
	AlbumID int `gorm:"primary_key;AUTO_INCREMENT;column:AlbumId;type:INTEGER;" json:"album_id" db:"AlbumId" protobuf:"int32,0,opt,name=album_id"`
	//[ 1] Title                                          nvarchar(160)        null: false  primary: false  auto: false  col: nvarchar        len: 160     default: []
	Title string `gorm:"column:Title;type:NVARCHAR(160);size:160;" json:"title" db:"Title" protobuf:"string,1,opt,name=title"`
	//[ 2] ArtistId                                       integer              null: false  primary: false  auto: false  col: integer         len: -1      default: []
	ArtistID int `gorm:"column:ArtistId;type:INTEGER;" json:"artist_id" db:"ArtistId" protobuf:"int32,2,opt,name=artist_id"`
}

Code generation for a complete CRUD rest project is possible with DAO crud functions, http handlers, makefile, sample server are available. Check out some of the code generated samples.

Binary Installation

## install gen tool (should be installed to ~/go/bin, make sure ~/go/bin is in your path.

## go version < 1.17
$ go get -u github.com/smallnest/gen

## go version == 1.17
$ go install github.com/smallnest/gen@latest

## download sample sqlite database
$ wget https://github.com/smallnest/gen/raw/master/example/sample.db

## generate code based on the sqlite database (project will be contained within the ./example dir)
$ gen --sqltype=sqlite3 \
   	--connstr "./sample.db" \
   	--database main  \
   	--json \
   	--gorm \
   	--guregu \
   	--rest \
   	--out ./example \
   	--module example.com/rest/example \
   	--mod \
   	--server \
   	--makefile \
   	--json-fmt=snake \
   	--generate-dao \
   	--generate-proj \
   	--overwrite

## build example code (build process will install packr2 if not installed)
$ cd ./example
$ make example

## binary will be located at ./bin/example
## when launching make sure that the SQLite file sample.db is located in the same dir as the binary
$ cp ../../sample.db  .
$ ./example


## Open a browser to http://127.0.0.1:8080/swagger/index.html

## Use wget/curl/httpie to fetch via command line
http http://localhost:8080/albums
curl http://localhost:8080/artists

Usage

Usage of gen:
	gen [-v] --sqltype=mysql --connstr "user:password@/dbname" --database <databaseName> --module=example.com/example [--json] [--gorm] [--guregu] [--generate-dao] [--generate-proj]
git fetch up
           sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]


Options:
  --sqltype=mysql                                          sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]
  -c, --connstr=nil                                        database connection string
  -d, --database=nil                                       Database to for connection
  -t, --table=                                             Table to build struct from
  -x, --exclude=                                           Table(s) to exclude
  --templateDir=                                           Template Dir
  --fragmentsDir=                                          Code fragments Dir
  --save=                                                  Save templates to dir
  --model=model                                            name to set for model package
  --model_naming={{FmtFieldName .}}                        model naming template to name structs
  --field_naming={{FmtFieldName (stringifyFirstChar .) }}  field naming template to name structs
  --file_naming={{.}}                                      file_naming template to name files
  --dao=dao                                                name to set for dao package
  --api=api                                                name to set for api package
  --grpc=grpc                                              name to set for grpc package
  --out=.                                                  output dir
  --module=example.com/example                             module path
  --overwrite                                              Overwrite existing files (default)
  --no-overwrite                                           disable overwriting files
  --windows                                                use windows line endings in generated files
  --no-color                                               disable color output
  --context=                                               context file (json) to populate context with
  --mapping=                                               mapping file (json) to map sql types to golang/protobuf etc
  --exec=                                                  execute script for custom code generation
  --json                                                   Add json annotations (default)
  --no-json                                                Disable json annotations
  --json-fmt=snake                                         json name format [snake | camel | lower_camel | none]
  --xml                                                    Add xml annotations (default)
  --no-xml                                                 Disable xml annotations
  --xml-fmt=snake                                          xml name format [snake | camel | lower_camel | none]
  --gorm                                                   Add gorm annotations (tags)
  --protobuf                                               Add protobuf annotations (tags)
  --proto-fmt=snake                                        proto name format [snake | camel | lower_camel | none]
  --gogo-proto=                                            location of gogo import 
  --db                                                     Add db annotations (tags)
  --guregu                                                 Add guregu null types
  --copy-templates                                         Copy regeneration templates to project directory
  --mod                                                    Generate go.mod in output dir
  --makefile                                               Generate Makefile in output dir
  --server                                                 Generate server app output dir
  --generate-dao                                           Generate dao functions
  --generate-proj                                          Generate project readme and gitignore
  --rest                                                   Enable generating RESTful api
  --run-gofmt                                              run gofmt on output dir
  --listen=                                                listen address e.g. :8080
  --scheme=http                                            scheme for server url
  --host=localhost                                         host for server
  --port=8080                                              port for server
  --swagger_version=1.0                                    swagger version
  --swagger_path=/                                         swagger base path
  --swagger_tos=                                           swagger tos url
  --swagger_contact_name=Me                                swagger contact name
  --swagger_contact_url=http://me.com/terms.html           swagger contact url
  [email protected]                        swagger contact email
  -v, --verbose                                            Enable verbose output
  --name_test=                                             perform name test using the --model_naming or --file_naming options
  -h, --help                                               Show usage message
  --version                                                Show version

Building

The project contains a makefile for easy building and common tasks.

  • go get - get the relevant dependencies as a "go" software
  • make help - list available targets
  • make build - generate the binary ./gen
  • make example - run the gen process on the example SqlLite db located in ./examples place the sources in ./example Other targets exist for dev tasks.

Example

The project provides a sample SQLite database in the ./example directory. From the project Makefile can be used to generate the example code.

make example

The generated project will contain the following code under the ./example directory.

  • Makefile
    • useful Makefile for installing tools building project etc. Issue make to display help output.
  • .gitignore
    • git ignore for go project
  • go.mod
    • go module setup, pass --module flag for setting the project module default example.com/example
  • README.md
    • Project readme
  • app/server/main.go
    • Sample Gin Server, with swagger init and comments
  • api/.go
    • REST crud controllers
  • dao/
  • .go
    • DAO functions providing CRUD access to database
  • model/
  • .go
    • Structs representing a row for each database table

    Generated Samples

    The REST api server utilizes the Gin framework, GORM db api and Swag for providing swagger documentation

    Supported Databases

    Currently Supported,

    • MariaDB
    • MySQL
    • PostgreSQL
    • Microsoft SQL Server
    • SQLite

    Planned Support

    • Oracle

    Supported Data Types

    Most data types are supported, for Mysql, Postgres, SQLite and MS SQL. gen uses a mapping json file that can be used to add mapping types. By default, the internal mapping file is loaded and processed. If can be overwritten or additional types added by using the --mapping=extra.json command line option.

    The default mapping.json file is located within the ./templates dir. Use gen --save=./templates to save the contents of the templates to ./templates. Below is a portion of the mapping file, showing the mapping for varchar.

        {
          "sql_type": "varchar",
          "go_type": "string",
          "protobuf_type": "bytes",
          "guregu_type": "null.String",
          "go_nullable_type": "sql.NullString"
        }

    Advanced

    The gen tool provides functionality to layout your own project format. Users have 2 options.

    • Provide local templates with the --templateDir= option - this will generate code using the local templates. Templates can either be exported from gen via the command gen --save ./mytemplates. This will save the embedded templates for local editing. Then you would specify the --templateDir= option when generating a project.

    • Passing --exec=../sample.gen on the command line will load the sample.gen script and execute it. The script has access to the table information and other info passed to gen. This allows developers to customize the generation of code. You could loop through the list of tables and invoke GenerateTableFile or GenerateFile. You can also perform operations such as mkdir, copy, touch, pwd.

    Example - generate files from a template looping thru a map of tables.

    Loop thru map of tables, key is the table name and value is ModelInfo. Creating a file using the table ModelInfo.

    tableInfos := map[string]*ModelInfo

    GenerateTableFile(tableInfos map[string]*ModelInfo, tableName, templateFilename, outputDirectory, outputFileName string, formatOutput bool)

    
    {{ range $tableName , $table := .tableInfos }}
       {{$i := inc }}
       {{$name := toUpper $table.TableName -}}
       {{$filename  := printf "My%s.go" $name -}}
    
       {{ GenerateTableFile $.tableInfos $table.TableName  "custom.go.tmpl" "test" $filename true}}{{- end }}
    
    

    Example - generate file from a template.

    GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOutput bool, overwrite bool)

    
    {{ GenerateFile "custom.md.tmpl" "test" "custom.md" false false }}
    
    

    Example - make a directory.

    
    {{ mkdir "test/alex/test/mkdir" }}
    
    

    Example - touch a file.

    
    {{ touch "test/alex/test/mkdir/alex.txt" }}
    
    

    Example - display working directory.

    
    {{ pwd }}
    
    

    Example - copy a file or directory from source to a target directory.

    copy function updated to provide --include and --exclude patterns. Patterns are processed in order, an include preceeding an exclude will take precedence. Multiple include and excludes can be specified. Files ending with .table.tmpl will be processed for each table. Output filenames will be stored in the proper directory, with a name of the table with the suffix of the template extension. Files ending with .tmpl will be processed as a template and the filename will be the name of the template stripped with the .tmpl suffix.

    
    {{ copy "../_test" "test" }}
    
    {{ copy "./backend" "test/backend" "--exclude .idea|commands" "--exclude go.sum"  "--include .*" }}
    
    {{ copy "./backend" "test/backend" "--include backend" "--include go.mod" "--exclude .*"  }}
    
    
    

    You can also populate the context used by templates with extra data by passing the --context=<json file> option. The json file will be used to populate the context used when parsing templates.

    File Generation

    // Loop through tables and print out table name and various forms of the table name
    
    {{ range $i, $table := .tables }}
        {{$singular   := singular $table -}}
        {{$plural     := pluralize $table -}}
        {{$title      := title $table -}}
        {{$lower      := toLower $table -}}
        {{$lowerCamel := toLowerCamelCase $table -}}
        {{$snakeCase  := toSnakeCase $table -}}
        {{ printf "[%-2d] %-20s %-20s %-20s %-20s %-20s %-20s %-20s" $i $table $singular $plural $title $lower $lowerCamel $snakeCase}}{{- end }}
    
    
    {{ range $i, $table := .tables }}
       {{$name := toUpper $table -}}
       {{$filename  := printf "My%s" $name -}}
       {{ printf "[%-2d] %-20s %-20s" $i $table $filename}}
       {{ GenerateTableFile $table  "custom.go.tmpl" "test" $filename true}}
    {{- end }}
    
    
    // GenerateTableFile(tableInfos map[string]*ModelInfo, tableName, templateFilename, outputDirectory, outputFileName string, formatOutput bool)
    // GenerateFile(templateFilename, outputDirectory, outputFileName string, formatOutput bool, overwrite bool)
    
    The following info is available within use of the exec template.
    
    
       "AdvancesSample"            string                         "\n{{ range $i, $table := .tables }}\n    {{$singular   := singular $table -}}\n    {{$plural     := pluralize $table -}}\n    {{$title      := title $table -}}\n    {{$lower      := toLower $table -}}\n    {{$lowerCamel := toLowerCamelCase $table -}}\n    {{$snakeCase  := toSnakeCase $table -}}\n    {{ printf \"[%-2d] %-20s %-20s %-20s %-20s %-20s %-20s %-20s\" $i $table $singular $plural $title $lower $lowerCamel $snakeCase}}{{- end }}\n\n\n{{ range $i, $table := .tables }}\n   {{$name := toUpper $table -}}\n   {{$filename  := printf \"My%s\" $name -}}\n   {{ printf \"[%-2d] %-20s %-20s\" $i $table $filename}}\n   {{ GenerateTableFile $table  \"custom.go.tmpl\" \"test\" $filename true}}\n{{- end }}\n"
       "Config"                    *dbmeta.Config                 &dbmeta.Config{SQLType:"sqlite3", SQLConnStr:"./example/sample.db", SQLDatabase:"main", Module:"github.com/alexj212/test", ModelPackageName:"model", ModelFQPN:"github.com/alexj212/test/model", AddJSONAnnotation:true, AddGormAnnotation:true, AddProtobufAnnotation:true, AddXMLAnnotation:true, AddDBAnnotation:true, UseGureguTypes:false, JSONNameFormat:"snake", XMLNameFormat:"snake", ProtobufNameFormat:"", DaoPackageName:"dao", DaoFQPN:"github.com/alexj212/test/dao", APIPackageName:"api", APIFQPN:"github.com/alexj212/test/api", GrpcPackageName:"", GrpcFQPN:"", Swagger:(*dbmeta.SwaggerInfoDetails)(0xc000ad0510), ServerPort:8080, ServerHost:"127.0.0.1", ServerScheme:"http", ServerListen:":8080", Verbose:false, OutDir:".", Overwrite:true, LineEndingCRLF:false, CmdLine:"/tmp/go-build271698611/b001/exe/readme --sqltype=sqlite3 --connstr ./example/sample.db --database main --table invoices", CmdLineWrapped:"/tmp/go-build271698611/b001/exe/readme \\\n    --sqltype=sqlite3 \\\n    --connstr \\\n    ./example/sample.db \\\n    --database \\\n    main \\\n    --table \\\n    invoices", CmdLineArgs:[]string{"/tmp/go-build271698611/b001/exe/readme", "--sqltype=sqlite3", "--connstr", "./example/sample.db", "--database", "main", "--table", "invoices"}, FileNamingTemplate:"{{.}}", ModelNamingTemplate:"{{FmtFieldName .}}", FieldNamingTemplate:"{{FmtFieldName (stringifyFirstChar .) }}", ContextMap:map[string]interface {}{"GenHelp":"Usage of gen:\n\tgen [-v] --sqltype=mysql --connstr \"user:password@/dbname\" --database <databaseName> --module=example.com/example [--json] [--gorm] [--guregu] [--generate-dao] [--generate-proj]\ngit fetch up\n           sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]\n\n\nOptions:\n  --sqltype=mysql                                          sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]\n  -c, --connstr=nil                                        database connection string\n  -d, --database=nil                                       Database to for connection\n  -t, --table=                                             Table to build struct from\n  -x, --exclude=                                           Table(s) to exclude\n  --templateDir=                                           Template Dir\n  --save=                                                  Save templates to dir\n  --model=model                                            name to set for model package\n  --model_naming={{FmtFieldName .}}                        model naming template to name structs\n  --field_naming={{FmtFieldName (stringifyFirstChar .) }}  field naming template to name structs\n  --file_naming={{.}}                                      file_naming template to name files\n  --dao=dao                                                name to set for dao package\n  --api=api                                                name to set for api package\n  --grpc=grpc                                              name to set for grpc package\n  --out=.                                                  output dir\n  --module=example.com/example                             module path\n  --overwrite                                              Overwrite existing files (default)\n  --no-overwrite                                           disable overwriting files\n  --windows                                                use windows line endings in generated files\n  --no-color                                               disable color output\n  --context=                                               context file (json) to populate context with\n  --mapping=                                               mapping file (json) to map sql types to golang/protobuf etc\n  --exec=                                                  execute script for custom code generation\n  --json                                                   Add json annotations (default)\n  --no-json                                                Disable json annotations\n  --json-fmt=snake                                         json name format [snake | camel | lower_camel | none]\n  --xml                                                    Add xml annotations (default)\n  --no-xml                                                 Disable xml annotations\n  --xml-fmt=snake                                          xml name format [snake | camel | lower_camel | none]\n  --gorm                                                   Add gorm annotations (tags)\n  --protobuf                                               Add protobuf annotations (tags)\n  --proto-fmt=snake                                        proto name format [snake | camel | lower_camel | none]\n  --gogo-proto=                                            location of gogo import \n  --db                                                     Add db annotations (tags)\n  --guregu                                                 Add guregu null types\n  --copy-templates                                         Copy regeneration templates to project directory\n  --mod                                                    Generate go.mod in output dir\n  --makefile                                               Generate Makefile in output dir\n  --server                                                 Generate server app output dir\n  --generate-dao                                           Generate dao functions\n  --generate-proj                                          Generate project readme and gitignore\n  --rest                                                   Enable generating RESTful api\n  --run-gofmt                                              run gofmt on output dir\n  --listen=                                                listen address e.g. :8080\n  --scheme=http                                            scheme for server url\n  --host=localhost                                         host for server\n  --port=8080                                              port for server\n  --swagger_version=1.0                                    swagger version\n  --swagger_path=/                                         swagger base path\n  --swagger_tos=                                           swagger tos url\n  --swagger_contact_name=Me                                swagger contact name\n  --swagger_contact_url=http://me.com/terms.html           swagger contact url\n  [email protected]                        swagger contact email\n  -v, --verbose                                            Enable verbose output\n  --name_test=                                             perform name test using the --model_naming or --file_naming options\n  -h, --help                                               Show usage message\n  --version                                                Show version\n\n", "tableInfos":map[string]*dbmeta.ModelInfo{"invoices":(*dbmeta.ModelInfo)(0xc0001e94a0)}}, TemplateLoader:(dbmeta.TemplateLoader)(0x8a7e40), TableInfos:map[string]*dbmeta.ModelInfo(nil)}
       "DatabaseName"              string                         "main"
       "Dir"                       string                         "."
       "File"                      string                         "./README.md"
       "GenHelp"                   string                         "Usage of gen:\n\tgen [-v] --sqltype=mysql --connstr \"user:password@/dbname\" --database <databaseName> --module=example.com/example [--json] [--gorm] [--guregu] [--generate-dao] [--generate-proj]\ngit fetch up\n           sqltype - sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]\n\n\nOptions:\n  --sqltype=mysql                                          sql database type such as [ mysql, mssql, postgres, sqlite, etc. ]\n  -c, --connstr=nil                                        database connection string\n  -d, --database=nil                                       Database to for connection\n  -t, --table=                                             Table to build struct from\n  -x, --exclude=                                           Table(s) to exclude\n  --templateDir=                                           Template Dir\n  --save=                                                  Save templates to dir\n  --model=model                                            name to set for model package\n  --model_naming={{FmtFieldName .}}                        model naming template to name structs\n  --field_naming={{FmtFieldName (stringifyFirstChar .) }}  field naming template to name structs\n  --file_naming={{.}}                                      file_naming template to name files\n  --dao=dao                                                name to set for dao package\n  --api=api                                                name to set for api package\n  --grpc=grpc                                              name to set for grpc package\n  --out=.                                                  output dir\n  --module=example.com/example                             module path\n  --overwrite                                              Overwrite existing files (default)\n  --no-overwrite                                           disable overwriting files\n  --windows                                                use windows line endings in generated files\n  --no-color                                               disable color output\n  --context=                                               context file (json) to populate context with\n  --mapping=                                               mapping file (json) to map sql types to golang/protobuf etc\n  --exec=                                                  execute script for custom code generation\n  --json                                                   Add json annotations (default)\n  --no-json                                                Disable json annotations\n  --json-fmt=snake                                         json name format [snake | camel | lower_camel | none]\n  --xml                                                    Add xml annotations (default)\n  --no-xml                                                 Disable xml annotations\n  --xml-fmt=snake                                          xml name format [snake | camel | lower_camel | none]\n  --gorm                                                   Add gorm annotations (tags)\n  --protobuf                                               Add protobuf annotations (tags)\n  --proto-fmt=snake                                        proto name format [snake | camel | lower_camel | none]\n  --gogo-proto=                                            location of gogo import \n  --db                                                     Add db annotations (tags)\n  --guregu                                                 Add guregu null types\n  --copy-templates                                         Copy regeneration templates to project directory\n  --mod                                                    Generate go.mod in output dir\n  --makefile                                               Generate Makefile in output dir\n  --server                                                 Generate server app output dir\n  --generate-dao                                           Generate dao functions\n  --generate-proj                                          Generate project readme and gitignore\n  --rest                                                   Enable generating RESTful api\n  --run-gofmt                                              run gofmt on output dir\n  --listen=                                                listen address e.g. :8080\n  --scheme=http                                            scheme for server url\n  --host=localhost                                         host for server\n  --port=8080                                              port for server\n  --swagger_version=1.0                                    swagger version\n  --swagger_path=/                                         swagger base path\n  --swagger_tos=                                           swagger tos url\n  --swagger_contact_name=Me                                swagger contact name\n  --swagger_contact_url=http://me.com/terms.html           swagger contact url\n  [email protected]                        swagger contact email\n  -v, --verbose                                            Enable verbose output\n  --name_test=                                             perform name test using the --model_naming or --file_naming options\n  -h, --help                                               Show usage message\n  --version                                                Show version\n\n"
       "NonPrimaryKeyNamesList"    []string                       []string{"CustomerId", "InvoiceDate", "BillingAddress", "BillingCity", "BillingState", "BillingCountry", "BillingPostalCode", "Total"}
       "NonPrimaryKeysJoined"      string                         "CustomerId,InvoiceDate,BillingAddress,BillingCity,BillingState,BillingCountry,BillingPostalCode,Total"
       "Parent"                    string                         "."
       "PrimaryKeyNamesList"       []string                       []string{"InvoiceId"}
       "PrimaryKeysJoined"         string                         "InvoiceId"
       "ReleaseHistory"            string                         "- v0.9.27 (08/04/2020)\n    - Updated '--exec' mode to provide various functions for processing\n    - copy function updated to provide --include and --exclude patterns. Patterns are processed in order, an include preceeding an exclude will take precedence. Multiple include and excludes can be specified. Files ending with .table.tmpl will be processed for each table. Output filenames will be stored in the proper directory, with a name of the table with the suffix of the template extension. Files ending with .tmpl will be processed as a template and the filename will be the name of the template stripped with the .tmpl suffix. \n    - When processing templates, files generated with a .go extension will be formatted with the go fmt.\n- v0.9.26 (07/31/2020)\n    - Release scripting\n    - Added custom script functions to copy, mkdir, touch, pwd\n    - Fixed custom script exec example\n- v0.9.25 (07/26/2020)\n    - Adhere json-fmt flag for all JSON response so when camel or lower_camel is specified, fields name in GetAll variant and DDL info will also have the same name format\n    - Fix: Build information embedded through linker in Makefile is not consistent with the variable defined in main file.\n    - Added --scheme and --listen options. This allows compiled binary to be used behind reverse proxy.\n    - In addition, template for generating URL was fixed, i.e. when PORT is 80, then PORT is omitted from URL segment.\n- v0.9.24 (07/13/2020)\n    - Fixed array bounds issue parsing mysql db meta\n- v0.9.23 (07/10/2020)\n    - Added postgres types: bigserial, serial, smallserial, bigserial, float4 to mapping.json\n- v0.9.22 (07/08/2020)\n    - Modified gogo.proto check to use GOPATH not hardcoded.\n    - Updated gen to error exit on first error encountered\n    - Added color output for error\n    - Added --no-color option for non colorized output\n- v0.9.21 (07/07/2020)\n    - Repacking templates, update version number in info.\n- v0.9.20 (07/07/2020)\n    - Fixed render error in router.go.tmpl\n    - upgraded project to use go.mod 1.14\n- v0.9.19 (07/07/2020)\n    - Added --windows flag to write files with CRLF windows line endings, otherwise they are all unix based LF line endings\n- v0.9.18 (06/30/2020)\n    - Fixed naming in templates away from hard coded model package.\n- v0.9.17 (06/30/2020)\n    - Refactored template loading, to better report error in template\n    - Added option to run gofmt on output directory\n- v0.9.16 (06/29/2020)\n    - Fixes to router.go.tmpl from calvinchengx\n    - Added postgres db support for inet and timestamptz\n- v0.9.15 (06/23/2020)\n    - Code cleanup using gofmt name suggestions.\n    - Template updates for generated code cleanup using gofmt name suggestions.\n- v0.9.14 (06/23/2020)\n    - Added model comment on field line if available from database.\n    - Added exposing TableInfo via api call.\n- v0.9.13 (06/22/2020)\n    - fixed closing of connections via defer\n    - bug fixes in sqlx generated code\n- v0.9.12 (06/14/2020)\n    - SQLX changed MustExec to Exec and checking/returning error\n    - Updated field renaming if duplicated, need more elegant renaming solution.\n    - Added exclude to test.sh\n- v0.9.11 (06/13/2020)\n    - Added ability to pass field, model and file naming format\n    - updated test scripts\n    - Fixed sqlx sql query placeholders\n- v0.9.10 (06/11/2020)\n    - Bug fix with retrieving varchar length from mysql\n    - Added support for mysql unsigned decimal - maps to float\n- v0.9.9 (06/11/2020)\n    - Fixed issue with mysql and table named `order`\n    - Fixed internals in GetAll generation in gorm and sqlx.\n- v0.9.8 (06/10/2020)\n    - Added ability to set file naming convention for models, dao, apis and grpc  `--file_naming={{.}}`\n    - Added ability to set struct naming convention `--model_naming={{.}}`\n    - Fixed bug with Makefile generation removing quoted conn string in `make regen`\n- v0.9.7 (06/09/2020)\n    - Added grpc server generation - WIP (looking for code improvements)\n    - Added ability to exclude tables\n    - Added support for unsigned from mysql ddl.\n- v0.9.6 (06/08/2020)\n    - Updated SQLX codegen\n    - Updated templates to split code gen functions into seperate files\n    - Added code_dao_gorm, code_dao_sqlx to be generated from templates\n- v0.9.5 (05/16/2020)\n    - Added SQLX codegen by default, split dao templates.\n    - Renamed templates\n- v0.9.4 (05/15/2020)\n    - Documentation updates, samples etc.\n- v0.9.3 (05/14/2020)\n    - Template bug fixes, when using custom api, dao and model package.\n    - Set primary key if not set to the first column\n    - Skip code gen if primary key column is not int or string\n    - validated codegen for mysql, mssql, postgres and sqlite3\n    - Fixed file naming if table ends with _test.go renames to _tst.go\n    - Fix for duplicate field names in struct due to renaming\n    - Added Notes for columns and tables for situations where a primary key is set since not defined in db\n    - Fixed issue when model contained field that had were named the same as funcs within model.\n- v0.9.2 (05/12/2020)\n    - Code cleanup gofmt, etc.\n- v0.9.1 (05/12/2020)\n- v0.9 (05/12/2020)\n    - updated db meta data loading fetching default values\n    - added default value to GORM tags\n    - Added protobuf .proto generation\n    - Added test app to display meta data\n    - Cleanup DDL generation\n    - Added support for varchar2, datetime2, float8, USER_DEFINED\n- v0.5\n"
       "ShortStructName"           string                         "i"
       "StructName"                string                         "Invoices"
       "SwaggerInfo"               *dbmeta.SwaggerInfoDetails     &dbmeta.SwaggerInfoDetails{Version:"1.0.0", Host:"127.0.0.1:8080", BasePath:"/", Title:"Sample CRUD api for main db", Description:"Sample CRUD api for main db", TOS:"My Custom TOS", ContactName:"", ContactURL:"", ContactEmail:""}
       "TableInfo"                 *dbmeta.ModelInfo              &dbmeta.ModelInfo{Index:0, IndexPlus1:1, PackageName:"model", StructName:"Invoices", ShortStructName:"i", TableName:"invoices", Fields:[]string{"//[ 0] InvoiceId                                      integer              null: false  primary: true   isArray: false  auto: true   col: integer         len: -1      default: []\n    InvoiceID int32 `gorm:\"primary_key;AUTO_INCREMENT;column:InvoiceId;type:integer;\" json:\"invoice_id\" xml:\"invoice_id\" db:\"InvoiceId\" protobuf:\"int32,0,opt,name=InvoiceId\"`", "//[ 1] CustomerId                                     integer              null: false  primary: false  isArray: false  auto: false  col: integer         len: -1      default: []\n    CustomerID int32 `gorm:\"column:CustomerId;type:integer;\" json:\"customer_id\" xml:\"customer_id\" db:\"CustomerId\" protobuf:\"int32,1,opt,name=CustomerId\"`", "//[ 2] InvoiceDate                                    datetime             null: false  primary: false  isArray: false  auto: false  col: datetime        len: -1      default: []\n    InvoiceDate time.Time `gorm:\"column:InvoiceDate;type:datetime;\" json:\"invoice_date\" xml:\"invoice_date\" db:\"InvoiceDate\" protobuf:\"google.protobuf.Timestamp,2,opt,name=InvoiceDate\"`", "//[ 3] BillingAddress                                 nvarchar(70)         null: true   primary: false  isArray: false  auto: false  col: nvarchar        len: 70      default: []\n    BillingAddress sql.NullString `gorm:\"column:BillingAddress;type:nvarchar;size:70;\" json:\"billing_address\" xml:\"billing_address\" db:\"BillingAddress\" protobuf:\"string,3,opt,name=BillingAddress\"`", "//[ 4] BillingCity                                    nvarchar(40)         null: true   primary: false  isArray: false  auto: false  col: nvarchar        len: 40      default: []\n    BillingCity sql.NullString `gorm:\"column:BillingCity;type:nvarchar;size:40;\" json:\"billing_city\" xml:\"billing_city\" db:\"BillingCity\" protobuf:\"string,4,opt,name=BillingCity\"`", "//[ 5] BillingState                                   nvarchar(40)         null: true   primary: false  isArray: false  auto: false  col: nvarchar        len: 40      default: []\n    BillingState sql.NullString `gorm:\"column:BillingState;type:nvarchar;size:40;\" json:\"billing_state\" xml:\"billing_state\" db:\"BillingState\" protobuf:\"string,5,opt,name=BillingState\"`", "//[ 6] BillingCountry                                 nvarchar(40)         null: true   primary: false  isArray: false  auto: false  col: nvarchar        len: 40      default: []\n    BillingCountry sql.NullString `gorm:\"column:BillingCountry;type:nvarchar;size:40;\" json:\"billing_country\" xml:\"billing_country\" db:\"BillingCountry\" protobuf:\"string,6,opt,name=BillingCountry\"`", "//[ 7] BillingPostalCode                              nvarchar(10)         null: true   primary: false  isArray: false  auto: false  col: nvarchar        len: 10      default: []\n    BillingPostalCode sql.NullString `gorm:\"column:BillingPostalCode;type:nvarchar;size:10;\" json:\"billing_postal_code\" xml:\"billing_postal_code\" db:\"BillingPostalCode\" protobuf:\"string,7,opt,name=BillingPostalCode\"`", "//[ 8] Total                                          numeric              null: false  primary: false  isArray: false  auto: false  col: numeric         len: -1      default: []\n    Total float64 `gorm:\"column:Total;type:numeric;\" json:\"total\" xml:\"total\" db:\"Total\" protobuf:\"float,8,opt,name=Total\"`"}, DBMeta:(*dbmeta.dbTableMeta)(0xc000133b00), Instance:(*struct { BillingState string "json:\"billing_state\""; BillingCountry string "json:\"billing_country\""; BillingPostalCode string "json:\"billing_postal_code\""; CustomerID int "json:\"customer_id\""; InvoiceDate time.Time "json:\"invoice_date\""; BillingAddress string "json:\"billing_address\""; BillingCity string "json:\"billing_city\""; Total float64 "json:\"total\""; InvoiceID int "json:\"invoice_id\"" })(0xc000a85700), CodeFields:[]*dbmeta.FieldInfo{(*dbmeta.FieldInfo)(0xc00025c640), (*dbmeta.FieldInfo)(0xc00025c780), (*dbmeta.FieldInfo)(0xc00025c8c0), (*dbmeta.FieldInfo)(0xc00025ca00), (*dbmeta.FieldInfo)(0xc00025cb40), (*dbmeta.FieldInfo)(0xc00025cc80), (*dbmeta.FieldInfo)(0xc00025cdc0), (*dbmeta.FieldInfo)(0xc00025cf00), (*dbmeta.FieldInfo)(0xc00025d040)}}
       "TableName"                 string                         "invoices"
       "apiFQPN"                   string                         "github.com/alexj212/test/api"
       "apiPackageName"            string                         "api"
       "daoFQPN"                   string                         "github.com/alexj212/test/dao"
       "daoPackageName"            string                         "dao"
       "delSql"                    string                         "DELETE FROM `invoices` where InvoiceId = ?"
       "insertSql"                 string                         "INSERT INTO `invoices` ( CustomerId,  InvoiceDate,  BillingAddress,  BillingCity,  BillingState,  BillingCountry,  BillingPostalCode,  Total) values ( ?, ?, ?, ?, ?, ?, ?, ? )"
       "modelFQPN"                 string                         "github.com/alexj212/test/model"
       "modelPackageName"          string                         "model"
       "module"                    string                         "github.com/alexj212/test"
       "outDir"                    string                         "."
       "selectMultiSql"            string                         "SELECT * FROM `invoices`"
       "selectOneSql"              string                         "SELECT * FROM `invoices` WHERE InvoiceId = ?"
       "serverHost"                string                         "127.0.0.1"
       "serverListen"              string                         ":8080"
       "serverPort"                int                            8080
       "serverScheme"              string                         "http"
       "sqlConnStr"                string                         "./example/sample.db"
       "sqlType"                   string                         "sqlite3"
       "tableInfos"                map[string]*dbmeta.ModelInfo   map[string]*dbmeta.ModelInfo{"invoices":(*dbmeta.ModelInfo)(0xc0001e94a0)}
       "updateSql"                 string                         "UPDATE `invoices` set CustomerId = ?, InvoiceDate = ?, BillingAddress = ?, BillingCity = ?, BillingState = ?, BillingCountry = ?, BillingPostalCode = ?, Total = ? WHERE InvoiceId = ?"
    
    
    

    Struct naming

    The ability exists to set a template that will be used for generating a struct name. By passing the flag --model_naming={{.}} The struct will be named the table name. Various functions can be used in the template to modify the name such as

    You can use the argument --name_test=user in conjunction with the --model_naming or --file_name, to view what the naming would be.

    Function Table Name Output
    singular Users User
    pluralize Users Users
    title Users Users
    toLower Users users
    toUpper Users USERS
    toLowerCamelCase Users users
    toUpperCamelCase Users Users
    toSnakeCase Users users

    Struct naming Examples

    Table Name: registration_source

    Model Naming Format Generated Struct Name
    {{.}} registration_source
    Struct{{.}} Structregistration_source
    Struct{{ singular .}} Structregistration_source
    Struct{{ toLowerCamelCase .}} Structregistration_source
    Struct{{ toUpperCamelCase .}} StructRegistration_source
    Struct{{ toSnakeCase .}} Structregistration_source
    Struct{{ toLowerCamelCase .}} Structtable_registration_source
    Struct{{ toUpperCamelCase .}} StructTable_registration_source
    Struct{{ toSnakeCase .}} Structtable_registration_source
    Struct{{ toSnakeCase ( replace . "table_" "") }} Structregistration_source

    Notes

    • MySql, Mssql, Postgres and Sqlite have a database metadata fetcher that will query the db, and update the auto increment, primary key and nullable info for the gorm annotation.
    • Tables that have a non-standard primary key (NON integer based or String) the table will be ignored.

    DB Meta Data Loading

    DB Type Nullable Primary Key Auto Increment Column Len default Value create ddl
    sqlite y y y y y y y
    postgres y y y y y y n
    mysql y y y y y y y
    ms sql y y y y y y n

    Version History

    • v0.9.27 (08/04/2020)
      • Updated '--exec' mode to provide various functions for processing
      • copy function updated to provide --include and --exclude patterns. Patterns are processed in order, an include preceeding an exclude will take precedence. Multiple include and excludes can be specified. Files ending with .table.tmpl will be processed for each table. Output filenames will be stored in the proper directory, with a name of the table with the suffix of the template extension. Files ending with .tmpl will be processed as a template and the filename will be the name of the template stripped with the .tmpl suffix.
      • When processing templates, files generated with a .go extension will be formatted with the go fmt.
    • v0.9.26 (07/31/2020)
      • Release scripting
      • Added custom script functions to copy, mkdir, touch, pwd
      • Fixed custom script exec example
    • v0.9.25 (07/26/2020)
      • Adhere json-fmt flag for all JSON response so when camel or lower_camel is specified, fields name in GetAll variant and DDL info will also have the same name format
      • Fix: Build information embedded through linker in Makefile is not consistent with the variable defined in main file.
      • Added --scheme and --listen options. This allows compiled binary to be used behind reverse proxy.
      • In addition, template for generating URL was fixed, i.e. when PORT is 80, then PORT is omitted from URL segment.
    • v0.9.24 (07/13/2020)
      • Fixed array bounds issue parsing mysql db meta
    • v0.9.23 (07/10/2020)
      • Added postgres types: bigserial, serial, smallserial, bigserial, float4 to mapping.json
    • v0.9.22 (07/08/2020)
      • Modified gogo.proto check to use GOPATH not hardcoded.
      • Updated gen to error exit on first error encountered
      • Added color output for error
      • Added --no-color option for non colorized output
    • v0.9.21 (07/07/2020)
      • Repacking templates, update version number in info.
    • v0.9.20 (07/07/2020)
      • Fixed render error in router.go.tmpl
      • upgraded project to use go.mod 1.14
    • v0.9.19 (07/07/2020)
      • Added --windows flag to write files with CRLF windows line endings, otherwise they are all unix based LF line endings
    • v0.9.18 (06/30/2020)
      • Fixed naming in templates away from hard coded model package.
    • v0.9.17 (06/30/2020)
      • Refactored template loading, to better report error in template
      • Added option to run gofmt on output directory
    • v0.9.16 (06/29/2020)
      • Fixes to router.go.tmpl from calvinchengx
      • Added postgres db support for inet and timestamptz
    • v0.9.15 (06/23/2020)
      • Code cleanup using gofmt name suggestions.
      • Template updates for generated code cleanup using gofmt name suggestions.
    • v0.9.14 (06/23/2020)
      • Added model comment on field line if available from database.
      • Added exposing TableInfo via api call.
    • v0.9.13 (06/22/2020)
      • fixed closing of connections via defer
      • bug fixes in sqlx generated code
    • v0.9.12 (06/14/2020)
      • SQLX changed MustExec to Exec and checking/returning error
      • Updated field renaming if duplicated, need more elegant renaming solution.
      • Added exclude to test.sh
    • v0.9.11 (06/13/2020)
      • Added ability to pass field, model and file naming format
      • updated test scripts
      • Fixed sqlx sql query placeholders
    • v0.9.10 (06/11/2020)
      • Bug fix with retrieving varchar length from mysql
      • Added support for mysql unsigned decimal - maps to float
    • v0.9.9 (06/11/2020)
      • Fixed issue with mysql and table named order
      • Fixed internals in GetAll generation in gorm and sqlx.
    • v0.9.8 (06/10/2020)
      • Added ability to set file naming convention for models, dao, apis and grpc --file_naming={{.}}
      • Added ability to set struct naming convention --model_naming={{.}}
      • Fixed bug with Makefile generation removing quoted conn string in make regen
    • v0.9.7 (06/09/2020)
      • Added grpc server generation - WIP (looking for code improvements)
      • Added ability to exclude tables
      • Added support for unsigned from mysql ddl.
    • v0.9.6 (06/08/2020)
      • Updated SQLX codegen
      • Updated templates to split code gen functions into seperate files
      • Added code_dao_gorm, code_dao_sqlx to be generated from templates
    • v0.9.5 (05/16/2020)
      • Added SQLX codegen by default, split dao templates.
      • Renamed templates
    • v0.9.4 (05/15/2020)
      • Documentation updates, samples etc.
    • v0.9.3 (05/14/2020)
      • Template bug fixes, when using custom api, dao and model package.
      • Set primary key if not set to the first column
      • Skip code gen if primary key column is not int or string
      • validated codegen for mysql, mssql, postgres and sqlite3
      • Fixed file naming if table ends with _test.go renames to _tst.go
      • Fix for duplicate field names in struct due to renaming
      • Added Notes for columns and tables for situations where a primary key is set since not defined in db
      • Fixed issue when model contained field that had were named the same as funcs within model.
    • v0.9.2 (05/12/2020)
      • Code cleanup gofmt, etc.
    • v0.9.1 (05/12/2020)
    • v0.9 (05/12/2020)
      • updated db meta data loading fetching default values
      • added default value to GORM tags
      • Added protobuf .proto generation
      • Added test app to display meta data
      • Cleanup DDL generation
      • Added support for varchar2, datetime2, float8, USER_DEFINED
    • v0.5

    Contributors

    • alexj212 - a big thanks to alexj212 for his contributions

    See more contributors: contributors

Owner
smallnest
Author of 《Scala Collections Cookbook》
smallnest
Comments
  • Error in rendering internal://router.go.tmpl

    Error in rendering internal://router.go.tmpl

    Error in rendering internal://router.go.tmpl: template: router.go.tmpl:347:22: executing "router.go.tmpl" at <.modelPackageName>: can't evaluate field modelPackageName in type *dbmeta.ModelInfo

    how to fix this error?

  • generated struct has no fields

    generated struct has no fields

    `package model

    import ( "database/sql" "time" "github.com/guregu/null" )

    var ( _ = time.Second _ = sql.LevelDefault _ = null.Bool{} )

    type UserAliinfo struct { }

    // TableName sets the insert table name for this struct type func (u *UserAliinfo) TableName() string { return "user_aliinfo" } `

  • Gorm Optional Struct tags

    Gorm Optional Struct tags

    Does this package support gorm optional structs tags such as Size, PRIMARY_KEY, AUTO_INCREMENT? https://gorm.io/docs/models.html#Supported-Struct-tags

    type Batch struct {
    	ID          int            `gorm:"column:id;primary_key" json:"id"`
    	Title       string         `gorm:"column:title" json:"title"`
    	Description sql.NullString `gorm:"column:description" json:"description"`
    	UrlsCount   int            `gorm:"column:urls_count" json:"urls_count"`
    	CreatedAt   time.Time      `gorm:"column:created_at" json:"created_at"`
    	UpdatedAt   time.Time      `gorm:"column:updated_at" json:"updated_at"`
    }
    

    above struct was created with following command but no optional struct tags gen --connstr "USER:PASSWORD@tcp(127.0.0.1:3306)/dbname" --database dbname --table batches --gorm --json

  • undefined schema when using go get to install the lib

    undefined schema when using go get to install the lib

    ~ % go get -u github.com/smallnest/gen

    github.com/smallnest/gen/dbmeta

    go/src/github.com/smallnest/gen/dbmeta/meta_mssql.go:19:15: undefined: schema.Table go/src/github.com/smallnest/gen/dbmeta/meta_mysql.go:20:15: undefined: schema.Table go/src/github.com/smallnest/gen/dbmeta/meta_postgres.go:18:15: undefined: schema.Table go/src/github.com/smallnest/gen/dbmeta/meta_sqlite.go:38:15: undefined: schema.Table go/src/github.com/smallnest/gen/dbmeta/meta_unknown.go:21:15: undefined: schema.Table

  • doesn't generate model properties

    doesn't generate model properties

    All generated models have no properties generated

    $ go version
    go version go1.12.7 darwin/amd64
    
    gen --connstr "root:root@tcp(127.0.0.1:3306)/abc?&parseTime=True" --database abc  --json --gorm --guregu --rest
    
    package model
    
    import (
            "database/sql"
            "time"
            "github.com/guregu/null"
    )
    
    var (
            _ = time.Second
            _ = sql.LevelDefault
            _ = null.Bool{}
    )
    
    type Transaction struct {
    }
    
    // TableName sets the insert table name for this struct type
    func (t *Transaction) TableName() string {
            return "transaction"
    }
    
  • when generate dao function. does not support composite primary key

    when generate dao function. does not support composite primary key

    model is as list

    DROP TABLE IF EXISTS `abcd_apt`;
    CREATE TABLE `abcd_apt` (
      `id` bigint(40) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `version` varchar(100) NOT NULL,
      `owner` varchar(100) NOT NULL,
      `desc` varchar(500) NOT NULL,
      PRIMARY KEY (`name`,`version`),
      UNIQUE KEY `id` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    gorm is listed as below // AbcdApt struct is a row record of the abcd_apt table in the bytesib database type AbcdApt struct { //[ 0] id bigint null: false primary: false isArray: false auto: true col: bigint len: -1 default: [] ID int64 gorm:"AUTO_INCREMENT;column:id;type:bigint;" //[ 1] name varchar(100) null: false primary: true isArray: false auto: false col: varchar len: 100 default: [] Name string gorm:"primary_key;column:name;type:varchar;size:100;" //[ 2] version varchar(100) null: false primary: false isArray: false auto: false col: varchar len: 100 default: [] Version string gorm:"column:version;type:varchar;size:100;" //[ 3] owner varchar(100) null: false primary: false isArray: false auto: false col: varchar len: 100 default: [] Owner string gorm:"column:owner;type:varchar;size:100;" //[ 4] desc varchar(500) null: false primary: false isArray: false auto: false col: varchar len: 500 default: [] Desc string gorm:"column:desc;type:varchar;size:500;" }

  • Issue with make example

    Issue with make example

    Having run make tools; make build; make install I tried make example. I am seeing an issue with: github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with go get -u github.com/gogo/protobuf/proto

    writing Makefile writing main.proto github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with go get -u github.com/gogo/protobuf/proto

    Error compiling proto file github.com/gogo/protobuf/gogoproto/gogo.proto does not exist Error in executing generate github.com/gogo/protobuf/gogoproto/gogo.proto does not exist exit status 1

    I had similar problems following the instructions for a quick start, before trying to build gen myself.

    I noticed a couple more errors logged: Error calling func: github.com/smallnest/gen/dbmeta.LoadSqliteMeta error: unsupported table: sqlite_sequence LoadMeta Error getting table info for sqlite_sequence error: unsupported table: sqlite_sequence

    Full log: stevef@Stevens-MacBook-Pro-3 gen % gen --version v0.9.16 (06/29/2020) stevef@Stevens-MacBook-Pro-3 gen % make example rm -rf ./example/Makefile
    ./example/README.md
    ./example/api
    ./example/app
    ./example/bin
    ./example/dao
    ./example/docs
    ./example/go.mod
    ./example/go.sum
    ./example/model
    ./example/.gitignore
    ./tests ls -latr ./example total 1736 drwxr-xr-x 3 stevef staff 96 30 Jun 16:23 . -rw-r--r-- 1 stevef staff 887808 30 Jun 16:23 sample.db drwxr-xr-x 26 stevef staff 832 30 Jun 16:25 .. cd ./example && go run ..
    --sqltype=sqlite3
    --connstr "./sample.db"
    --database main
    --module github.com/alexj212/generated
    --verbose
    --overwrite
    --out ./
    --templateDir=../template
    --json
    --db
    --generate-dao
    --generate-proj
    --protobuf
    --gorm
    --guregu
    --rest
    --mod
    --server
    --makefile
    --copy-templates [0] [GEN_README.md.tmpl] [1] [Makefile.tmpl] [2] [README.md.tmpl] [3] [api.go.tmpl] [4] [api_add.go.tmpl] [5] [api_delete.go.tmpl] [6] [api_get.go.tmpl] [7] [api_getall.go.tmpl] [8] [api_update.go.tmpl] [9] [code_dao_gorm.md.tmpl] [10] [code_dao_sqlx.md.tmpl] [11] [code_http.md.tmpl] [12] [dao_gorm.go.tmpl] [13] [dao_gorm_add.go.tmpl] [14] [dao_gorm_delete.go.tmpl] [15] [dao_gorm_get.go.tmpl] [16] [dao_gorm_getall.go.tmpl] [17] [dao_gorm_init.go.tmpl] [18] [dao_gorm_update.go.tmpl] [19] [dao_sqlx.go.tmpl] [20] [dao_sqlx_add.go.tmpl] [21] [dao_sqlx_delete.go.tmpl] [22] [dao_sqlx_get.go.tmpl] [23] [dao_sqlx_getall.go.tmpl] [24] [dao_sqlx_init.go.tmpl] [25] [dao_sqlx_update.go.tmpl] [26] [debug.txt] [27] [gitignore.tmpl] [28] [gomod.tmpl] [29] [http_utils.go.tmpl] [30] [main_gorm.go.tmpl] [31] [main_sqlx.go.tmpl] [32] [mapping.json] [33] [model.go.tmpl] [34] [model_base.go.tmpl] [35] [protobuf.tmpl] [36] [protomain.go.tmpl] [37] [protoserver.go.tmpl] [38] [router.go.tmpl] Loaded 58 mappings from: internal Mapping:[ 0] -> bit Mapping:[ 1] -> bool Mapping:[ 2] -> tinyint Mapping:[ 3] -> int Mapping:[ 4] -> smallint Mapping:[ 5] -> mediumint Mapping:[ 6] -> int4 Mapping:[ 7] -> int2 Mapping:[ 8] -> integer Mapping:[ 9] -> bigint Mapping:[10] -> int8 Mapping:[11] -> char Mapping:[12] -> enum Mapping:[13] -> varchar Mapping:[14] -> nvarchar Mapping:[15] -> longtext Mapping:[16] -> mediumtext Mapping:[17] -> text Mapping:[18] -> tinytext Mapping:[19] -> varchar2 Mapping:[20] -> inet Mapping:[21] -> json Mapping:[22] -> jsonb Mapping:[23] -> nchar Mapping:[24] -> date Mapping:[25] -> datetime2 Mapping:[26] -> datetime Mapping:[27] -> time Mapping:[28] -> timestamp Mapping:[29] -> timestamptz Mapping:[30] -> smalldatetime Mapping:[31] -> decimal Mapping:[32] -> double Mapping:[33] -> money Mapping:[34] -> real Mapping:[35] -> float Mapping:[36] -> float8 Mapping:[37] -> binary Mapping:[38] -> blob Mapping:[39] -> longblob Mapping:[40] -> mediumblob Mapping:[41] -> varbinary Mapping:[42] -> numeric Mapping:[43] -> bpchar Mapping:[44] -> tsvector Mapping:[45] -> _text Mapping:[46] -> bytea Mapping:[47] -> USER_DEFINED Mapping:[48] -> uuid Mapping:[49] -> Mapping:[50] -> utinyint Mapping:[51] -> usmallint Mapping:[52] -> umediumint Mapping:[53] -> uint Mapping:[54] -> ubigint Mapping:[55] -> udecimal Mapping:[56] -> udouble Mapping:[57] -> ufloat tableName: albums [ 0] AlbumId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Title nvarchar(160) null: false primary: false isArray: false auto: false col: nvarchar len: 160 default: [] [ 2] ArtistId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] Error calling func: github.com/smallnest/gen/dbmeta.LoadSqliteMeta error: unsupported table: sqlite_sequence LoadMeta Error getting table info for sqlite_sequence error: unsupported table: sqlite_sequence tableName: artists [ 0] ArtistId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Name nvarchar(120) null: true primary: false isArray: false auto: false col: nvarchar len: 120 default: [] tableName: customers [ 0] CustomerId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] FirstName nvarchar(40) null: false primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 2] LastName nvarchar(20) null: false primary: false isArray: false auto: false col: nvarchar len: 20 default: [] [ 3] Company nvarchar(80) null: true primary: false isArray: false auto: false col: nvarchar len: 80 default: [] [ 4] Address nvarchar(70) null: true primary: false isArray: false auto: false col: nvarchar len: 70 default: [] [ 5] City nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 6] State nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 7] Country nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 8] PostalCode nvarchar(10) null: true primary: false isArray: false auto: false col: nvarchar len: 10 default: [] [ 9] Phone nvarchar(24) null: true primary: false isArray: false auto: false col: nvarchar len: 24 default: [] [10] Fax nvarchar(24) null: true primary: false isArray: false auto: false col: nvarchar len: 24 default: [] [11] Email nvarchar(60) null: false primary: false isArray: false auto: false col: nvarchar len: 60 default: [] [12] SupportRepId integer null: true primary: false isArray: false auto: false col: integer len: -1 default: [] tableName: employees [ 0] EmployeeId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] LastName nvarchar(20) null: false primary: false isArray: false auto: false col: nvarchar len: 20 default: [] [ 2] FirstName nvarchar(20) null: false primary: false isArray: false auto: false col: nvarchar len: 20 default: [] [ 3] Title nvarchar(30) null: true primary: false isArray: false auto: false col: nvarchar len: 30 default: [] [ 4] ReportsTo integer null: true primary: false isArray: false auto: false col: integer len: -1 default: [] [ 5] BirthDate datetime null: true primary: false isArray: false auto: false col: datetime len: -1 default: [] [ 6] HireDate datetime null: true primary: false isArray: false auto: false col: datetime len: -1 default: [] [ 7] Address nvarchar(70) null: true primary: false isArray: false auto: false col: nvarchar len: 70 default: [] [ 8] City nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 9] State nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [10] Country nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [11] PostalCode nvarchar(10) null: true primary: false isArray: false auto: false col: nvarchar len: 10 default: [] [12] Phone nvarchar(24) null: true primary: false isArray: false auto: false col: nvarchar len: 24 default: [] [13] Fax nvarchar(24) null: true primary: false isArray: false auto: false col: nvarchar len: 24 default: [] [14] Email nvarchar(60) null: true primary: false isArray: false auto: false col: nvarchar len: 60 default: [] tableName: genres [ 0] GenreId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Name nvarchar(120) null: true primary: false isArray: false auto: false col: nvarchar len: 120 default: [] tableName: invoices [ 0] InvoiceId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] CustomerId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 2] InvoiceDate datetime null: false primary: false isArray: false auto: false col: datetime len: -1 default: [] [ 3] BillingAddress nvarchar(70) null: true primary: false isArray: false auto: false col: nvarchar len: 70 default: [] [ 4] BillingCity nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 5] BillingState nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 6] BillingCountry nvarchar(40) null: true primary: false isArray: false auto: false col: nvarchar len: 40 default: [] [ 7] BillingPostalCode nvarchar(10) null: true primary: false isArray: false auto: false col: nvarchar len: 10 default: [] [ 8] Total numeric null: false primary: false isArray: false auto: false col: numeric len: -1 default: [] tableName: invoice_items [ 0] InvoiceLineId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] InvoiceId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 2] TrackId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 3] UnitPrice numeric null: false primary: false isArray: false auto: false col: numeric len: -1 default: [] [ 4] Quantity integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] tableName: media_types [ 0] MediaTypeId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Name nvarchar(120) null: true primary: false isArray: false auto: false col: nvarchar len: 120 default: [] tableName: playlists [ 0] PlaylistId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Name nvarchar(120) null: true primary: false isArray: false auto: false col: nvarchar len: 120 default: [] tableName: playlist_track [ 0] PlaylistId integer null: false primary: true isArray: false auto: false col: integer len: -1 default: [] [ 1] TrackId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] tableName: tracks [ 0] TrackId integer null: false primary: true isArray: false auto: true col: integer len: -1 default: [] [ 1] Name nvarchar(200) null: false primary: false isArray: false auto: false col: nvarchar len: 200 default: [] [ 2] AlbumId integer null: true primary: false isArray: false auto: false col: integer len: -1 default: [] [ 3] MediaTypeId integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 4] GenreId integer null: true primary: false isArray: false auto: false col: integer len: -1 default: [] [ 5] Composer nvarchar(220) null: true primary: false isArray: false auto: false col: nvarchar len: 220 default: [] [ 6] Milliseconds integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 7] Bytes integer null: true primary: false isArray: false auto: false col: integer len: -1 default: [] [ 8] UnitPrice numeric null: false primary: false isArray: false auto: false col: numeric len: -1 default: [] Error calling func: github.com/smallnest/gen/dbmeta.LoadSqliteMeta error: unsupported table: sqlite_stat1 LoadMeta Error getting table info for sqlite_stat1 error: unsupported table: sqlite_stat1 tableName: purchase_order [ 0] id integer null: false primary: true isArray: false auto: false col: integer len: -1 default: [] [ 1] payment_id integer null: false primary: false isArray: false auto: false col: integer len: -1 default: [] [ 2] full_name text null: false primary: false isArray: false auto: false col: text len: -1 default: [] Generating code for the following tables (12) [0] customers [1] invoice_items [2] media_types [3] invoices [4] playlists [5] playlist_track [6] tracks [7] albums [8] artists [9] employees [10] genres [11] purchase_order writing model/customers.go writing api/customers.go writing dao/customers.go writing model/invoice_items.go writing api/invoice_items.go writing dao/invoice_items.go writing model/media_types.go writing api/media_types.go writing dao/media_types.go writing model/playlist_track.go writing api/playlist_track.go writing dao/playlist_track.go writing model/tracks.go writing api/tracks.go writing dao/tracks.go writing model/albums.go writing api/albums.go writing dao/albums.go writing model/artists.go writing api/artists.go writing dao/artists.go writing model/employees.go writing api/employees.go writing dao/employees.go writing model/genres.go writing api/genres.go writing dao/genres.go writing model/invoices.go writing api/invoices.go writing dao/invoices.go writing model/playlists.go writing api/playlists.go writing dao/playlists.go writing model/purchase_order.go writing api/purchase_order.go writing dao/purchase_order.go writing api/router.go writing api/http_utils.go writing dao/dao_base.go writing model/model_base.go writing go.mod github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with go get -u github.com/gogo/protobuf/proto

    writing Makefile writing main.proto github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with go get -u github.com/gogo/protobuf/proto

    Error compiling proto file github.com/gogo/protobuf/gogoproto/gogo.proto does not exist Error in executing generate github.com/gogo/protobuf/gogoproto/gogo.proto does not exist exit status 1 make: *** [generate_example] Error 1 stevef@Stevens-MacBook-Pro-3 gen %

  • mysql cann't generate go model struct

    mysql cann't generate go model struct

    in dbmeta/meta.go GenerateStruct function line101, when the tablename is user , the sql string is SELECT * FROM user LIMIT 0 . that is a error sql query string.

  • If table name is order will case gen fail

    If table name is order will case gen fail

    if table name is 'order', the struct generated wiill be empty:

    package model
    
    import (
    	"database/sql"
    	"time"
    
    	"github.com/guregu/null"
    )
    
    var (
    	_ = time.Second
    	_ = sql.LevelDefault
    	_ = null.Bool{}
    )
    
    type Order struct {
    }
    
    // TableName sets the insert table name for this struct type
    func (o *Order) TableName() string {
    	return "order"
    }
    

    gen param isgen --connstr 'xxxx' --table order --gorm --json

    when I change table to order2, bug not show again

  • default dao 'getall' with 'paging' writes the sql and ALSO passes as parameters

    default dao 'getall' with 'paging' writes the sql and ALSO passes as parameters

    default dao 'getall' with 'paging' writes the sql and ALSO passes as parameters

    this causes: sql: expected 0 arguments, got 2 errors

    err = DB.SelectContext(ctx, &structs, sql, page, pagesize) should not pass page and pagesize, as the fmt.Sprintf is actually embedding the parameters in the actual query string

  • why the struct don't have member?

    why the struct don't have member?

    gen -c 'mitra:shopee123!@tcp(10.12.77.233)/apc_report_db?&parseTime=True' --database apc_report_db -t dp_order_report_tab --package dbreport --json --gorm --guregu

    package model

    import ( "database/sql" "time"

    "github.com/guregu/null"
    

    )

    var ( _ = time.Second _ = sql.LevelDefault _ = null.Bool{} )

    type DpOrderReportTab struct { }

    // TableName sets the insert table name for this struct type func (d *DpOrderReportTab) TableName() string { return "dp_order_report_tab" }

  • Unused imports added to models causes compilation to fail

    Unused imports added to models causes compilation to fail

    Library version master@bac0f1d.

    Outputs incorrect version number:

    $ gen --version
    v0.9.27 (08/04/2020)
    

    Generates models with unused imports:

    import (
    	"database/sql" // unused
    	"time"
    
    	"github.com/satori/go.uuid" // unused
    
    	"gorm.io/gorm"
    )
    

    Causes compilation to fail:

    # <redacted>/model
    <redacted>.go:4:2: imported and not used: "database/sql"
    <redacted>.go:5:2: imported and not used: "time"
    <redacted>.go:7:2: imported and not used: "github.com/satori/go.uuid" as uuid
    <redacted>.go:4:2: imported and not used: "database/sql"
    <redacted>.go:5:2: imported and not used: "time"
    <redacted>.go:7:2: imported and not used: "github.com/satori/go.uuid" as uuid
    <redacted>.go:4:2: imported and not used: "database/sql"
    <redacted>.go:5:2: imported and not used: "time"
    <redacted>.go:7:2: imported and not used: "github.com/satori/go.uuid" as uuid
    <redacted>.go:4:2: imported and not used: "database/sql"
    <redacted>.go:4:2: too many errors
    
    Compilation finished with exit code 2
    

    Goland complains, but allows me to optimise the imports for all the models, fixing the issue: Screenshot from 2022-06-04 12-18-35

  • Create new release

    Create new release

    go install installs the latest release v0.9.27 (08/04/2020). Many improvements have been made since then.

    Please create a new release which includes the improvements.

  • check database name when collect schemas

    check database name when collect schemas

    When collecting table schemas, we must check selectel database name to avoid conflicts with multiple cloned databases. For example, you can create database foo, clone it to foo_dev, set the same login/password to both databases and then try to generate structs, after code generation you can see conflicts.

  • Junk databases tables schemas

    Junk databases tables schemas

    Hi! When I run utility, I will set up --database flag with database name, same from connStr But, on main.go line 229 you will check database name, otherwise too many schemas collected in the dbTables variable, and when I have some copies of my database (application, application_dev, application_test), names of tables from this databases has conflicts. See my change:

    	var dbTables []string
    	// parse or read tables
    	if *sqlTable != "" {
    		dbTables = strings.Split(*sqlTable, ",")
    	} else {
    		schemaTables, err := schema.TableNames(db)
    		if err != nil {
    			fmt.Print(au.Red(fmt.Sprintf("Error in fetching tables information from %s information schema from %s\n", *sqlType, *sqlConnStr)))
    			os.Exit(1)
    			return
    		}
    		for _, st := range schemaTables {
    			//We need only one database schema <<<
    			if st[0] != *sqlDatabase {
    				continue
    			}
    			dbTables = append(dbTables, st[1]) // s[0] == sqlDatabase
    		}
    	}
    
  • Add a hook function after db operation inside http handlers

    Add a hook function after db operation inside http handlers

    hey, there. I wonder if we counld add a post hook function inside http handlers. We can modify fields here. Here's an example.

    func GetAllSystemRole_(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
            // ..... some  code
    
    	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
            if err := systemrole_.AfterDBOperation(); err != nil {.  // add hook here
    		returnError(ctx, w, r, dao.ErrServerInternal)
    	}
    	writeJSON(ctx, w, result)
    }
    
Api-project - Api project with Golang, Gorm, Gorilla-Mux, Postgresql

TECHNOLOGIES GOLANG 1.14 GORM GORILLA-MUX POSTGRESQL API's PATHS For Product Ser

Nov 23, 2022
A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker.

golang-test-api A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker. Cloning the repository To clone the repository run the following com

Dec 14, 2022
Users - Restful API that can get/create/update/delete user data from persistence storage

Users Service The users service is used to maintain Restful API that can get/cre

Feb 15, 2022
A simple wrapper around sql.DB to help with structs. Not quite an ORM.

go-modeldb A simple wrapper around sql.DB to help with structs. Not quite an ORM. Philosophy: Don't make an ORM Example: // Setup require "modeldb" db

Nov 16, 2019
100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood.

go-queryset 100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. Contents

Dec 30, 2022
A example of a join table using liquibase and gorm

A example of a join table using liquibase and gorm. Additionally the join table's composite key is used as a composite foreign key for another table.

Feb 4, 2022
EZCoin is a control panel for Bitfinex funding, backend is build by Golang, Gin and GORM, frontend is build by angular

EZCoin server is backend for Bitfinex funding, it build by Golang, Gin and GORM.

Feb 7, 2022
A pure golang SQL database for learning database theory

Go SQL DB 中文 "Go SQL DB" is a relational database that supports SQL queries for research purposes. The main goal is to show the basic principles and k

Dec 29, 2022
opentracing integration with GORM
opentracing integration with GORM

gorm-opentracing opentracing support for gorm2. Features Record SQL in span logs. Record Result in span logs. Record Table in span tags. Record Error

Nov 27, 2022
A code generator base on GORM

GORM/GEN The code generator base on GORM, aims to be developer friendly. Overview CRUD or DIY query method code generation Auto migration from databas

Jan 1, 2023
Scope function for GORM queries provides easy filtering with query parameters

Gin GORM filter Scope function for GORM queries provides easy filtering with query parameters Usage go get github.com/ActiveChooN/gin-gorm-filter Mod

Dec 28, 2022
A plugin to allow telemetry by NewRelic Go Agent for GORM

GORM NewRelic Telemetry Plugin A plugin to allow telemetry by NewRelic Go Agent for GORM Overview Plugin implementation to add datastore segments on a

Aug 19, 2022
Gorm firebird driver

gorm-firebird GORM firebird driver import: "github.com/flylink888/gorm-firebird" Example: var products []Product dsn := "SYSDBA:[email protected]/sy

Sep 27, 2022
OpenTelemetry plugin for GORM v2

gorm-opentelemetry OpenTelemetry plugin for GORM v2 Traces all queries along with the query SQL. Usage Example: // Copyright The OpenTelemetry Authors

Jan 11, 2022
CURD using go fiber - gorm - mysql

GO Fiber - CRUD - GORM - Mysql Folder Structure - database | database config |- migration | migration config - middleware | mid

Nov 13, 2022
Fiber Clean Architecture With GORM

Fiber Clean Architecture With GORM I offer this repository as a proposal for a c

Oct 30, 2022
Ormtool - 将数据库表转换为golang的结构体,自定义生成tag,如json,gorm,xorm和简单的db信息

数据库表转换为golang 结构体 1. 获取方式 2. 配置说明 # 保存路径 SavePath: "./models/test.go", # 是

May 30, 2022
Simple-crm-system - Simple CRM system CRUD backend using Go, Fiber, SQLite, Gorm

Simple CRM system CRUD backend using GO, Fiber, Gorm, SQLite Developent go mod t

Nov 13, 2022
This library is a complementary library for Gorm (v2) which resolves the first available pool passed to it.

This library is a complementary library for Gorm (v2) which resolves the first available pool passed to it.

Feb 2, 2022