Commit e3c370b1 by Yuchao Shen

add updateOrCreate firstOrCreate

parent f87708e5
......@@ -5,4 +5,25 @@ func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, dat
return conn.Save(&data).Error
}, {{.keyValues}}){{else}}err:=m.conn.WithContext(ctx).Save(&data).Error{{end}}
return err
}
\ No newline at end of file
}
func (m *default{{.upperStartCamelObject}}Model) FirstOrCreate(ctx context.Context, where interface{}, data *{{.upperStartCamelObject}}) error {
{{if .withCache}}{{.keys}}
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Save(&data).Error
}, {{.keyValues}}){{else}}var resp {{.upperStartCamelObject}}
err := m.conn.WithContext(ctx).First(&resp, where).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
err = m.conn.WithContext(ctx).Create(data).Error
return err{{end}}
}
func (m *default{{.upperStartCamelObject}}Model) UpdateOrCreate(ctx context.Context, where interface{}, data *{{.upperStartCamelObject}}) error {
{{if .withCache}}{{.keys}}
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Save(&data).Error
}, {{.keyValues}}){{else}}err := m.conn.WithContext(ctx).Where(where).Assign(data).FirstOrCreate(&{{.upperStartCamelObject}}{}).Error
return err{{end}}
}
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) error
\ No newline at end of file
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) error
FirstOrCreate(ctx context.Context, where interface{}, data *{{.upperStartCamelObject}}) error
UpdateOrCreate(ctx context.Context, where interface{}, data *{{.upperStartCamelObject}}) error
\ No newline at end of file
......@@ -5,4 +5,4 @@ func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, dat
return conn.Save(data).Error
}, {{.keyValues}}){{else}}err:=m.conn.WithContext(ctx).Save(data).Error{{end}}
return err
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment