feat: support softdelete in table functions
This commit is contained in:
@@ -29,6 +29,7 @@ type TableModelParam struct {
|
||||
PkgName string
|
||||
CamelTable string // user
|
||||
PascalTable string // User
|
||||
SoftDelete bool
|
||||
}
|
||||
|
||||
func Generate(tables []string, transformer Transformer) error {
|
||||
@@ -65,6 +66,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 +77,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,6 +108,10 @@ func Generate(tables []string, transformer Transformer) error {
|
||||
PascalTable: lo.PascalCase(table),
|
||||
PkgName: gomod.GetModuleName(),
|
||||
}
|
||||
if strings.Contains(table, "DeletedAt") {
|
||||
tableInfo.SoftDelete = true
|
||||
}
|
||||
|
||||
items = append(items, tableInfo)
|
||||
|
||||
// tableFuncsFile
|
||||
|
||||
@@ -44,6 +44,8 @@ func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalT
|
||||
return nil
|
||||
}
|
||||
|
||||
// if SoftDelete
|
||||
{{- if .SoftDelete }}
|
||||
|
||||
func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
@@ -75,6 +77,7 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
m.log().WithField("func", "BatchSoftDelete").Infof("{{.PascalTable}} items soft deleted successfully")
|
||||
return nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
||||
func (m *{{.PascalTable}}) ForceDelete(ctx context.Context) error {
|
||||
|
||||
Reference in New Issue
Block a user