From f39e1b9c647ff37a9ee77f475446c5747e182bec 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: Fri, 18 Apr 2025 18:34:29 -0400 Subject: [PATCH] rename `getColl` -> `Collection` --- document.go | 6 +++--- document_internals.go | 2 +- model.go | 21 +++++++++------------ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/document.go b/document.go index 1fb604d..0af0b9a 100644 --- a/document.go +++ b/document.go @@ -157,15 +157,15 @@ func (d *Document) Remove() error { func (d *Document) SaveWith(opts *SaveOptions) error { val := valueOf(d.self) if val.Kind() == reflect.Slice { - for i := 0; i < val.Len(); i++ { + for i := range val.Len() { cur := val.Index(i) - if err := doSave(d.model.getColl(), !d.exists, opts, cur.Interface()); err != nil { + if err := doSave(d.model.Collection(), !d.exists, opts, cur.Interface()); err != nil { return err } } return nil } else { - return doSave(d.model.getColl(), !d.exists, opts, d.self) + return doSave(d.model.Collection(), !d.exists, opts, d.self) } } diff --git a/document_internals.go b/document_internals.go index 4248a5c..a3d874d 100644 --- a/document_internals.go +++ b/document_internals.go @@ -220,7 +220,7 @@ func doDelete(d *Document, arg interface{}) error { if !ok { return fmt.Errorf(errFmtNotHasID, nameOf(arg)) } - c := d.model.getColl() + c := d.model.Collection() _, err := c.DeleteOne(context.TODO(), bson.M{"_id": self.Id()}) if err == nil { d.exists = false diff --git a/model.go b/model.go index bfae4ce..354b70a 100644 --- a/model.go +++ b/model.go @@ -40,8 +40,8 @@ type IModel interface { FindByID(id interface{}) (*Query, error) FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error) FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error) + Collection() *mongo.Collection - getColl() *mongo.Collection getIdxs() []*mongo.IndexModel getParsedIdxs() map[string][]InternalIndex getTypeName() string @@ -56,12 +56,9 @@ func (m *Model) setTypeName(str string) { m.typeName = str } -func (m *Model) getColl() *mongo.Collection { - _, ri, ok := ModelRegistry.HasByName(m.typeName) - if !ok { - panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName)) - } - return DB.Collection(ri.collection) +// Collection - returns the collection associated with this Model +func (m *Model) Collection() *mongo.Collection { + return DB.Collection(m.collection) } func (m *Model) getIdxs() []*mongo.IndexModel { @@ -87,7 +84,7 @@ func (m *Model) getParsedIdxs() map[string][]InternalIndex { // FindRaw - find documents satisfying `query` and return a plain mongo cursor. func (m *Model) FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error) { - coll := m.getColl() + coll := m.Collection() if opts == nil { opts = options.Find() } @@ -108,7 +105,7 @@ func (m *Model) Find(query interface{}, opts *options.FindOptionsBuilder) (*Quer qqv.Elem().Set(reflect.MakeSlice(qqt, 0, 0)) qq := &Query{ model: m, - collection: m.getColl(), + collection: m.Collection(), doc: qqv.Interface(), op: OP_FIND_ALL, } @@ -164,7 +161,7 @@ func (m *Model) FindByID(id interface{}) (*Query, error) { // FindOne - find a single document satisfying `query`. // returns a pointer to a Query for further chaining. func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error) { - coll := m.getColl() + coll := m.Collection() rip := coll.FindOne(context.TODO(), query, options) raw := bson.M{} err := rip.Decode(&raw) @@ -176,7 +173,7 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde idoc.setRaw(raw) qq := &Query{ - collection: m.getColl(), + collection: m.Collection(), rawDoc: raw, doc: idoc, op: OP_FIND_ONE, @@ -198,7 +195,7 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde } func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (int64, error) { - coll := m.getColl() + coll := m.Collection() return coll.CountDocuments(context.TODO(), query, options) }