Compare commits
No commits in common. "d328bc3fbd381b8d8fa66b7a8f006b909ccf3e61" and "bb37212be74073b9fc36850572dc10d4a930f609" have entirely different histories.
d328bc3fbd
...
bb37212be7
27
document.go
27
document.go
@ -21,7 +21,6 @@ type IDocument interface {
|
|||||||
Delete() error
|
Delete() error
|
||||||
Remove() error
|
Remove() error
|
||||||
Save() error
|
Save() error
|
||||||
SaveWith(opts *SaveOptions) error
|
|
||||||
setSelf(arg interface{})
|
setSelf(arg interface{})
|
||||||
getExists() bool
|
getExists() bool
|
||||||
setExists(n bool)
|
setExists(n bool)
|
||||||
@ -34,10 +33,6 @@ type IDocument interface {
|
|||||||
setModel(m Model)
|
setModel(m Model)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SaveOptions struct {
|
|
||||||
SetTimestamps bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Document) getCreated() time.Time {
|
func (d *Document) getCreated() time.Time {
|
||||||
return d.Created
|
return d.Created
|
||||||
}
|
}
|
||||||
@ -94,33 +89,25 @@ func (d *Document) Remove() error {
|
|||||||
return d.Delete()
|
return d.Delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveWith - updates this Model in the database,
|
// Save - updates this Model in the database,
|
||||||
// or inserts it if it doesn't exist, using the provided
|
// or inserts it if it doesn't exist
|
||||||
// SaveOptions
|
func (d *Document) Save() 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 := 0; i < val.Len(); i++ {
|
||||||
cur := val.Index(i)
|
cur := val.Index(i)
|
||||||
if err := doSave(d.model.getColl(), !d.exists, opts, cur.Interface()); err != nil {
|
asHId := asId(cur.Interface())
|
||||||
|
if err := doSave(d.model.getColl(), !d.exists && reflect.ValueOf(asHId.Id()).IsZero(), cur.Interface()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return doSave(d.model.getColl(), !d.exists, opts, d.self)
|
asHId := asId(val.Interface())
|
||||||
|
return doSave(d.model.getColl(), !d.exists && reflect.ValueOf(asHId.Id()).IsZero(), d.self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save - updates this Model in the database,
|
|
||||||
// or inserts it if it doesn't exist, using
|
|
||||||
// default SaveOptions
|
|
||||||
func (d *Document) Save() error {
|
|
||||||
return d.SaveWith(&SaveOptions{
|
|
||||||
SetTimestamps: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Document) serializeToStore() any {
|
func (d *Document) serializeToStore() any {
|
||||||
return serializeIDs((d).self)
|
return serializeIDs((d).self)
|
||||||
}
|
}
|
||||||
|
@ -83,14 +83,10 @@ func serializeIDs(input interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
if fv.Type() == reflect.TypeFor[time.Time]() {
|
|
||||||
ret0[bbson.Name] = fv.Interface()
|
|
||||||
} else {
|
} else {
|
||||||
ret0[bbson.Name] = serializeIDs(fv.Interface())
|
ret0[bbson.Name] = serializeIDs(fv.Interface())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ret = ret0
|
ret = ret0
|
||||||
}
|
}
|
||||||
@ -106,7 +102,7 @@ func serializeIDs(input interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
func doSave(c *mongo.Collection, isNew bool, opts *SaveOptions, arg interface{}) error {
|
func doSave(c *mongo.Collection, isNew bool, arg interface{}) error {
|
||||||
var err error
|
var err error
|
||||||
d, ok := arg.(IDocument)
|
d, ok := arg.(IDocument)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -122,12 +118,10 @@ func doSave(c *mongo.Collection, isNew bool, opts *SaveOptions, arg interface{})
|
|||||||
}
|
}
|
||||||
var asHasId = vp.Interface().(HasID)
|
var asHasId = vp.Interface().(HasID)
|
||||||
var asModel = vp.Interface().(IDocument)
|
var asModel = vp.Interface().(IDocument)
|
||||||
if isNew && opts.SetTimestamps {
|
if isNew {
|
||||||
d.setCreated(now)
|
d.setCreated(now)
|
||||||
}
|
}
|
||||||
if opts.SetTimestamps {
|
|
||||||
d.setModified(now)
|
d.setModified(now)
|
||||||
}
|
|
||||||
idxs := d.getModel().getIdxs()
|
idxs := d.getModel().getIdxs()
|
||||||
for _, i := range idxs {
|
for _, i := range idxs {
|
||||||
_, err = c.Indexes().CreateOne(context.TODO(), *i)
|
_, err = c.Indexes().CreateOne(context.TODO(), *i)
|
||||||
|
7
util.go
7
util.go
@ -45,12 +45,7 @@ func asId(i interface{}) HasID {
|
|||||||
var ok bool
|
var ok bool
|
||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
asHasId, ok = v.Interface().(HasID)
|
v = reflect.New(reflect.PointerTo(v.Type()))
|
||||||
if ok {
|
|
||||||
return asHasId
|
|
||||||
}
|
|
||||||
v = reflect.New(v.Type())
|
|
||||||
v.Elem().Set(reflect.ValueOf(i))
|
|
||||||
fallthrough
|
fallthrough
|
||||||
case reflect.Pointer:
|
case reflect.Pointer:
|
||||||
asHasId, ok = v.Interface().(HasID)
|
asHasId, ok = v.Interface().(HasID)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user