Skip to content

Commit a43b681

Browse files
committed
"Exists" method for store
1 parent 8048762 commit a43b681

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: generator/templates/model.tgo

+19
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ func (s *{{.StoreName}}) MustCount(q *{{.QueryName}}) int64 {
408408
return s.Store.MustCount(q)
409409
}
410410

411+
// Exists returns true if there is at least one record by given query.
412+
func (s *{{.StoreName}}) Exists(q *{{.QueryName}}) (bool, error) {
413+
q.Limit(1)
414+
q.Select(Schema.{{.Name}}.ID)
415+
q.Offset(0)
416+
rs, err := s.Find(q)
417+
if err != nil {
418+
return false, err
419+
}
420+
421+
if !rs.Next() {
422+
return false, nil
423+
}
424+
425+
err = rs.Close()
426+
return true, err
427+
}
428+
429+
411430
// FindOne returns the first row returned by the given query.
412431
// `ErrNotFound` is returned if there are no results.
413432
func (s *{{.StoreName}}) FindOne(q *{{.QueryName}}) (*{{.Name}}, error) {

0 commit comments

Comments
 (0)