Skip to content

Commit

Permalink
perf: optimize FindByPage
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Feb 24, 2022
1 parent 1e781ca commit c1fcd5c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/template/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,21 @@ func ({{.S}} {{.NewStructName}}Do) FirstOrCreate() (*{{.StructInfo.Package}}.{{.
}
func ({{.S}} {{.NewStructName}}Do) FindByPage(offset int, limit int) (result []*{{.StructInfo.Package}}.{{.StructInfo.Type}}, count int64, err error) {
count, err = {{.S}}.Count()
if err != nil {
if limit <= 0 {
return
}
if limit <= 0 {
result, err = {{.S}}.Offset(offset).Limit(limit).Find()
if err != nil{
return
}
result, err = {{.S}}.Offset(offset).Limit(limit).Find()
if size := len(result); 0 < size && size < limit {
count = int64(size+offset)
return
}
count, err = {{.S}}.Count()
return
}
Expand Down

0 comments on commit c1fcd5c

Please sign in to comment.