rename getColl
-> Collection
This commit is contained in:
parent
a608ddd46d
commit
f39e1b9c64
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
21
model.go
21
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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user