Skip to content

Commit

Permalink
"Exists" method for store
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbuchma committed Sep 21, 2017
1 parent 8048762 commit a43b681
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions generator/templates/model.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,25 @@ func (s *{{.StoreName}}) MustCount(q *{{.QueryName}}) int64 {
return s.Store.MustCount(q)
}

// Exists returns true if there is at least one record by given query.
func (s *{{.StoreName}}) Exists(q *{{.QueryName}}) (bool, error) {
q.Limit(1)
q.Select(Schema.{{.Name}}.ID)
q.Offset(0)
rs, err := s.Find(q)
if err != nil {
return false, err
}

if !rs.Next() {
return false, nil
}

err = rs.Close()
return true, err
}


// FindOne returns the first row returned by the given query.
// `ErrNotFound` is returned if there are no results.
func (s *{{.StoreName}}) FindOne(q *{{.QueryName}}) (*{{.Name}}, error) {
Expand Down

0 comments on commit a43b681

Please sign in to comment.