Compare commits
10 Commits
8277c79d23
...
1147ca4733
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1147ca4733 | ||
|
|
cd7c13e49d | ||
|
|
e5000bcc73 | ||
|
|
9ddea39084 | ||
|
|
e83332ea6a | ||
|
|
ab36ea0e5d | ||
|
|
c6b1a1664c | ||
|
|
f9a32a9ecb | ||
|
|
56ec95e43a | ||
|
|
3617c68a91 |
@@ -26,9 +26,12 @@ var tableTestTpl string
|
||||
var providerTplStr string
|
||||
|
||||
type TableModelParam struct {
|
||||
PkgName string
|
||||
CamelTable string // user
|
||||
PascalTable string // User
|
||||
PkgName string
|
||||
CamelTable string // user
|
||||
PascalTable string // User
|
||||
SoftDelete bool
|
||||
HasUpdatedAt bool
|
||||
HasCreatedAt bool
|
||||
}
|
||||
|
||||
func Generate(tables []string, transformer Transformer) error {
|
||||
@@ -65,6 +68,7 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
return err
|
||||
}
|
||||
|
||||
modelContent := make(map[string]string)
|
||||
for _, file := range files {
|
||||
// get filename without ext
|
||||
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
|
||||
@@ -75,6 +79,13 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
if err := os.Rename(from, to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// read file content
|
||||
content, err := os.ReadFile(to)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modelContent[name] = string(content)
|
||||
}
|
||||
|
||||
// remove database/schemas/public/model
|
||||
@@ -99,8 +110,37 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
PascalTable: lo.PascalCase(table),
|
||||
PkgName: gomod.GetModuleName(),
|
||||
}
|
||||
if strings.Contains(table, "DeletedAt") {
|
||||
tableInfo.SoftDelete = true
|
||||
}
|
||||
if strings.Contains(table, "UpdatedAt") {
|
||||
tableInfo.HasUpdatedAt = true
|
||||
}
|
||||
if strings.Contains(table, "CreatedAt") {
|
||||
tableInfo.HasCreatedAt = true
|
||||
}
|
||||
|
||||
items = append(items, tableInfo)
|
||||
|
||||
// tableFuncsFile
|
||||
tableFuncsFile := fmt.Sprintf("%s/%s.funcs.gen.go", baseDir, table)
|
||||
// 如果 modelFuncsFile 已存在,则跳过
|
||||
if _, err := os.Stat(tableFuncsFile); err == nil {
|
||||
fmt.Printf("Model funcs file %s already exists. Skipping...\n", tableFuncsFile)
|
||||
continue
|
||||
}
|
||||
|
||||
// 如果 modelFuncsFile 不存在,则创建
|
||||
fd, err := os.Create(tableFuncsFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create model funcs file %s: %w", tableFuncsFile, err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if err := tableFuncsTpl.Execute(fd, tableInfo); err != nil {
|
||||
return fmt.Errorf("failed to render model funcs template: %w", err)
|
||||
}
|
||||
|
||||
modelFile := fmt.Sprintf("%s/%s.go", baseDir, table)
|
||||
// 如果 modelFile 已存在,则跳过
|
||||
if _, err := os.Stat(modelFile); err == nil {
|
||||
@@ -109,7 +149,7 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
}
|
||||
|
||||
// 如果 modelFile 不存在,则创建
|
||||
fd, err := os.Create(modelFile)
|
||||
fd, err = os.Create(modelFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create model file %s: %w", modelFile, err)
|
||||
}
|
||||
@@ -137,24 +177,6 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
return fmt.Errorf("failed to render model test template: %w", err)
|
||||
}
|
||||
|
||||
// tableFuncsFile
|
||||
tableFuncsFile := fmt.Sprintf("%s/%s.funcs.gen.go", baseDir, table)
|
||||
// 如果 modelFuncsFile 已存在,则跳过
|
||||
if _, err := os.Stat(tableFuncsFile); err == nil {
|
||||
fmt.Printf("Model funcs file %s already exists. Skipping...\n", tableFuncsFile)
|
||||
continue
|
||||
}
|
||||
|
||||
// 如果 modelFuncsFile 不存在,则创建
|
||||
fd, err = os.Create(tableFuncsFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create model funcs file %s: %w", tableFuncsFile, err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if err := tableFuncsTpl.Execute(fd, tableInfo); err != nil {
|
||||
return fmt.Errorf("failed to render model funcs template: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染总的 provider 文件
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
var {{.CamelTable}}UpdateExcludeColumns = []Column{
|
||||
{{- if .HasCreatedAt}}
|
||||
table.{{.PascalTable}}.CreatedAt,
|
||||
{{- end}}
|
||||
|
||||
func (m *{{.PascalTable}}) log() *log.Entry {
|
||||
return log.WithField("model", "{{.PascalTable}}Model")
|
||||
}
|
||||
{{- if .SoftDelete}}
|
||||
table.{{.PascalTable}}.DeletedAt,
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"{{ .PkgName }}/database/table"
|
||||
|
||||
"github.com/samber/lo"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -11,7 +17,15 @@ func (m *{{.PascalTable}}) log() *log.Entry {
|
||||
}
|
||||
|
||||
func (m *{{.PascalTable}}) Create(ctx context.Context) error {
|
||||
{{- if .HasCreatedAt}}
|
||||
m.CreatedAt = time.Now()
|
||||
{{- end}}
|
||||
|
||||
{{- if .HasUpdatedAt}}
|
||||
m.UpdatedAt = time.Now()
|
||||
{{- end}}
|
||||
|
||||
|
||||
stmt := table.Medias.INSERT(table.{{.PascalTable}}.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
||||
m.log().WithField("func","Create").Info( stmt.DebugSql())
|
||||
|
||||
@@ -38,9 +52,43 @@ func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalT
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete
|
||||
// if SoftDelete
|
||||
{{- if .SoftDelete }}
|
||||
func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.EQ(m.ID))
|
||||
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func","SoftDelete").Errorf("error soft deleting {{.PascalTable}} item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "SoftDelete").Infof("{{.PascalTable}} item soft deleted successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchDelete
|
||||
func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.IN(condIds...))
|
||||
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func","BatchSoftDelete").Errorf("error soft deleting {{.PascalTable}} items: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "BatchSoftDelete").Infof("{{.PascalTable}} items soft deleted successfully")
|
||||
return nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
||||
func (m *{{.PascalTable}}) ForceDelete(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
@@ -52,8 +100,12 @@ func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.IN(ids))
|
||||
func (m *{{.PascalTable}}) BatchForceDelete(ctx context.Context, ids []int64) error {
|
||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.IN(condIds...))
|
||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
@@ -66,7 +118,11 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
}
|
||||
|
||||
func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(m.ID)).RETURNING(table.{{.PascalTable}}.AllColumns)
|
||||
{{- if .HasUpdatedAt}}
|
||||
m.UpdatedAt = time.Now()
|
||||
{{- end}}
|
||||
|
||||
stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns.Except({{.CamelTable}}UpdateExcludeColumns...)).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(table.{{.PascalTable}}.AllColumns)
|
||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
@@ -78,8 +134,14 @@ func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
// GetByID
|
||||
func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64) (*{{.PascalTable}}, error) {
|
||||
stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(table.{{.PascalTable}}.ID.EQ(id))
|
||||
func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64, conds ...BoolExpression) (*{{.PascalTable}}, error) {
|
||||
expr := table.{{.PascalTable}}.ID.EQ(Int(m.ID))
|
||||
if len(conds) > 0 {
|
||||
for _, c := range conds {
|
||||
expr = expr.AND(c)
|
||||
}
|
||||
}
|
||||
stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(expr)
|
||||
m.log().WithField("func", "GetByID").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user