From 14ac93b327df74cc75581ef10552f804ca62aad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Thu, 5 Sep 2024 14:29:35 -0400 Subject: [PATCH] rename model methods - rename FindAll -> Find - rename Find -> FindRaw --- model.go | 23 +++++++++++++++++------ model_test.go | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/model.go b/model.go index ba02c2d..954d2d2 100644 --- a/model.go +++ b/model.go @@ -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 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 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 for further chaining. func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) { coll := m.getColl() rip := coll.FindOne(context.TODO(), query, options...) @@ -330,6 +339,8 @@ func (m *Model) Swap(field string, i, j int) error { return nil } +// Save - updates this Model in the database, +// or inserts it if it doesn't exist func (m *Model) Save() error { val := valueOf(m.self) if val.Kind() == reflect.Slice { diff --git a/model_test.go b/model_test.go index a17112e..98413a3 100644 --- a/model_test.go +++ b/model_test.go @@ -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)