Documentation
¶
Index ¶
- func AggregateOption() *options.AggregateOptions
- func Array(args ...any) primitive.A
- func BatchUpdate[T any](condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func BatchUpdateCtx[T any](ctx context.Context, condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func Count[T any](filter any, opts ...MongoOption) (int64, error)
- func CountCtx[T any](ctx context.Context, filter any, opts ...MongoOption) (int64, error)
- func CountRaw[T any](filter any, opts ...MongoOption) (int64, error)
- func CountRawCtx[T any](ctx context.Context, pipeline MongoPipeline, opts ...MongoOption) (int64, error)
- func Delete[T any](v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
- func DeleteCtx[T any](ctx context.Context, v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
- func Doc(args ...any) primitive.D
- func FillBackupFields(model any) bool
- func Find[T any](filter any, sorts any, skip int64, limit int64, opts ...MongoOption) ([]T, error)
- func FindCtx[T any](ctx context.Context, filter any, sorts any, skip int64, limit int64, ...) ([]T, error)
- func FindOne[T any](filter any, sorts any, opts ...MongoOption) (*T, error)
- func FindOneCtx[T any](ctx context.Context, filter any, sorts any, opts ...MongoOption) (*T, error)
- func FindOption(sort any, skip int64, limit int64) *options.FindOptions
- func FindRaw[T any](pipeline MongoPipeline, opts ...MongoOption) ([]T, error)
- func FindRawCtx[T any](ctx context.Context, pipeline MongoPipeline, opts ...MongoOption) ([]T, error)
- func In(k string, v ...any) primitive.M
- func Increment[T any](condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func IncrementCtx[T any](ctx context.Context, condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func Insert[T any](v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
- func InsertCtx[T any](ctx context.Context, v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
- 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 ModelHasChanged(old any, model any) bool
- func MongoOperationCtx() (context.Context, context.CancelFunc)
- func ParseObjectID(id string) *primitive.ObjectID
- func Patch[T any](condition any, data primitive.M, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- func PatchCtx[T any](ctx context.Context, condition any, data primitive.M, silent bool, ...) (*mongo.UpdateResult, error)
- 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
- func Update[T any](v *T, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- func UpdateCtx[T any](ctx context.Context, v *T, isSilent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- type Backup
- type BackupModel
- type BaseModel
- func (*BaseModel) Cleanup()
- func (BaseModel) Collection(db *mongo.Database) *mongo.Collection
- func (model *BaseModel) FillCreatedAt()
- func (model *BaseModel) FillUpdatedAt()
- func (model BaseModel) GetID() primitive.ObjectID
- func (BaseModel) Index(db *mongo.Database) error
- func (BaseModel) IsDeletable() bool
- func (BaseModel) IsEditable() bool
- func (model *BaseModel) NewId()
- func (*BaseModel) OnDelete(ctx context.Context, opt ...MongoOption) error
- func (BaseModel) OnDeleted(ctx context.Context, opt ...MongoOption) error
- func (*BaseModel) OnInsert(ctx context.Context, opt ...MongoOption) error
- func (BaseModel) OnInserted(ctx context.Context, opt ...MongoOption) error
- func (*BaseModel) OnUpdate(ctx context.Context, opt ...MongoOption) error
- func (BaseModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption) error
- func (BaseModel) Pipeline() MongoPipeline
- func (BaseModel) Seed(db *mongo.Database) error
- func (model *BaseModel) SetID(id primitive.ObjectID)
- func (BaseModel) TypeName() string
- type Checksum
- type EmptyModel
- func (*EmptyModel) Cleanup()
- func (EmptyModel) Collection(db *mongo.Database) *mongo.Collection
- func (*EmptyModel) FillCreatedAt()
- func (*EmptyModel) FillUpdatedAt()
- func (model EmptyModel) GetID() primitive.ObjectID
- func (EmptyModel) Index(db *mongo.Database) error
- func (EmptyModel) IsDeletable() bool
- func (EmptyModel) IsEditable() bool
- func (model *EmptyModel) NewId()
- func (*EmptyModel) OnDelete(ctx context.Context, opt ...MongoOption) error
- func (EmptyModel) OnDeleted(ctx context.Context, opt ...MongoOption) error
- func (*EmptyModel) OnInsert(ctx context.Context, opt ...MongoOption) error
- func (EmptyModel) OnInserted(ctx context.Context, opt ...MongoOption) error
- func (*EmptyModel) OnUpdate(ctx context.Context, opt ...MongoOption) error
- func (EmptyModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption) error
- func (EmptyModel) Pipeline() MongoPipeline
- func (EmptyModel) Seed(db *mongo.Database) error
- func (model *EmptyModel) SetID(id primitive.ObjectID)
- func (EmptyModel) TypeName() string
- type IrCity
- func (ic *IrCity) Cleanup()
- func (*IrCity) Collection(db *mongo.Database) *mongo.Collection
- func (ic *IrCity) GetID() primitive.ObjectID
- func (ic *IrCity) Index(db *mongo.Database) error
- func (*IrCity) IsEditable() bool
- func (ic *IrCity) NewId()
- func (i *IrCity) Seed(db *mongo.Database) error
- func (ic *IrCity) SetID(id primitive.ObjectID)
- func (*IrCity) SinglePipeline() MongoPipeline
- func (*IrCity) TypeName() string
- type MetaCounter
- type MetaCounterResult
- type MetaSetter
- type MetaSetterResult
- type Model
- type MongoDoc
- type MongoOption
- type MongoPipeline
- type SchemaModel
- type SchemaVersioning
- type SoftDelete
- type SoftDeleteModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AggregateOption ¶
func AggregateOption() *options.AggregateOptions
AggregateOption generate aggregation options
func BatchUpdate ¶
func BatchUpdate[T any](condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
func BatchUpdateCtx ¶
func BatchUpdateCtx[T any]( ctx context.Context, condition any, updates any, opts ...MongoOption, ) (*mongo.UpdateResult, error)
BatchUpdate update multiple records
@param ctx operation context @param condition update condition @param updates update value @opts operation option
func CountCtx ¶
Count get records count
@param ctx operation context @param filter (ignored on nil) @opts operation option
func CountRaw ¶ added in v1.2.0
func CountRaw[T any](filter any, opts ...MongoOption) (int64, error)
func CountRawCtx ¶ added in v1.2.0
func CountRawCtx[T any]( ctx context.Context, pipeline MongoPipeline, opts ...MongoOption, ) (int64, error)
CountRaw get records count option Pipeline not effected
@param ctx operation context @param filter (ignored on nil) @opts operation option
func Delete ¶
func Delete[T any](v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
func DeleteCtx ¶
func DeleteCtx[T any]( ctx context.Context, v *T, opts ...MongoOption, ) (*mongo.DeleteResult, error)
Delete delete record
@param ctx operation context @param v model @opts operation option
func Doc ¶
Doc generate primitive.D from args
Args count must even Example: Doc("_id", 1, "name", "John")
func FillBackupFields ¶ added in v1.1.0
FillBackupFields fill backup related fields of backup model
func FindCtx ¶
func FindCtx[T any]( ctx context.Context, filter any, sorts any, skip int64, limit int64, opts ...MongoOption, ) ([]T, error)
Find find records
@param ctx operation context @param filter (ignored on nil) @param sorts (ignored on nil) @param skip (ignored on 0) @param limit (ignored on 0) @opts operation option
func FindOneCtx ¶
func FindOneCtx[T any]( ctx context.Context, filter any, sorts any, opts ...MongoOption, ) (*T, error)
FindOne find one record
@param ctx operation context @param filter (ignored on nil) @param sorts (ignored on nil) @opts operation option
func FindOption ¶
func FindOption(sort any, skip int64, limit int64) *options.FindOptions
FindOption generate find option with sorts params
func FindRaw ¶ added in v1.2.0
func FindRaw[T any](pipeline MongoPipeline, opts ...MongoOption) ([]T, error)
func FindRawCtx ¶ added in v1.2.0
func FindRawCtx[T any]( ctx context.Context, pipeline MongoPipeline, opts ...MongoOption, ) ([]T, error)
FindRaw find records from pipeline option pipeline not effected
@param ctx operation context @param pipeline aggregation pipeline @opts operation option
func Increment ¶
func Increment[T any](condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
func IncrementCtx ¶
func IncrementCtx[T any]( ctx context.Context, condition any, data any, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Increment increment numeric data pass negative value for decrement increment run on silent mode
@param ctx operation context @param condition update condition @param data update value @opts operation option
func Insert ¶
func Insert[T any](v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
func InsertCtx ¶
func InsertCtx[T any]( ctx context.Context, v *T, opts ...MongoOption, ) (*mongo.InsertOneResult, error)
Insert insert new record this function use FindOne to find old record
@param ctx operation context @param v model @opts operation option
func IsValidObjectId ¶
IsValidObjectId check if object id is valid and not zero
func ModelHasChanged ¶ added in v1.1.0
ModelHasChanged check if backup model fields changed
func MongoOperationCtx ¶
func MongoOperationCtx() (context.Context, context.CancelFunc)
MongoOperationCtx create context for mongo db operations for 10 sec
func ParseObjectID ¶
ParseObjectID parse object id from string
func Patch ¶
func Patch[T any](condition any, data primitive.M, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
func PatchCtx ¶
func PatchCtx[T any]( ctx context.Context, condition any, data primitive.M, silent bool, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Patch partial update multiple records using $set
@param ctx operation context @param condition update condition @param data update value @param silent disable update meta (updated_at) @opts operation option
func RegexFor ¶
RegexFor generate map with regex parameter
{ "name": { pattern: "John.*", options: "i" } }
func TxOption ¶
func TxOption() *options.TransactionOptions
TxOption generate transaction option with majority write and snapshot read
func Update ¶
func Update[T any](v *T, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
func UpdateCtx ¶
func UpdateCtx[T any]( ctx context.Context, v *T, isSilent bool, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Update update one record
@param ctx operation context @param v model @param isSilent disable update meta (updated_at) @opts operation option
Types ¶
type Backup ¶ added in v1.0.4
type Backup interface {
// ToMap get model as map for backup
// return nil or empty map to skip backup
ToMap() map[string]any
// SetChecksum set model md5 checksum
SetChecksum(string)
// GetChecksum get model md5 checksum
GetChecksum() string
// NeedBackup check if record need backup
NeedBackup() bool
// MarkBackup set backup state to current date
MarkBackup()
// UnMarkBackup set backup state to nil
UnMarkBackup()
}
type BackupModel ¶ added in v1.0.4
type BackupModel struct {
Checksum string `bson:"checksum" json:"-"`
LastBackup *time.Time `bson:"last_backup" json:"-"`
}
BackupModel backup implementation
func (BackupModel) GetChecksum ¶ added in v1.0.4
func (model BackupModel) GetChecksum() string
func (*BackupModel) MarkBackup ¶ added in v1.0.4
func (model *BackupModel) MarkBackup()
func (BackupModel) NeedBackup ¶ added in v1.0.4
func (model BackupModel) NeedBackup() bool
func (*BackupModel) SetChecksum ¶ added in v1.0.4
func (model *BackupModel) SetChecksum(v string)
func (*BackupModel) UnMarkBackup ¶ added in v1.0.4
func (model *BackupModel) UnMarkBackup()
type BaseModel ¶
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 ¶
func (BaseModel) Collection(db *mongo.Database) *mongo.Collection
func (*BaseModel) FillCreatedAt ¶ added in v1.1.0
func (model *BaseModel) FillCreatedAt()
func (*BaseModel) FillUpdatedAt ¶ added in v1.1.0
func (model *BaseModel) FillUpdatedAt()
func (BaseModel) IsDeletable ¶
func (BaseModel) IsEditable ¶
func (*BaseModel) OnDelete ¶
func (*BaseModel) OnDelete(ctx context.Context, opt ...MongoOption) error
func (BaseModel) OnDeleted ¶
func (BaseModel) OnDeleted(ctx context.Context, opt ...MongoOption) error
func (*BaseModel) OnInsert ¶
func (*BaseModel) OnInsert(ctx context.Context, opt ...MongoOption) error
func (BaseModel) OnInserted ¶
func (BaseModel) OnInserted(ctx context.Context, opt ...MongoOption) error
func (*BaseModel) OnUpdate ¶
func (*BaseModel) OnUpdate(ctx context.Context, opt ...MongoOption) error
func (BaseModel) Pipeline ¶
func (BaseModel) Pipeline() MongoPipeline
type Checksum ¶ added in v1.0.4
type Checksum struct {
// contains filtered or unexported fields
}
func NewChecksum ¶ added in v1.0.4
type EmptyModel ¶
EmptyModel only implement model methods
func (*EmptyModel) Cleanup ¶
func (*EmptyModel) Cleanup()
func (EmptyModel) Collection ¶
func (EmptyModel) Collection(db *mongo.Database) *mongo.Collection
func (*EmptyModel) FillCreatedAt ¶ added in v1.1.0
func (*EmptyModel) FillCreatedAt()
func (*EmptyModel) FillUpdatedAt ¶ added in v1.1.0
func (*EmptyModel) FillUpdatedAt()
func (EmptyModel) GetID ¶
func (model EmptyModel) GetID() primitive.ObjectID
func (EmptyModel) IsDeletable ¶
func (EmptyModel) IsDeletable() bool
func (EmptyModel) IsEditable ¶
func (EmptyModel) IsEditable() bool
func (*EmptyModel) NewId ¶
func (model *EmptyModel) NewId()
func (*EmptyModel) OnDelete ¶
func (*EmptyModel) OnDelete(ctx context.Context, opt ...MongoOption) error
func (EmptyModel) OnDeleted ¶
func (EmptyModel) OnDeleted(ctx context.Context, opt ...MongoOption) error
func (*EmptyModel) OnInsert ¶
func (*EmptyModel) OnInsert(ctx context.Context, opt ...MongoOption) error
func (EmptyModel) OnInserted ¶
func (EmptyModel) OnInserted(ctx context.Context, opt ...MongoOption) error
func (*EmptyModel) OnUpdate ¶
func (*EmptyModel) OnUpdate(ctx context.Context, opt ...MongoOption) error
func (EmptyModel) OnUpdated ¶
func (EmptyModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption) error
func (EmptyModel) Pipeline ¶
func (EmptyModel) Pipeline() MongoPipeline
func (*EmptyModel) SetID ¶
func (model *EmptyModel) SetID(id primitive.ObjectID)
func (EmptyModel) TypeName ¶
func (EmptyModel) TypeName() string
type IrCity ¶
type IrCity struct {
EmptyModel `bson:"inline"`
Code uint `bson:"code" json:"code"`
Name string `bson:"name" json:"name"`
CountyId *primitive.ObjectID `bson:"county_id" json:"county_id"`
ProvinceId *primitive.ObjectID `bson:"province_id" json:"province_id"`
County *IrCity `bson:"county,omitempty" json:"county"`
Province *IrCity `bson:"province,omitempty" json:"province"`
}
IrCity only implement model methods
func (*IrCity) Collection ¶
func (*IrCity) Collection(db *mongo.Database) *mongo.Collection
func (*IrCity) IsEditable ¶
func (*IrCity) SinglePipeline ¶
func (*IrCity) SinglePipeline() MongoPipeline
type MetaCounter ¶
type MetaCounter interface {
// Add increase meta amount
Add(_col, _meta string, id *primitive.ObjectID, amount int64) MetaCounter
// Sub decrease meta amount
Sub(_col, _meta string, id *primitive.ObjectID, amount int64) MetaCounter
// Build get combined meta with query
Build() []MetaCounterResult
}
type MetaCounterResult ¶
type MetaSetter ¶
type MetaSetter interface {
// Add new meta
Add(_col, _meta string, id *primitive.ObjectID, value any) MetaSetter
// Build get combined meta with query
Build() []MetaSetterResult
}
type MetaSetterResult ¶
type Model ¶
type Model interface {
// TypeName get type string
TypeName() string
// Collection get model collection
Collection(db *mongo.Database) *mongo.Collection
// Indexes create model indexes
Index(db *mongo.Database) error
// Seed run model seed
Seed(db *mongo.Database) error
// Pipeline get model pipeline
Pipeline() MongoPipeline
// FillCreatedAt fill created_at parameter with current time
FillCreatedAt()
// FillUpdatedAt fill updated_at parameter with current time
FillUpdatedAt()
// NewId generate new id for model
NewId()
// SetID set model id
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()
// OnInsert function to call before insert with repository Insert function
OnInsert(ctx context.Context, opt ...MongoOption) error
// OnUpdate function to call before update with repository Update function
OnUpdate(ctx context.Context, opt ...MongoOption) error
// OnDelete function to call before delete with repository Delete function
OnDelete(ctx context.Context, opt ...MongoOption) error
// OnInserted function to call after insert with repository Insert function
OnInserted(ctx context.Context, opt ...MongoOption) error
// OnUpdated function to call after update with repository Update function
OnUpdated(old any, ctx context.Context, opt ...MongoOption) error
// OnDeleted function to call after delete with repository Delete function
OnDeleted(ctx context.Context, opt ...MongoOption) error
}
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 MongoOption ¶
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
// Deleted generate match for not soft deleted models (deleted_at == nil)
Deleted() MongoPipeline
// Trashes generate match for soft deleted models (deleted_at != nil)
Trashes() MongoPipeline
// NotBackedUp generate match query for not backed up records
NotBackedUp() MongoPipeline
// Build generate mongo pipeline
Build() mongo.Pipeline
}
MongoPipeline mongo pipeline (mongo.Pipeline) builder
type SchemaModel ¶ added in v1.2.1
type SchemaModel struct {
SchemaVersion int `bson:"schema_version" json:"-"`
}
SchemaModel schema versioning field
func (SchemaModel) GetVersion ¶ added in v1.2.2
func (model SchemaModel) GetVersion() int
func (*SchemaModel) SetVersion ¶ added in v1.2.2
func (model *SchemaModel) SetVersion(v int)
type SchemaVersioning ¶ added in v1.2.2
type SoftDelete ¶ added in v1.0.5
type SoftDelete interface {
// SoftDelete set deleted_at field to current date
SoftDelete()
// Restore set deleted_at field to nil
Restore()
// IsDeleted check if item soft deleted
IsDeleted() bool
}
type SoftDeleteModel ¶ added in v1.0.5
func (SoftDeleteModel) IsDeleted ¶ added in v1.0.5
func (model SoftDeleteModel) IsDeleted() bool
func (*SoftDeleteModel) Restore ¶ added in v1.0.5
func (model *SoftDeleteModel) Restore()
func (*SoftDeleteModel) SoftDelete ¶ added in v1.0.5
func (model *SoftDeleteModel) SoftDelete()