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 { type IModel interface {
Append(field string, a ...interface{}) error Append(field string, a ...interface{}) error
Delete() error Delete() error
Find(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
FindAll(query interface{}, opts ...*options.FindOptions) (*Query, error) Find(query interface{}, opts ...*options.FindOptions) (*Query, error)
FindByID(id interface{}) (*Query, error) FindByID(id interface{}) (*Query, error)
FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)
FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*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 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() coll := m.getColl()
cursor, err := coll.Find(context.TODO(), query, opts...) cursor, err := coll.Find(context.TODO(), query, opts...)
return cursor, err 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) qqn := ModelRegistry.new_(m.typeName)
qqv := reflect.New(reflect.SliceOf(reflect.TypeOf(qqn).Elem())) qqv := reflect.New(reflect.SliceOf(reflect.TypeOf(qqn).Elem()))
qqv.Elem().Set(reflect.Zero(qqv.Elem().Type())) 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(), doc: qqv.Interface(),
Op: OP_FIND_ALL, Op: OP_FIND_ALL,
} }
q, err := m.Find(query, opts...) q, err := m.FindRaw(query, opts...)
if err == nil { if err == nil {
rawRes := bson.A{} rawRes := bson.A{}
@ -155,6 +158,8 @@ func (m *Model) FindAll(query interface{}, opts ...*options.FindOptions) (*Query
return qq, err 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) { func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error) {
skipAmt := perPage * (page - 1) skipAmt := perPage * (page - 1)
if skipAmt < 0 { if skipAmt < 0 {
@ -165,15 +170,19 @@ func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...
} else { } else {
opts = append(opts, options.Find().SetSkip(skipAmt).SetLimit(perPage)) 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 q.Op = OP_FIND_PAGED
return q, err 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) { func (m *Model) FindByID(id interface{}) (*Query, error) {
return m.FindOne(bson.D{{"_id", id}}) 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) { func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) {
coll := m.getColl() coll := m.getColl()
rip := coll.FindOne(context.TODO(), query, options...) rip := coll.FindOne(context.TODO(), query, options...)

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