rename model methods

- rename FindAll -> Find
- rename Find -> FindRaw
This commit is contained in:
parent 08ea2d9627
commit 39ddb693b4
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
2 changed files with 17 additions and 8 deletions

@ -54,8 +54,8 @@ type HasIDSlice []HasID
type IModel interface {
Append(field string, a ...interface{}) error
Delete() error
Find(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
FindAll(query interface{}, opts ...*options.FindOptions) (*Query, error)
FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
FindByID(id interface{}) (*Query, error)
FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error)
@ -120,13 +120,16 @@ func (m *Model) getParsedIdxs() map[string][]InternalIndex {
return ri.Indexes
}
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
// FindRaw - find documents satisfying `query` and return a plain mongo cursor.
func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
coll := m.getColl()
cursor, err := coll.Find(context.TODO(), query, opts...)
return cursor, err
}
func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query, error) {
// Find - find all documents satisfying `query`.
// returns a pointer to a Query object for further chaining.
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error) {
qqn := ModelRegistry.new_(m.typeName)
qqv := reflect.New(reflect.SliceOf(reflect.TypeOf(qqn).Elem()))
qqv.Elem().Set(reflect.Zero(qqv.Elem().Type()))
@ -136,7 +139,7 @@ func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query
doc: qqv.Interface(),
Op: OP_FIND_ALL,
}
q, err := m.Find(query, opts...)
q, err := m.FindRaw(query, opts...)
if err == nil {
rawRes := bson.A{}
@ -155,6 +158,8 @@ func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query
return qq, err
}
// FindPaged - Wrapper around FindAll with the Skip and Limit options populated.
// returns a pointer to a Query object for further chaining.
func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error) {
skipAmt := perPage * (page - 1)
if skipAmt < 0 {
@ -165,15 +170,19 @@ func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...
} else {
opts = append(opts, options.Find().SetSkip(skipAmt).SetLimit(perPage))
}
q, err := m.FindAll(query, opts...)
q, err := m.Find(query, opts...)
q.Op = OP_FIND_PAGED
return q, err
}
// FindByID - find a single document by its _id field.
// Wrapper around FindOne with an ID query as its first argument
func (m *Model) FindByID(id interface{}) (*Query, error) {
return m.FindOne(bson.D{{"_id", id}})
}
// FindOne - find a single document satisfying `query`.
// returns a pointer to a Query object for further chaining.
func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) {
coll := m.getColl()
rip := coll.FindOne(context.TODO(), query, options...)

@ -81,7 +81,7 @@ func TestModel_FindAll(t *testing.T) {
initTest()
createAndSave(t, &iti_multi)
smodel := Create(story{}).(*story)
query, err := smodel.FindAll(bson.M{}, options.Find())
query, err := smodel.Find(bson.M{}, options.Find())
assert.Equal(t, nil, err)
final := make([]story, 0)
query.Exec(&final)
@ -94,7 +94,7 @@ func TestModel_PopulateMulti(t *testing.T) {
saveDoc(t, bandDoc)
createAndSave(t, &iti_multi)
smodel := Create(story{}).(*story)
query, err := smodel.FindAll(bson.M{}, options.Find())
query, err := smodel.Find(bson.M{}, options.Find())
assert.Equal(t, nil, err)
final := make([]story, 0)
query.Populate("Author", "Chapters.Bands").Exec(&final)