mongodbr

package
v0.0.0-...-418145e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

mongodbr

mongodb repository

repository for mongodb

Documentation

Index

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 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 GetClient

func GetClient(key string) *mongo.Client

get client by key

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 Ping

func Ping(client *mongo.Client) error

func RegistClient

func RegistClient(key string, uri string, opts ...func(*options.ClientOptions)) (*mongo.Client, error)

func RepositoryOptionWithClientKey

func RepositoryOptionWithClientKey(clientKey string) func(*NewRepositoryOption)

specifiy repository with client key

func SetupDefaultClient

func SetupDefaultClient(uri string, opts ...func(*options.ClientOptions)) (*mongo.Client, error)

构建默认的client

func Validate

func Validate(v interface{}) error

validate object if object implement IValidation interface

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

type Configuration struct {
	QueryTimeout time.Duration
	// contains filtered or unexported fields
}

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

type Entity struct {
	ObjectId primitive.ObjectID `json:"objectId,omitempty" bson:"_id"`
}

func (*Entity) BeforeCreate

func (entity *Entity) BeforeCreate()

创建时设置对象的基本信息

func (*Entity) GetObjectId

func (entity *Entity) GetObjectId() primitive.ObjectID

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 EntityOption

type EntityOption = func(e IEntity)

modify IEntity object

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 ICreationAuditedEntity interface {
	GetCreatorId() string
	GetCreationTime() time.Time
}

type IEntity

type IEntity interface {
	GetObjectId() primitive.ObjectID
}

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 IFindResult interface {
	One(val interface{}) (err error)
	ToOne() (interface{}, error)
	All(val interface{}) (err error)
	ToAll() ([]interface{}, error)

	GetSingleResult() (res *mongo.SingleResult)
	GetCursor() (cur *mongo.Cursor)
	GetError() (err error)
}

type IModificationEntity

type IModificationEntity interface {
	GetLastModificationTime() *time.Time
	GetLastModifierId() string
}

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 IndexFieldDefine struct {
	FieldName string
	IsAsc     bool
}

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) CountAll

func (r *MongoCol) CountAll() (count int64, err error)

func (*MongoCol) CountByFilter

func (r *MongoCol) CountByFilter(filter interface{}) (int64, error)

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 (r *MongoCol) DeleteAllIndexes() (err error)

func (*MongoCol) DeleteIndex

func (r *MongoCol) DeleteIndex(name string) (err error)

func (*MongoCol) Distinct

func (r *MongoCol) Distinct(fieldName string, filter interface{}) ([]interface{}, error)

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 (r *MongoCol) FindOneAndUpdateWithId(objectId primitive.ObjectID, update interface{}, opts ...*options.FindOneAndUpdateOptions) error

func (*MongoCol) ListIndexes

func (r *MongoCol) ListIndexes() (indexes []map[string]interface{}, err error)

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)

func (*MongoCol) UpdateOne

func (r *MongoCol) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) 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

删除指定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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL