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 {
|
func (d *Document) SaveWith(opts *SaveOptions) error {
|
||||||
val := valueOf(d.self)
|
val := valueOf(d.self)
|
||||||
if val.Kind() == reflect.Slice {
|
if val.Kind() == reflect.Slice {
|
||||||
for i := 0; i < val.Len(); i++ {
|
for i := range val.Len() {
|
||||||
cur := val.Index(i)
|
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 err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} 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 {
|
if !ok {
|
||||||
return fmt.Errorf(errFmtNotHasID, nameOf(arg))
|
return fmt.Errorf(errFmtNotHasID, nameOf(arg))
|
||||||
}
|
}
|
||||||
c := d.model.getColl()
|
c := d.model.Collection()
|
||||||
_, err := c.DeleteOne(context.TODO(), bson.M{"_id": self.Id()})
|
_, err := c.DeleteOne(context.TODO(), bson.M{"_id": self.Id()})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
d.exists = false
|
d.exists = false
|
||||||
|
21
model.go
21
model.go
@ -40,8 +40,8 @@ type IModel interface {
|
|||||||
FindByID(id interface{}) (*Query, error)
|
FindByID(id interface{}) (*Query, error)
|
||||||
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error)
|
||||||
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
FindPaged(query interface{}, page int64, perPage int64, options *options.FindOptionsBuilder) (*Query, error)
|
||||||
|
Collection() *mongo.Collection
|
||||||
|
|
||||||
getColl() *mongo.Collection
|
|
||||||
getIdxs() []*mongo.IndexModel
|
getIdxs() []*mongo.IndexModel
|
||||||
getParsedIdxs() map[string][]InternalIndex
|
getParsedIdxs() map[string][]InternalIndex
|
||||||
getTypeName() string
|
getTypeName() string
|
||||||
@ -56,12 +56,9 @@ func (m *Model) setTypeName(str string) {
|
|||||||
m.typeName = str
|
m.typeName = str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) getColl() *mongo.Collection {
|
// Collection - returns the collection associated with this Model
|
||||||
_, ri, ok := ModelRegistry.HasByName(m.typeName)
|
func (m *Model) Collection() *mongo.Collection {
|
||||||
if !ok {
|
return DB.Collection(m.collection)
|
||||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
|
||||||
}
|
|
||||||
return DB.Collection(ri.collection)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) getIdxs() []*mongo.IndexModel {
|
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.
|
// FindRaw - find documents satisfying `query` and return a plain mongo cursor.
|
||||||
func (m *Model) FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error) {
|
func (m *Model) FindRaw(query interface{}, opts *options.FindOptionsBuilder) (*mongo.Cursor, error) {
|
||||||
coll := m.getColl()
|
coll := m.Collection()
|
||||||
if opts == nil {
|
if opts == nil {
|
||||||
opts = options.Find()
|
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))
|
qqv.Elem().Set(reflect.MakeSlice(qqt, 0, 0))
|
||||||
qq := &Query{
|
qq := &Query{
|
||||||
model: m,
|
model: m,
|
||||||
collection: m.getColl(),
|
collection: m.Collection(),
|
||||||
doc: qqv.Interface(),
|
doc: qqv.Interface(),
|
||||||
op: OP_FIND_ALL,
|
op: OP_FIND_ALL,
|
||||||
}
|
}
|
||||||
@ -164,7 +161,7 @@ func (m *Model) FindByID(id interface{}) (*Query, error) {
|
|||||||
// FindOne - find a single document satisfying `query`.
|
// FindOne - find a single document satisfying `query`.
|
||||||
// returns a pointer to a Query for further chaining.
|
// returns a pointer to a Query for further chaining.
|
||||||
func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error) {
|
func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilder) (*Query, error) {
|
||||||
coll := m.getColl()
|
coll := m.Collection()
|
||||||
rip := coll.FindOne(context.TODO(), query, options)
|
rip := coll.FindOne(context.TODO(), query, options)
|
||||||
raw := bson.M{}
|
raw := bson.M{}
|
||||||
err := rip.Decode(&raw)
|
err := rip.Decode(&raw)
|
||||||
@ -176,7 +173,7 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde
|
|||||||
idoc.setRaw(raw)
|
idoc.setRaw(raw)
|
||||||
|
|
||||||
qq := &Query{
|
qq := &Query{
|
||||||
collection: m.getColl(),
|
collection: m.Collection(),
|
||||||
rawDoc: raw,
|
rawDoc: raw,
|
||||||
doc: idoc,
|
doc: idoc,
|
||||||
op: OP_FIND_ONE,
|
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) {
|
func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (int64, error) {
|
||||||
coll := m.getColl()
|
coll := m.Collection()
|
||||||
return coll.CountDocuments(context.TODO(), query, options)
|
return coll.CountDocuments(context.TODO(), query, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user