const ( + OP_FIND_ONE = "findOne" + OP_FIND_PAGED = "findPaged" + OP_FIND_ALL = "findAll" + OP_FIND = "find" +)+ + +
const COUNTER_COL = "@counters"
+
+
+ var ( + ErrNotASlice = errors.New("Current object or field is not a slice!") + ErrNotAStruct = errors.New("Current object or field is not a struct!") + ErrOutOfBounds = errors.New("Index(es) out of bounds!") + ErrAppendMultipleDocuments = errors.New("Cannot append to multiple documents!") + ErrNotSliceOrStruct = errors.New("Current object or field is not a slice nor a struct!") + ErrUnsupportedID = errors.New("Unknown or unsupported id type provided") +)+ +
DB - The mongodb database handle +
var DB *mongo.Database+ +
DBClient - The mongodb client +
var DBClient *mongo.Client+ +
ModelRegistry - the ModelRegistry stores a map containing + pointers to Model instances, keyed by an associated + model name +
var ModelRegistry = make(TModelRegistry)+ +
NextStringID - Override this function with your own + string ID generator! +
var NextStringID func() string+ + +
func Connect(uri string, dbName string)+ + +
func Create(d any) any+
Create creates a new instance of a given Document + type and returns a pointer to it. + + +
func CreateSlice[T any](d T) []*T+
CreateSlice - convenience method which creates a new slice + of type *T (where T is a type which embeds Document) and + returns it + + +
type Counter struct { + Current any `bson:"current"` + Collection string `bson:"collection"` +} ++ + +
type Document struct { + // Created time. updated/added automatically. + Created time.Time `bson:"createdAt" json:"createdAt"` + // Modified time. updated/added automatically. + Modified time.Time `bson:"updatedAt" json:"updatedAt"` + // contains filtered or unexported fields +} ++ + +
func (d *Document) Append(field string, a ...interface{}) error+
Append appends one or more items to `field`. + will error if this Model contains a reference + to multiple documents, or if `field` is not a + slice. + + +
func (d *Document) Delete() error+
Delete - deletes a model instance from the database + + +
func (d *Document) Pull(field string, a ...any) error+
Pull - removes elements from the subdocument slice stored in `field`. + + +
func (d *Document) Remove() error+
Remove - alias for Delete + + +
func (d *Document) Save() error+
Save - updates this Model in the database, + or inserts it if it doesn't exist + + +
func (d *Document) Swap(field string, i, j int) error+
Swap - swaps the elements at indexes `i` and `j` in the + slice stored at `field` + + +
type DocumentSlice []IDocument+ + +
func (d *DocumentSlice) Delete() error+ + +
func (d *DocumentSlice) Remove() error+ + +
func (d *DocumentSlice) Save() error+ + +
type GridFSFile struct { + ID primitive.ObjectID `bson:"_id"` + Name string `bson:"filename"` + Length int `bson:"length"` +} ++ + +
HasID is a simple interface that you must implement + in your models, using a pointer receiver. + This allows for more flexibility in cases where + your ID isn't an ObjectID (e.g., int, uint, string...). +
and yes, those darn ugly ObjectIDs are supported :) +
type HasID interface { + Id() any + SetId(id any) +}+ + +
type HasIDSlice []HasID+ + +
type IDocument interface { + Append(field string, a ...interface{}) error + Pull(field string, a ...any) error + Swap(field string, i, j int) error + Delete() error + Remove() error + Save() error + // contains filtered or unexported methods +}+ + +
type IDocumentSlice interface { + Delete() error + Remove() error + Save() error + // contains filtered or unexported methods +}+ + +
type IModel interface { + FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) + Find(query interface{}, opts ...*options.FindOptions) (*Query, error) + FindByID(id interface{}) (*Query, error) + FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error) + FindPaged(query interface{}, page int64, perPage int64, options ...*options.FindOptions) (*Query, error) + // contains filtered or unexported methods +}+ + +
prolly won't need to use indexes, but just in case... +
type InternalIndex struct { + Fields []string + Options []string + // contains filtered or unexported fields +} ++ + +
Model - type which contains "static" methods like + Find, FindOne, etc. +
type Model struct { + Indexes map[string][]InternalIndex + Type reflect.Type + // contains filtered or unexported fields +} ++ + +
func (m *Model) Find(query interface{}, opts ...*options.FindOptions) (*Query, error)+
Find - find all documents satisfying `query`. + returns a pointer to a Query for further chaining. + + +
func (m *Model) FindByID(id interface{}) (*Query, error)+
FindByID - find a single document by its _id field. + Wrapper around FindOne with an ID query as its first argument + + +
func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (*Query, error)+
FindOne - find a single document satisfying `query`. + returns a pointer to a Query for further chaining. + + +
func (m *Model) FindPaged(query interface{}, page int64, perPage int64, opts ...*options.FindOptions) (*Query, error)+
FindPaged - Wrapper around FindAll with the Skip and Limit options populated. + returns a pointer to a Query for further chaining. + + +
func (m *Model) FindRaw(query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)+
FindRaw - find documents satisfying `query` and return a plain mongo cursor. + + +
type Query struct {
+ // contains filtered or unexported fields
+}
+
+
+
+ func (q *Query) Exec(result interface{})+
Exec - executes the query and puts its results into the + provided argument. +
Will panic if called more than once on the same Query instance. + + +
func (q *Query) JSON() (string, error)+
JSON - marshals this Query's results into json format + + +
func (q *Query) LoadFile(fields ...string) *Query+
LoadFile - loads the contents of one or more files + stored in gridFS into the fields named by `fields`. +
gridFS fields can be either a `string` or `[]byte`, and are + tagged with `gridfs:"BUCKET,FILE_FORMAT` + where: +
func (q *Query) Populate(fields ...string) *Query+
Populate populates document references via reflection + + +
Reference stores a typed document reference +
type Reference struct { + // owning model name + Model string + // the name of the struct field + FieldName string + // index of field in owning struct + Idx int + // the type of the referenced object + HydratedType reflect.Type + + // field kind (struct, slice, ...) + Kind reflect.Kind + // contains filtered or unexported fields +} ++ + +
type TModelRegistry map[string]*Model+ + +
func (r TModelRegistry) Get(name string) *Model+ + +
func (r TModelRegistry) Has(i interface{}) (string, *Model, bool)+
Has returns the model typename and Model instance corresponding + to the argument passed, as well as a boolean indicating whether it + was found. otherwise returns `"", nil, false` + + +
func (r TModelRegistry) HasByName(n string) (string, *Model, bool)+
HasByName functions almost identically to Has, + except that it takes a string as its argument. + + +
func (r TModelRegistry) Index(n string) int+
Index returns the index at which the Document struct is embedded + + +
func (r TModelRegistry) Model(mdl ...any)+
Model registers models in the ModelRegistry, where + they can be accessed via a model's struct name + + +