Compare commits
	
		
			3 Commits
		
	
	
		
			ad7c7f38af
			...
			f92f447f73
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f92f447f73 | |||
| 93b81308cb | |||
| 322d97dc95 | 
							
								
								
									
										13
									
								
								document.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								document.go
									
									
									
									
									
								
							| @ -7,12 +7,13 @@ import ( | |||||||
| 
 | 
 | ||||||
| type Document struct { | type Document struct { | ||||||
| 	// Created time. updated/added automatically.
 | 	// Created time. updated/added automatically.
 | ||||||
| 	Created time.Time `bson:"createdAt" json:"createdAt"` | 	Created time.Time `bson:"createdAt" json:"createdAt" tstype:"Date"` | ||||||
| 	// Modified time. updated/added automatically.
 | 	// Modified time. updated/added automatically.
 | ||||||
| 	Modified time.Time `bson:"updatedAt" json:"updatedAt"` | 	Modified        time.Time       `bson:"updatedAt" json:"updatedAt" tstype:"Date"` | ||||||
| 	model    *Model    `bson:"-"` | 	model           *Model          `bson:"-"` | ||||||
| 	exists   bool      `bson:"-"` | 	exists          bool            `bson:"-"` | ||||||
| 	self     any       `bson:"-"` | 	self            any             `bson:"-"` | ||||||
|  | 	populatedFields map[string]bool `bson:"-"` | ||||||
| } | } | ||||||
| type IDocument interface { | type IDocument interface { | ||||||
| 	Append(field string, a ...interface{}) error | 	Append(field string, a ...interface{}) error | ||||||
| @ -32,6 +33,8 @@ type IDocument interface { | |||||||
| 	serializeToStore() any | 	serializeToStore() any | ||||||
| 	getModel() *Model | 	getModel() *Model | ||||||
| 	setModel(m Model) | 	setModel(m Model) | ||||||
|  | 	markPopulated(field string) | ||||||
|  | 	markDepopulated(field string) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type SaveOptions struct { | type SaveOptions struct { | ||||||
|  | |||||||
| @ -180,6 +180,7 @@ func doDelete(d *Document, arg interface{}) error { | |||||||
| 	_, 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 | ||||||
|  | 		err = gridFsDel(arg, *d.model) | ||||||
| 	} | 	} | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| @ -221,9 +222,9 @@ func incrementTagged(item interface{}) interface{} { | |||||||
| 			} else { | 			} else { | ||||||
| 				cur.Set(reflect.ValueOf(incrementInterface(nid))) | 				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() | 	return rv.Elem().Interface() | ||||||
| @ -265,3 +266,11 @@ func incrementAll(item interface{}) { | |||||||
| 	default: | 	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,6 +105,82 @@ func gridFsLoad(val any, g gridFSReference, field string) any { | |||||||
| 	return doc.Interface() | 	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 { | func gridFsSave(val any, imodel Model) error { | ||||||
| 	var rerr error | 	var rerr error | ||||||
| 	v := reflect.ValueOf(val) | 	v := reflect.ValueOf(val) | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								model.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								model.go
									
									
									
									
									
								
							| @ -193,6 +193,11 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde | |||||||
| 	return qq, err | 	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) { | func createBase(d any) (reflect.Value, int, string) { | ||||||
| 	var n string | 	var n string | ||||||
| 	var ri *Model | 	var ri *Model | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								query.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								query.go
									
									
									
									
									
								
							| @ -28,6 +28,8 @@ const ( | |||||||
| 	OP_FIND       = "find" | 	OP_FIND       = "find" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | var nilVal = reflect.Zero(reflect.TypeFor[any]()) | ||||||
|  | 
 | ||||||
| func populate(r Reference, rcoll string, rawDoc interface{}, d string, src interface{}) any { | func populate(r Reference, rcoll string, rawDoc interface{}, d string, src interface{}) any { | ||||||
| 	rt := reflect.TypeOf(src) | 	rt := reflect.TypeOf(src) | ||||||
| 	rv := reflect.ValueOf(src) | 	rv := reflect.ValueOf(src) | ||||||
| @ -399,7 +401,7 @@ func rerere(input interface{}, resType reflect.Type) interface{} { | |||||||
| 							fv.Set(reflect.ValueOf(tmp)) | 							fv.Set(reflect.ValueOf(tmp)) | ||||||
| 						} | 						} | ||||||
| 					} else { | 					} else { | ||||||
| 						fv.Set(reflect.Zero(ft.Type)) | 						fv.Set(reflect.ValueOf(nil)) | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user