Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
goctl
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yao Lei
goctl
Commits
e3c370b1
Commit
e3c370b1
authored
Aug 30, 2022
by
Yuchao Shen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add updateOrCreate firstOrCreate
parent
f87708e5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
5 deletions
+28
-5
insert.tpl
1.4.0/model/insert.tpl
+22
-2
interface-insert.tpl
1.4.0/model/interface-insert.tpl
+4
-2
update.tpl
1.4.0/model/update.tpl
+2
-1
No files found.
1.4.0/model/insert.tpl
View file @
e3c370b1
...
...
@@ -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
}
}
}
1.4.0/model/interface-insert.tpl
View file @
e3c370b1
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
1.4.0/model/update.tpl
View file @
e3c370b1
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment