Documentation ¶
Index ¶
- Variables
- func BuildWriteModelList(filterList []interface{}, getUpdateFn func(filter interface{}) interface{}) []mongo.WriteModel
- func BuildWriteModelListWithObjectId(dataList map[primitive.ObjectID]interface{}) []mongo.WriteModel
- func CreateContext(c *Configuration) (context.Context, context.CancelFunc)
- func EnableMongodbMonitor() func(*options.ClientOptions)
- func FindAllT[T any](repository IRepository, opts ...FindOption) ([]T, error)
- func FindOneTByFilter[T any](repository IRepository, filter interface{}, opts ...FindOneOption) (*T, error)
- func FindTByFilter[T any](repository IRepository, filter interface{}, opts ...FindOption) ([]T, error)
- func FindTByObjectId[T any](repository IRepository, id primitive.ObjectID) (*T, error)
- func GetClient(key string) *mongo.Client
- func GetCollection(databaseName string, collectionName string, opts ...*options.CollectionOptions) *mongo.Collection
- func GetCollectionByKey(key string, databaseName string, collectionName string, ...) *mongo.Collection
- func GetDatabase(databaseName string, opts ...*options.DatabaseOptions) *mongo.Database
- func GetDatabaseByKey(key string, databaseName string, opts ...*options.DatabaseOptions) *mongo.Database
- func Ping(client *mongo.Client) error
- func RegistClient(key string, uri string, opts ...func(*options.ClientOptions)) (*mongo.Client, error)
- func RepositoryOptionWithClientKey(clientKey string) func(*NewRepositoryOption)
- func SetupDefaultClient(uri string, opts ...func(*options.ClientOptions)) (*mongo.Client, error)
- func Validate(v interface{}) error
- type AggregateOption
- type AuditedEntity
- type Configuration
- type CreationAuditedEntity
- type Entity
- type EntityIndexDefine
- type EntityOption
- type EntityUpdate
- type FindOneOption
- type FindOption
- type ICreationAuditedEntity
- type IEntity
- type IEntityBeforeCreate
- type IEntityBeforeUpdate
- type IEntityBulkWrite
- type IEntityCreate
- type IEntityDelete
- type IEntityFind
- type IEntityIndex
- type IEntityUpdate
- type IFindResult
- type IModificationEntity
- type IRepository
- type IValidation
- type IndexFieldDefine
- type MongoCol
- func (c *MongoCol) BulkWrite(models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
- func (c *MongoCol) BulkWriteEntityList(entityList []IEntity, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
- func (r *MongoCol) CountAll() (count int64, err error)
- func (r *MongoCol) CountByFilter(filter interface{}) (int64, error)
- func (r *MongoCol) CreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)
- func (r *MongoCol) CreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)
- func (r *MongoCol) DeleteAllIndexes() (err error)
- func (r *MongoCol) DeleteIndex(name string) (err error)
- func (r *MongoCol) Distinct(fieldName string, filter interface{}) ([]interface{}, error)
- func (r *MongoCol) FindAll(opts ...FindOption) IFindResult
- func (r *MongoCol) FindByFilter(filter interface{}, opts ...FindOption) IFindResult
- func (r *MongoCol) FindByObjectId(id primitive.ObjectID) IFindResult
- func (r *MongoCol) FindOne(filter interface{}, opts ...FindOneOption) IFindResult
- func (r *MongoCol) FindOneAndUpdate(entity IEntity, opts ...*options.FindOneAndUpdateOptions) error
- func (r *MongoCol) FindOneAndUpdateWithId(objectId primitive.ObjectID, update interface{}, ...) error
- func (r *MongoCol) ListIndexes() (indexes []map[string]interface{}, err error)
- func (r *MongoCol) MustCreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions)
- func (r *MongoCol) MustCreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions)
- func (r *MongoCol) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (interface{}, error)
- func (r *MongoCol) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) error
- type NewRepositoryOption
- type RepositoryBase
- func (r *RepositoryBase) Aggregate(pipeline interface{}, dataList interface{}, opts ...AggregateOption) (err error)
- func (r *RepositoryBase) Create(item interface{}, opts ...*options.InsertOneOptions) (id primitive.ObjectID, err error)
- func (r *RepositoryBase) CreateMany(itemList []interface{}, opts ...*options.InsertManyOptions) (ids []primitive.ObjectID, err error)
- func (r *RepositoryBase) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (r *RepositoryBase) DeleteOne(id primitive.ObjectID, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (r *RepositoryBase) DeleteOneByFilter(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (r *RepositoryBase) GetCollection() (c *mongo.Collection)
- func (r *RepositoryBase) GetName() (name string)
- func (r *RepositoryBase) Replace(filter interface{}, doc interface{}, opts ...*options.ReplaceOptions) (err error)
- func (r *RepositoryBase) ReplaceById(id primitive.ObjectID, doc interface{}, opts ...*options.ReplaceOptions) (err error)
- type RepositoryOption
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidType = errors.New("invalid type") ErrNoCursor = errors.New("no cursor") )
View Source
var ( DefaultConfiguration = NewConfiguration() //默认的client DefaultClient *mongo.Client )
Functions ¶
func BuildWriteModelList ¶
func BuildWriteModelList(filterList []interface{}, getUpdateFn func(filter interface{}) interface{}) []mongo.WriteModel
build mongo.WriteModel list with ObjectId list
func BuildWriteModelListWithObjectId ¶
func BuildWriteModelListWithObjectId(dataList map[primitive.ObjectID]interface{}) []mongo.WriteModel
build mongo.WriteModel list with ObjectId list
func CreateContext ¶
func CreateContext(c *Configuration) (context.Context, context.CancelFunc)
func EnableMongodbMonitor ¶
func EnableMongodbMonitor() func(*options.ClientOptions)
enable mongodb monitor
func FindAllT ¶
func FindAllT[T any](repository IRepository, opts ...FindOption) ([]T, error)
find all t
func FindOneTByFilter ¶
func FindOneTByFilter[T any](repository IRepository, filter interface{}, opts ...FindOneOption) (*T, error)
find one by _id
func FindTByFilter ¶
func FindTByFilter[T any](repository IRepository, filter interface{}, opts ...FindOption) ([]T, error)
find t by filter
func FindTByObjectId ¶
func FindTByObjectId[T any](repository IRepository, id primitive.ObjectID) (*T, error)
find t by _id
func GetCollection ¶
func GetCollection(databaseName string, collectionName string, opts ...*options.CollectionOptions) *mongo.Collection
get mongo.Collection instanc
func GetCollectionByKey ¶
func GetCollectionByKey(key string, databaseName string, collectionName string, opts ...*options.CollectionOptions) *mongo.Collection
func GetDatabase ¶
func GetDatabase(databaseName string, opts ...*options.DatabaseOptions) *mongo.Database
get mongo.Database instance
func GetDatabaseByKey ¶
func GetDatabaseByKey(key string, databaseName string, opts ...*options.DatabaseOptions) *mongo.Database
get mongo.Database instance
func RegistClient ¶
func RepositoryOptionWithClientKey ¶
func RepositoryOptionWithClientKey(clientKey string) func(*NewRepositoryOption)
specifiy repository with client key
func SetupDefaultClient ¶
构建默认的client
Types ¶
type AggregateOption ¶
type AggregateOption func(*options.AggregateOptions)
AggregateOptions handler pipeline
type AuditedEntity ¶
type AuditedEntity struct { CreationAuditedEntity `bson:",inline"` //last modification time LastModificationTime *time.Time `json:"lastModificationTime,omitempty" bson:"lastModificationTime"` //last modification user LastModifierId string `json:"lastModifierId,omitempty" bson:"lastModifierId"` }
auditable entity
func (*AuditedEntity) BeforeUpdate ¶
func (entity *AuditedEntity) BeforeUpdate()
func (*AuditedEntity) GetLastModificationTime ¶
func (e *AuditedEntity) GetLastModificationTime() *time.Time
func (*AuditedEntity) GetLastModifierId ¶
func (e *AuditedEntity) GetLastModifierId() string
func (AuditedEntity) GetObjectId ¶
func (e AuditedEntity) GetObjectId() primitive.ObjectID
type Configuration ¶
func NewConfiguration ¶
func NewConfiguration() *Configuration
type CreationAuditedEntity ¶
type CreationAuditedEntity struct { Entity `bson:",inline"` //create time CreationTime time.Time `json:"creationTime,omitempty" bson:"creationTime" ` //create user CreatorId string `json:"creatorId,omitempty" bson:"creatorId"` }
can audit creation entity
func (*CreationAuditedEntity) BeforeCreate ¶
func (entity *CreationAuditedEntity) BeforeCreate()
func (*CreationAuditedEntity) GetCreationTime ¶
func (e *CreationAuditedEntity) GetCreationTime() time.Time
func (*CreationAuditedEntity) GetCreatorId ¶
func (e *CreationAuditedEntity) GetCreatorId() string
type Entity ¶
func (*Entity) GetObjectId ¶
type EntityIndexDefine ¶
type EntityIndexDefine struct {
FieldList []IndexFieldDefine
}
index model
func NewEntityIndexDefine ¶
func NewEntityIndexDefine() *EntityIndexDefine
func (*EntityIndexDefine) AddField ¶
func (d *EntityIndexDefine) AddField(fieldName string, isAsc bool) *EntityIndexDefine
func (*EntityIndexDefine) ToIndexModel ¶
func (d *EntityIndexDefine) ToIndexModel() *mongo.IndexModel
type EntityUpdate ¶
type EntityUpdate struct { }
type FindOneOption ¶
type FindOneOption func(*options.FindOneOptions)
type FindOption ¶
type FindOption func(*options.FindOptions)
func FindOptionWithLimit ¶
func FindOptionWithLimit(limit int64) FindOption
func FindOptionWithPage ¶
func FindOptionWithPage(pageIndex int64, pageSize int64) FindOption
func FindOptionWithSkip ¶
func FindOptionWithSkip(skip int64) FindOption
func FindOptionWithSort ¶
func FindOptionWithSort(sort bson.D) FindOption
type ICreationAuditedEntity ¶
type IEntityBeforeCreate ¶
type IEntityBeforeCreate interface {
BeforeCreate()
}
type IEntityBeforeUpdate ¶
type IEntityBeforeUpdate interface {
BeforeUpdate()
}
type IEntityBulkWrite ¶
type IEntityBulkWrite interface { BulkWrite(models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) BulkWriteEntityList(entityList []IEntity, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) }
type IEntityCreate ¶
type IEntityCreate interface { // create Create(data interface{}, opts ...*options.InsertOneOptions) (id primitive.ObjectID, err error) CreateMany(itemList []interface{}, opts ...*options.InsertManyOptions) (ids []primitive.ObjectID, err error) }
type IEntityDelete ¶
type IEntityDelete interface { // delete DeleteOne(id primitive.ObjectID, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteOneByFilter(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) }
type IEntityFind ¶
type IEntityFind interface { CountByFilter(filter interface{}) (count int64, err error) CountAll() (count int64, err error) // find FindAll(opts ...FindOption) IFindResult FindByObjectId(id primitive.ObjectID) IFindResult FindOne(filter interface{}, opts ...FindOneOption) IFindResult FindByFilter(filter interface{}, opts ...FindOption) IFindResult Distinct(fieldName string, filter interface{}) ([]interface{}, error) }
type IEntityIndex ¶
type IEntityIndex interface { // index CreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) CreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) MustCreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions) MustCreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions) DeleteIndex(name string) (err error) DeleteAllIndexes() (err error) ListIndexes() (indexes []map[string]interface{}, err error) }
type IEntityUpdate ¶
type IEntityUpdate interface { FindOneAndUpdate(entity IEntity, opts ...*options.FindOneAndUpdateOptions) error FindOneAndUpdateWithId(objectId primitive.ObjectID, update interface{}, opts ...*options.FindOneAndUpdateOptions) error UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) error UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (interface{}, error) }
update
type IFindResult ¶
type IModificationEntity ¶
type IRepository ¶
type IRepository interface { IEntityFind IEntityCreate IEntityUpdate IEntityDelete IEntityIndex IEntityBulkWrite // aggregate Aggregate(pipeline interface{}, dataList interface{}, opts ...AggregateOption) (err error) // replace* ReplaceById(id primitive.ObjectID, doc interface{}, opts ...*options.ReplaceOptions) (err error) Replace(filter interface{}, doc interface{}, opts ...*options.ReplaceOptions) (err error) GetName() (name string) GetCollection() (c *mongo.Collection) }
一个抽象的用来处理任意类型的mongodb的仓储基类
type IValidation ¶
type IValidation interface {
Validate() error
}
type IndexFieldDefine ¶
type MongoCol ¶
type MongoCol struct {
// contains filtered or unexported fields
}
func NewMongoCol ¶
func NewMongoCol(col *mongo.Collection, opts ...*Configuration) *MongoCol
new MongoCol instance, panic if col is nil
func (*MongoCol) BulkWrite ¶
func (c *MongoCol) BulkWrite(models []mongo.WriteModel, opts ...*options.BulkWriteOptions) ( *mongo.BulkWriteResult, error)
func (*MongoCol) BulkWriteEntityList ¶
func (c *MongoCol) BulkWriteEntityList(entityList []IEntity, opts ...*options.BulkWriteOptions) ( *mongo.BulkWriteResult, error)
func (*MongoCol) CountByFilter ¶
func (*MongoCol) CreateIndex ¶
func (r *MongoCol) CreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)
func (*MongoCol) CreateIndexes ¶
func (r *MongoCol) CreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)
func (*MongoCol) DeleteAllIndexes ¶
func (*MongoCol) DeleteIndex ¶
func (*MongoCol) FindAll ¶
func (r *MongoCol) FindAll(opts ...FindOption) IFindResult
func (*MongoCol) FindByFilter ¶
func (r *MongoCol) FindByFilter(filter interface{}, opts ...FindOption) IFindResult
根据条件来筛选
func (*MongoCol) FindByObjectId ¶
func (r *MongoCol) FindByObjectId(id primitive.ObjectID) IFindResult
根据_id来查找,返回的是对象的指针
func (*MongoCol) FindOne ¶
func (r *MongoCol) FindOne(filter interface{}, opts ...FindOneOption) IFindResult
查找一条记录
func (*MongoCol) FindOneAndUpdate ¶
func (r *MongoCol) FindOneAndUpdate(entity IEntity, opts ...*options.FindOneAndUpdateOptions) error
func (*MongoCol) FindOneAndUpdateWithId ¶
func (*MongoCol) ListIndexes ¶
func (*MongoCol) MustCreateIndex ¶
func (r *MongoCol) MustCreateIndex(indexModel mongo.IndexModel, opts ...*options.CreateIndexesOptions)
func (*MongoCol) MustCreateIndexes ¶
func (r *MongoCol) MustCreateIndexes(indexModelList []mongo.IndexModel, opts ...*options.CreateIndexesOptions)
func (*MongoCol) UpdateMany ¶
func (r *MongoCol) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (interface{}, error)
type NewRepositoryOption ¶
type NewRepositoryOption struct { DefaultSortField string // contains filtered or unexported fields }
type RepositoryBase ¶
type RepositoryBase struct { *MongoCol // contains filtered or unexported fields }
RepositoryBase represents a mongodb repository
func NewRepository ¶
func NewRepository(databaseName string, collectionName string, opts ...func(*NewRepositoryOption)) (*RepositoryBase, error)
func NewRepositoryBase ¶
func NewRepositoryBase(getDbCollection func() *mongo.Collection, opts ...RepositoryOption) (*RepositoryBase, error)
new一个新的实例
func (*RepositoryBase) Aggregate ¶
func (r *RepositoryBase) Aggregate(pipeline interface{}, dataList interface{}, opts ...AggregateOption) (err error)
aggregate
func (*RepositoryBase) Create ¶
func (r *RepositoryBase) Create(item interface{}, opts ...*options.InsertOneOptions) (id primitive.ObjectID, err error)
func (*RepositoryBase) CreateMany ¶
func (r *RepositoryBase) CreateMany(itemList []interface{}, opts ...*options.InsertManyOptions) (ids []primitive.ObjectID, err error)
func (*RepositoryBase) DeleteMany ¶
func (r *RepositoryBase) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
删除多条记录
func (*RepositoryBase) DeleteOne ¶
func (r *RepositoryBase) DeleteOne(id primitive.ObjectID, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
删除指定id的记录
func (*RepositoryBase) DeleteOneByFilter ¶
func (r *RepositoryBase) DeleteOneByFilter(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
删除指定条件的一条记录
func (*RepositoryBase) GetCollection ¶
func (r *RepositoryBase) GetCollection() (c *mongo.Collection)
func (*RepositoryBase) GetName ¶
func (r *RepositoryBase) GetName() (name string)
func (*RepositoryBase) Replace ¶
func (r *RepositoryBase) Replace(filter interface{}, doc interface{}, opts ...*options.ReplaceOptions) (err error)
func (*RepositoryBase) ReplaceById ¶
func (r *RepositoryBase) ReplaceById(id primitive.ObjectID, doc interface{}, opts ...*options.ReplaceOptions) (err error)
type RepositoryOption ¶
type RepositoryOption func(*Configuration)
func WithCreateItemFunc ¶
func WithCreateItemFunc(createItemFunc func() interface{}) RepositoryOption
func WithDefaultSort ¶
func WithDefaultSort(defaultSortFunc func(*options.FindOptions) *options.FindOptions) RepositoryOption
Source Files ¶
Click to show internal directories.
Click to hide internal directories.