// TableName table name with gorm NamingStrategyfunc(m CommonMethod) TableName(namer schema.Namer) string {if namer == nil {return"@@table" }return namer.TableName("@@table")}
// DIY TableName method for the generated User structg.GenerateModel("user", gen.WithMethod(CommonMethod{}.TableName))
// DIY TableName method for the generated all structg.WithOpts(gen.WithMethod(CommonMethod{}.TableName))
// Set Default DIY TableName method for the generated all structg.WithOpts(gen.WithMethod(gen.DefaultMethodTableWithNamer))
Field Options
Following are options that can be used during GenerateModel/GenerateModelAs
FieldNew // create new a fieldFieldIgnore // ignore fieldFieldIgnoreReg // ignore field (match with regexp)FieldRename // rename field in the structFieldComment // specify field comment in generated structFieldType // specify the field typeFieldTypeReg // specify field type (match with regexp)FieldGenType // specify field gen typeFieldGenTypeReg // specify field gen type (match with regexp)FieldTag // specify gorm and json tagFieldJSONTag // specify json tagFieldJSONTagWithNS // specify json tag with name strategyFieldGORMTag // specify gorm tagFieldNewTag // append new tagFieldNewTagWithNS // specify the new tag with name strategyFieldTrimPrefix // trim column prefixFieldTrimSuffix // trim column suffixFieldAddPrefix // add the prefix to struct field's nameFieldAddSuffix // add the suffix to struct field's nameFieldRelate // specify relationship with other tablesFieldRelateModel // specify the relationship with existing models
全局生成选项
Gen 有一些全局选项可以在 gen.Config中设置:
g := gen.NewGenerator(gen.Config{// if you want the nullable field generation property to be pointer type, set FieldNullable true FieldNullable: true,// if you want to assign field which has a default value in the `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values FieldCoverable: true,// if you want to generate field with unsigned integer type, set FieldSignable true FieldSignable: true,// if you want to generate index tags from database, set FieldWithIndexTag true FieldWithIndexTag: true,// if you want to generate type tags from database, set FieldWithTypeTag true FieldWithTypeTag: true,// if you need unit tests for query code, set WithUnitTest true WithUnitTest: true,})
// WithDbNameOpts set get database name functionWithDbNameOpts(opts ...model.SchemaNameOpt)
// WithTableNameStrategy specify table name naming strategy, only work when syncing table from dbWithTableNameStrategy(ns func(tableName string) (targetTableName string))
// WithModelNameStrategy specify model struct name naming strategy, only work when syncing table from db// If an empty string is returned, the table will be ignoredWithModelNameStrategy(ns func(tableName string) (modelName string))
// WithFileNameStrategy specify file name naming strategy, only work when syncing table from dbWithFileNameStrategy(ns func(tableName string) (fileName string))
// WithDataTypeMap specify data type mapping relationship, only work when syncing table from dbWithDataTypeMap(newMap map[string]func(gorm.ColumnType) (dataType string))
// WithOpts specify global model optionsWithOpts(opts ...ModelOpt)
数据类型映射
指定model属性类型和 db 字段类型之间的映射关系。
var dataMap = map[string]func(gorm.ColumnType) (dataType string){// int mapping"int": func(columnType gorm.ColumnType) (dataType string) {if n, ok := columnType.Nullable(); ok && n {return"*int32" }return"int32" },