Compare commits
No commits in common. "f92f447f73eaa9d1521393878edfff6c8b7cb38c" and "ad7c7f38afb94e20930a2161c9b93d6de7d73afe" have entirely different histories.
f92f447f73
...
ad7c7f38af
13
document.go
13
document.go
@ -7,13 +7,12 @@ import (
|
||||
|
||||
type Document struct {
|
||||
// Created time. updated/added automatically.
|
||||
Created time.Time `bson:"createdAt" json:"createdAt" tstype:"Date"`
|
||||
Created time.Time `bson:"createdAt" json:"createdAt"`
|
||||
// Modified time. updated/added automatically.
|
||||
Modified time.Time `bson:"updatedAt" json:"updatedAt" tstype:"Date"`
|
||||
model *Model `bson:"-"`
|
||||
exists bool `bson:"-"`
|
||||
self any `bson:"-"`
|
||||
populatedFields map[string]bool `bson:"-"`
|
||||
Modified time.Time `bson:"updatedAt" json:"updatedAt"`
|
||||
model *Model `bson:"-"`
|
||||
exists bool `bson:"-"`
|
||||
self any `bson:"-"`
|
||||
}
|
||||
type IDocument interface {
|
||||
Append(field string, a ...interface{}) error
|
||||
@ -33,8 +32,6 @@ type IDocument interface {
|
||||
serializeToStore() any
|
||||
getModel() *Model
|
||||
setModel(m Model)
|
||||
markPopulated(field string)
|
||||
markDepopulated(field string)
|
||||
}
|
||||
|
||||
type SaveOptions struct {
|
||||
|
@ -180,7 +180,6 @@ func doDelete(d *Document, arg interface{}) error {
|
||||
_, err := c.DeleteOne(context.TODO(), bson.M{"_id": self.Id()})
|
||||
if err == nil {
|
||||
d.exists = false
|
||||
err = gridFsDel(arg, *d.model)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -222,9 +221,9 @@ func incrementTagged(item interface{}) interface{} {
|
||||
} else {
|
||||
cur.Set(reflect.ValueOf(incrementInterface(nid)))
|
||||
}
|
||||
counterColl := DB.Collection(COUNTER_COL)
|
||||
counterColl.UpdateOne(context.TODO(), bson.M{"collection": incTag.Name}, bson.M{"$set": bson.M{"collection": incTag.Name, "current": cur.Interface()}}, options.UpdateOne().SetUpsert(true))
|
||||
}
|
||||
counterColl := DB.Collection(COUNTER_COL)
|
||||
counterColl.UpdateOne(context.TODO(), bson.M{"collection": incTag.Name}, bson.M{"$set": bson.M{"collection": incTag.Name, "current": cur.Interface()}}, options.UpdateOne().SetUpsert(true))
|
||||
|
||||
}
|
||||
return rv.Elem().Interface()
|
||||
@ -266,11 +265,3 @@ func incrementAll(item interface{}) {
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Document) markPopulated(field string) {
|
||||
d.populatedFields[field] = true
|
||||
}
|
||||
|
||||
func (d *Document) markDepopulated(field string) {
|
||||
d.populatedFields[field] = false
|
||||
}
|
||||
|
76
gridfs.go
76
gridfs.go
@ -105,82 +105,6 @@ func gridFsLoad(val any, g gridFSReference, field string) any {
|
||||
return doc.Interface()
|
||||
}
|
||||
|
||||
func gridFsDel(val any, imodel Model) error {
|
||||
var rerr error
|
||||
v := reflect.ValueOf(val)
|
||||
el := v
|
||||
if v.Kind() == reflect.Pointer {
|
||||
el = el.Elem()
|
||||
}
|
||||
switch el.Kind() {
|
||||
case reflect.Struct:
|
||||
for i := 0; i < el.NumField(); i++ {
|
||||
ft := el.Type().Field(i)
|
||||
fv := el.Field(i)
|
||||
if !ft.IsExported() {
|
||||
continue
|
||||
}
|
||||
_, err := structtag.Parse(string(ft.Tag))
|
||||
panik(err)
|
||||
var gfsRef *gridFSReference
|
||||
for kk, vv := range imodel.gridFSReferences {
|
||||
if strings.HasPrefix(kk, ft.Name) {
|
||||
gfsRef = &vv
|
||||
break
|
||||
}
|
||||
}
|
||||
var inner = func(b *mongo.GridFSBucket, it reflect.Value) error {
|
||||
filename := parseFmt(gfsRef.FilenameFmt, it.Interface())
|
||||
contents := GridFSFile{}
|
||||
curs, err2 := b.Find(context.TODO(), bson.M{"filename": filename})
|
||||
|
||||
if !errors.Is(err2, mongo.ErrNoDocuments) {
|
||||
_ = curs.Decode(&contents)
|
||||
if !reflect.ValueOf(contents).IsZero() {
|
||||
return b.Delete(context.TODO(), contents.ID)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if gfsRef != nil {
|
||||
b := bucket(*gfsRef)
|
||||
if fv.Kind() == reflect.Slice {
|
||||
for j := 0; j < fv.Len(); j++ {
|
||||
lerr := inner(b, fv.Index(j))
|
||||
if lerr != nil {
|
||||
return lerr
|
||||
}
|
||||
}
|
||||
} else if fv.Kind() == reflect.Struct {
|
||||
lerr := inner(b, fv)
|
||||
if lerr != nil {
|
||||
return lerr
|
||||
}
|
||||
} else {
|
||||
lerr := inner(b, el)
|
||||
if lerr != nil {
|
||||
return lerr
|
||||
}
|
||||
}
|
||||
}
|
||||
err = gridFsDel(fv.Interface(), imodel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case reflect.Slice:
|
||||
for i := 0; i < el.Len(); i++ {
|
||||
rerr = gridFsDel(el.Index(i).Interface(), imodel)
|
||||
if rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
return rerr
|
||||
}
|
||||
|
||||
func gridFsSave(val any, imodel Model) error {
|
||||
var rerr error
|
||||
v := reflect.ValueOf(val)
|
||||
|
5
model.go
5
model.go
@ -193,11 +193,6 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde
|
||||
return qq, err
|
||||
}
|
||||
|
||||
func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (int64, error) {
|
||||
coll := m.getColl()
|
||||
return coll.CountDocuments(context.TODO(), query, options)
|
||||
}
|
||||
|
||||
func createBase(d any) (reflect.Value, int, string) {
|
||||
var n string
|
||||
var ri *Model
|
||||
|
4
query.go
4
query.go
@ -28,8 +28,6 @@ const (
|
||||
OP_FIND = "find"
|
||||
)
|
||||
|
||||
var nilVal = reflect.Zero(reflect.TypeFor[any]())
|
||||
|
||||
func populate(r Reference, rcoll string, rawDoc interface{}, d string, src interface{}) any {
|
||||
rt := reflect.TypeOf(src)
|
||||
rv := reflect.ValueOf(src)
|
||||
@ -401,7 +399,7 @@ func rerere(input interface{}, resType reflect.Type) interface{} {
|
||||
fv.Set(reflect.ValueOf(tmp))
|
||||
}
|
||||
} else {
|
||||
fv.Set(reflect.ValueOf(nil))
|
||||
fv.Set(reflect.Zero(ft.Type))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user