Skip to content

Commit

Permalink
feat: add gen script
Browse files Browse the repository at this point in the history
  • Loading branch information
LvGJ committed Dec 28, 2023
1 parent f6b0328 commit 7a4bce7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ sonic.db
sonic
__debug_bin
/resources/template/theme/
!/resources/template/theme/default-theme-anatole
!/resources/template/theme/default-theme-anatole
/conf/config.local.yaml
70 changes: 70 additions & 0 deletions scripts/gen/generate.go
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){}

Check failure on line 22 in scripts/gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

var `dataMap` is unused (unused)

func main() {
g := gen.NewGenerator(gen.Config{
OutPath: "../../dal",
ModelPkgPath: "../../model/entity",
Mode: gen.WithDefaultQuery,

WithUnitTest: false,
//FieldNullable: true,

Check failure on line 31 in scripts/gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
//FieldCoverable: true,
FieldWithIndexTag: true,
//FieldSignable: true,

Check failure on line 34 in scripts/gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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)

Check failure on line 46 in scripts/gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
//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
}
9 changes: 9 additions & 0 deletions scripts/generate.sh
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 .

0 comments on commit 7a4bce7

Please sign in to comment.