From a43b6819d44088b5d6fad185a5a34f9370217c24 Mon Sep 17 00:00:00 2001 From: Joseph Buchma Date: Fri, 22 Sep 2017 01:19:37 +0300 Subject: [PATCH] "Exists" method for store --- generator/templates/model.tgo | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/generator/templates/model.tgo b/generator/templates/model.tgo index 1d30659..9e9bee8 100644 --- a/generator/templates/model.tgo +++ b/generator/templates/model.tgo @@ -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) {