Gen Tool

Gen Toolは、依存関係のない単一のバイナリで、データベースから構造体を生成するために使用できます。

インストール

go install gorm.io/gen/tools/gentool@latest

使い方

gentool -h

Usage of gentool:
-c string
config file path
-db string
input mysql or postgres or sqlite or sqlserver. consult[https://gorm.dokyumento.jp/docs/connecting_to_the_database.html] (default "mysql")
-dsn string
consult[https://gorm.dokyumento.jp/docs/connecting_to_the_database.html]
-fieldNullable
generate with pointer when field is nullable
-fieldWithIndexTag
generate field with gorm index tag
-fieldWithTypeTag
generate field with gorm column type tag
-modelPkgName string
generated model code's package name
-outFile string
query code file name, default: gen.go
-outPath string
specify a directory for output (default "./dao/query")
-tables string
enter the required data table or leave it blank
-onlyModel
only generate models (without query file)
-withUnitTest
generate unit test for query code
-fieldSignable
detect integer field's unsigned type, adjust generated data type

c

設定ファイル名、デフォルト値は""、コマンドラインオプションは設定ファイルよりも優先度が高くなります。

db

ドライバーダイヤレクトを指定します。デフォルト値は "mysql" です。参照: https://gorm.dokyumento.jp/docs/connecting_to_the_database.html

dsn

データベースへの接続に使用されるDSNです。参照: https://gorm.dokyumento.jp/docs/connecting_to_the_database.html

fieldNullable

フィールドがnullableの場合、ポインタで生成します。

fieldWithIndexTag

gormのindexタグ付きでフィールドを生成します。

fieldWithTypeTag

gormのcolumn typeタグ付きでフィールドを生成します。

modelPkgName

生成されたモデルコードのパッケージ名。

outFile

生成されたクエリコードのファイル名、デフォルト: gen.go

outPath

出力先のディレクトリを指定します(デフォルト: "./dao/query")

tables

生成するテーブルを指定します。デフォルトはすべてのテーブルです。

--tables="orders"       # generate from `orders`

--tables="orders,users" # generate from `orders` and `users`

--tables=""             # generate from all tables

いくつかのテーブルコードを生成します。

withUnitTest

単体テストを生成します。デフォルト値はfalse、オプション: false / true

fieldSignable

符号付きデータ型をフィールド型として使用します。デフォルト値はfalse、オプション: false / true

gentool -dsn "user:pwd@tcp(localhost:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "orders,doctor"

gentool -c "./gen.tool"
version: "0.1"
database:
# consult[https://gorm.dokyumento.jp/docs/connecting_to_the_database.html]"
dsn : "username:password@tcp(address:port)/db?charset=utf8mb4&parseTime=true&loc=Local"
# input mysql or postgres or sqlite or sqlserver. consult[https://gorm.dokyumento.jp/docs/connecting_to_the_database.html]
db : "mysql"
# enter the required data table or leave it blank.You can input : orders,users,goods
tables : "user"
# specify a directory for output
outPath : "./dao/query"
# query code file name, default: gen.go
outFile : ""
# generate unit test for query code
withUnitTest : false
# generated model code's package name
modelPkgName : ""
# generate with pointer when field is nullable
fieldNullable : false
# generate field with gorm index tag
fieldWithIndexTag : false
# generate field with gorm column type tag
fieldWithTypeTag : false

プラチナスポンサー

ゴールドスポンサー

プラチナスポンサー

ゴールドスポンサー