Documentation
¶
Index ¶
- func AggregateOption() *options.AggregateOptions
- func Array(args ...any) primitive.A
- func Doc(args ...any) primitive.D
- func FindOption(sort any, skip int64, limit int64) *options.FindOptions
- func In(k string, v ...any) primitive.M
- func IsValidObjectId(id *primitive.ObjectID) bool
- func Map(args ...any) primitive.M
- func Maps(args ...any) []primitive.M
- func Match(v any) primitive.M
- func ParseObjectID(id string) *primitive.ObjectID
- func Regex(pattern string, opt string) primitive.Regex
- func RegexFor(k string, pattern string, opt string) primitive.M
- func Set(v any) primitive.M
- func SetNested(k string, v any) primitive.M
- func TxOption() *options.TransactionOptions
- type BaseModel
- func (BaseModel) Cleanup()
- func (BaseModel) Collection() *mongo.Collection
- func (model BaseModel) GetID() primitive.ObjectID
- func (BaseModel) Indexes() []mongo.IndexModel
- func (BaseModel) IsDeletable() bool
- func (BaseModel) IsEditable() bool
- func (model *BaseModel) NewId()
- func (*BaseModel) OnDelete(ctx context.Context)
- func (BaseModel) OnDeleted(ctx context.Context)
- func (*BaseModel) OnInsert(ctx context.Context)
- func (BaseModel) OnInserted(ctx context.Context)
- func (*BaseModel) OnUpdate(ctx context.Context)
- func (BaseModel) OnUpdated(old any, ctx context.Context)
- func (BaseModel) Pipeline() MongoPipeline
- func (model *BaseModel) PrepareInsert()
- func (model *BaseModel) PrepareUpdate(ghost bool)
- func (model *BaseModel) SetID(id primitive.ObjectID)
- type EmptyModel
- func (EmptyModel) Cleanup()
- func (EmptyModel) Collection() *mongo.Collection
- func (EmptyModel) GetID() primitive.ObjectID
- func (EmptyModel) Indexes() []mongo.IndexModel
- func (EmptyModel) IsDeletable() bool
- func (EmptyModel) IsEditable() bool
- func (*EmptyModel) NewId()
- func (*EmptyModel) OnDelete(ctx context.Context)
- func (EmptyModel) OnDeleted(ctx context.Context)
- func (*EmptyModel) OnInsert(ctx context.Context)
- func (EmptyModel) OnInserted(ctx context.Context)
- func (*EmptyModel) OnUpdate(ctx context.Context)
- func (EmptyModel) OnUpdated(old any, ctx context.Context)
- func (EmptyModel) Pipeline() MongoPipeline
- func (*EmptyModel) PrepareInsert()
- func (*EmptyModel) PrepareUpdate(ghost bool)
- func (*EmptyModel) SetID(id primitive.ObjectID)
- type MetaCounter
- type MetaCounterResult
- type MetaSetter
- type MetaSetterResult
- type Model
- type MongoDoc
- type MongoPipeline
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AggregateOption ¶ added in v1.1.0
func AggregateOption() *options.AggregateOptions
AggregateOption generate aggregation options
func Doc ¶ added in v1.1.6
Doc generate primitive.D from args
Args count must even Example: Doc("_id", 1, "name", "John")
func FindOption ¶ added in v1.1.0
func FindOption(sort any, skip int64, limit int64) *options.FindOptions
FindOption generate find option with sorts params
func IsValidObjectId ¶
IsValidObjectId check if object id is valid and not zero
func ParseObjectID ¶
ParseObjectID parse object id from string
func RegexFor ¶ added in v1.1.6
RegexFor generate map with regex parameter
{ "name": { pattern: "John.*", options: "i" } }
func TxOption ¶ added in v1.1.8
func TxOption() *options.TransactionOptions
TxOption generate transaction option with majority write and snapshot read
Types ¶
type BaseModel ¶ added in v1.3.0
type BaseModel struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt *time.Time `bson:"updated_at" json:"updated_at"`
}
BaseModel implementation with id and timestamp
func (BaseModel) Collection ¶ added in v1.3.0
func (BaseModel) Collection() *mongo.Collection
func (BaseModel) Indexes ¶ added in v1.3.3
func (BaseModel) Indexes() []mongo.IndexModel
Indexes get model indexes
func (BaseModel) IsDeletable ¶ added in v1.3.0
func (BaseModel) IsEditable ¶ added in v1.3.0
func (BaseModel) OnInserted ¶ added in v1.3.0
func (BaseModel) Pipeline ¶ added in v1.3.0
func (BaseModel) Pipeline() MongoPipeline
func (*BaseModel) PrepareInsert ¶ added in v1.3.0
func (model *BaseModel) PrepareInsert()
func (*BaseModel) PrepareUpdate ¶ added in v1.3.0
type EmptyModel ¶ added in v1.3.1
type EmptyModel struct{}
EmptyModel only implement model methods
func (EmptyModel) Cleanup ¶ added in v1.3.1
func (EmptyModel) Cleanup()
func (EmptyModel) Collection ¶ added in v1.3.1
func (EmptyModel) Collection() *mongo.Collection
func (EmptyModel) GetID ¶ added in v1.3.2
func (EmptyModel) GetID() primitive.ObjectID
func (EmptyModel) Indexes ¶ added in v1.3.3
func (EmptyModel) Indexes() []mongo.IndexModel
Indexes get model indexes
func (EmptyModel) IsDeletable ¶ added in v1.3.1
func (EmptyModel) IsDeletable() bool
func (EmptyModel) IsEditable ¶ added in v1.3.1
func (EmptyModel) IsEditable() bool
func (*EmptyModel) NewId ¶ added in v1.3.1
func (*EmptyModel) NewId()
func (*EmptyModel) OnDelete ¶ added in v1.3.1
func (*EmptyModel) OnDelete(ctx context.Context)
func (EmptyModel) OnDeleted ¶ added in v1.3.1
func (EmptyModel) OnDeleted(ctx context.Context)
func (*EmptyModel) OnInsert ¶ added in v1.3.1
func (*EmptyModel) OnInsert(ctx context.Context)
func (EmptyModel) OnInserted ¶ added in v1.3.1
func (EmptyModel) OnInserted(ctx context.Context)
func (*EmptyModel) OnUpdate ¶ added in v1.3.1
func (*EmptyModel) OnUpdate(ctx context.Context)
func (EmptyModel) OnUpdated ¶ added in v1.3.1
func (EmptyModel) OnUpdated(old any, ctx context.Context)
func (EmptyModel) Pipeline ¶ added in v1.3.1
func (EmptyModel) Pipeline() MongoPipeline
func (*EmptyModel) PrepareInsert ¶ added in v1.3.1
func (*EmptyModel) PrepareInsert()
func (*EmptyModel) PrepareUpdate ¶ added in v1.3.1
func (*EmptyModel) PrepareUpdate(ghost bool)
func (*EmptyModel) SetID ¶ added in v1.3.1
func (*EmptyModel) SetID(id primitive.ObjectID)
type MetaCounter ¶ added in v1.2.4
type MetaCounter interface {
// Add new meta
Add(_col, _meta string, id *primitive.ObjectID, amount int) MetaCounter
// Build get combined meta with query
Build() []MetaCounterResult
}
func NewMetaCounter ¶ added in v1.2.0
func NewMetaCounter() MetaCounter
NewMetaCounter new mongo meta counter
type MetaCounterResult ¶ added in v1.2.4
type MetaSetter ¶ added in v1.2.4
type MetaSetter interface {
// Add new meta
Add(_col, _meta string, id *primitive.ObjectID, value any) MetaSetter
// Build get combined meta with query
Build() []MetaSetterResult
}
func NewMetaSetter ¶ added in v1.2.4
func NewMetaSetter() MetaSetter
NewMetaSetter new mongo meta setter
type MetaSetterResult ¶ added in v1.2.4
type Model ¶ added in v1.1.0
type Model interface {
// Collection get model collection
Collection() *mongo.Collection
// Indexes get model indexes
Indexes() []mongo.IndexModel
// Pipeline get model pipeline
Pipeline() MongoPipeline
// NewId generate new id for model (Pointer)
NewId()
// SetID set model id (Pointer)
SetID(id primitive.ObjectID)
// ID get model id
GetID() primitive.ObjectID
// IsEditable check if document is editable
// by default returns true on BaseModel
IsEditable() bool
// IsDeletable check if document is deletable
// by default returns false on BaseModel
IsDeletable() bool
// Cleanup document before save
// e.g set document field nil for ignore saving
Cleanup()
// PrepareInsert fill created_at before save (Pointer)
PrepareInsert()
// PrepareUpdate fill updated_at before save (Pointer)
// in ghost mode updated_at field not changed
PrepareUpdate(ghost bool)
// OnInsert function to call before insert (Pointer)
OnInsert(ctx context.Context)
// OnUpdate function to call before update (Pointer)
OnUpdate(ctx context.Context)
// OnDelete function to call before delete (Pointer)
OnDelete(ctx context.Context)
// OnInserted function to call after insert
OnInserted(ctx context.Context)
// OnUpdated function to call after update
OnUpdated(old any, ctx context.Context)
// OnDeleted function to call after delete
OnDeleted(ctx context.Context)
}
model interface
type MongoDoc ¶
type MongoDoc interface {
// Add add new element
Add(k string, v any) MongoDoc
// Doc add new element with nested doc value
Doc(k string, cb func(d MongoDoc) MongoDoc) MongoDoc
// Array add new element with array value
Array(k string, v ...any) MongoDoc
// DocArray add new array element with doc
DocArray(k string, cb func(d MongoDoc) MongoDoc) MongoDoc
// Nested add new nested element
Nested(root string, k string, v any) MongoDoc
// NestedDoc add new nested element with doc value
NestedDoc(root string, k string, cb func(d MongoDoc) MongoDoc) MongoDoc
// NestedArray add new nested element with array value
NestedArray(root string, k string, v ...any) MongoDoc
// NestedDocArray add new nested array element with doc
NestedDocArray(root string, k string, cb func(d MongoDoc) MongoDoc) MongoDoc
// Regex add new element with regex value
Regex(k string, pattern string, opt string) MongoDoc
// Map creates a map from the elements of the Doc
Map() primitive.M
// Build generate mongo doc
Build() primitive.D
}
MongoDoc mongo document (primitive.D) builder
type MongoPipeline ¶
type MongoPipeline interface {
// Add add new Doc
Add(cb func(d MongoDoc) MongoDoc) MongoPipeline
// Match add $match stage. skip nil input
Match(filters any) MongoPipeline
// In add $in stage
In(key string, v any) MongoPipeline
// Limit add $limit stage (ignore negative and zero value)
Limit(limit int64) MongoPipeline
// Skip add $skip stage (ignore negative and zero value)
Skip(skip int64) MongoPipeline
// Sort add $sort stage (ignore nil value)
Sort(sorts any) MongoPipeline
// Unwind add $unwind stage
Unwind(path string, prevNullAndEmpty bool) MongoPipeline
// Lookup add $lookup stage
Lookup(from string, local string, foreign string, as string) MongoPipeline
// Unwrap get first item of array and insert to doc using $addFields stage
Unwrap(field string, as string) MongoPipeline
// LoadRelation load related document using $lookup and $addField
LoadRelation(from string, local string, foreign string, as string) MongoPipeline
// Group add $group stage
Group(cb func(d MongoDoc) MongoDoc) MongoPipeline
// ReplaceRoot add $replaceRoot stage
ReplaceRoot(v any) MongoPipeline
// MergeRoot add $replaceRoot stage with $mergeObjects operator
MergeRoot(fields ...any) MongoPipeline
// UnProject generate $project stage to remove fields from result
UnProject(fields ...string) MongoPipeline
// Project add $project stage. skip nil input
Project(projects any) MongoPipeline
// Build generate mongo pipeline
Build() mongo.Pipeline
}
MongoPipeline mongo pipeline (mongo.Pipeline) builder