-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LvGJ
committed
Dec 28, 2023
1 parent
f6b0328
commit 7a4bce7
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
|
||
"gorm.io/gen" | ||
) | ||
|
||
const MySQLDSN = "" | ||
|
||
var DB *gorm.DB | ||
|
||
func init() { | ||
DB = ConnectDB(MySQLDSN).Debug() | ||
} | ||
|
||
var dataMap = map[string]func(gorm.ColumnType) (dataType string){} | ||
|
||
func main() { | ||
g := gen.NewGenerator(gen.Config{ | ||
OutPath: "../../dal", | ||
ModelPkgPath: "../../model/entity", | ||
Mode: gen.WithDefaultQuery, | ||
|
||
WithUnitTest: false, | ||
//FieldNullable: true, | ||
//FieldCoverable: true, | ||
FieldWithIndexTag: true, | ||
//FieldSignable: true, | ||
FieldWithTypeTag: true, | ||
}) | ||
|
||
g.WithModelNameStrategy(func(tableName string) (modelName string) { | ||
if tableName == "meta" { | ||
return "Meta" | ||
} | ||
return DB.NamingStrategy.SchemaName(tableName) | ||
}) | ||
g.UseDB(DB) | ||
|
||
//g.WithDataTypeMap(dataMap) | ||
//g.WithJSONTagNameStrategy(func(c string) string { return "-" }) | ||
// | ||
//g.ApplyBasic(dto.AttachmentDTO{}) | ||
g.ApplyBasic(g.GenerateAllTable()...) | ||
g.GenerateAllTable() | ||
|
||
g.Execute() | ||
} | ||
|
||
func ConnectDB(dsn string) (db *gorm.DB) { | ||
var err error | ||
|
||
if strings.HasSuffix(dsn, "sqlite.db") { | ||
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{}) | ||
} else { | ||
db, err = gorm.Open(mysql.Open(dsn)) | ||
} | ||
|
||
if err != nil { | ||
panic(fmt.Errorf("connect db fail: %w", err)) | ||
} | ||
|
||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
PROJECT_DIR=$(dirname "$0") | ||
GENERATE_DIR="$PROJECT_DIR/gen" | ||
|
||
cd "$GENERATE_DIR" || exit | ||
|
||
echo "Start Generating" | ||
go run . |