Compare commits

..

No commits in common. "f39e1b9c647ff37a9ee77f475446c5747e182bec" and "95b6e3f1bf9b4487eb8fa3eeccde3713b0b29d95" have entirely different histories.

4 changed files with 17 additions and 14 deletions

View File

@ -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 := range val.Len() {
for i := 0; i < val.Len(); i++ {
cur := val.Index(i)
if err := doSave(d.model.Collection(), !d.exists, opts, cur.Interface()); err != nil {
if err := doSave(d.model.getColl(), !d.exists, opts, cur.Interface()); err != nil {
return err
}
}
return nil
} else {
return doSave(d.model.Collection(), !d.exists, opts, d.self)
return doSave(d.model.getColl(), !d.exists, opts, d.self)
}
}

View File

@ -220,7 +220,7 @@ func doDelete(d *Document, arg interface{}) error {
if !ok {
return fmt.Errorf(errFmtNotHasID, nameOf(arg))
}
c := d.model.Collection()
c := d.model.getColl()
_, err := c.DeleteOne(context.TODO(), bson.M{"_id": self.Id()})
if err == nil {
d.exists = false

View File

@ -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,9 +56,12 @@ func (m *Model) setTypeName(str string) {
m.typeName = str
}
// Collection - returns the collection associated with this Model
func (m *Model) Collection() *mongo.Collection {
return DB.Collection(m.collection)
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)
}
func (m *Model) getIdxs() []*mongo.IndexModel {
@ -84,7 +87,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.Collection()
coll := m.getColl()
if opts == nil {
opts = options.Find()
}
@ -105,7 +108,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.Collection(),
collection: m.getColl(),
doc: qqv.Interface(),
op: OP_FIND_ALL,
}
@ -161,7 +164,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.Collection()
coll := m.getColl()
rip := coll.FindOne(context.TODO(), query, options)
raw := bson.M{}
err := rip.Decode(&raw)
@ -173,7 +176,7 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde
idoc.setRaw(raw)
qq := &Query{
collection: m.Collection(),
collection: m.getColl(),
rawDoc: raw,
doc: idoc,
op: OP_FIND_ONE,
@ -195,7 +198,7 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde
}
func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (int64, error) {
coll := m.Collection()
coll := m.getColl()
return coll.CountDocuments(context.TODO(), query, options)
}

View File

@ -586,7 +586,7 @@ func handleAnon(raw interface{}, rtype reflect.Type, rval reflect.Value) reflect
if reflect.TypeOf(fval) == reflect.TypeFor[string]() && typeField.Type == reflect.TypeFor[time.Time]() {
tt, _ := time.Parse(time.RFC3339, fval.(string))
valueField.Set(reflect.ValueOf(tt))
} else if fval != nil {
} else {
valueField.Set(reflect.ValueOf(fval))
}