mongodb

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SuccessExit            = 0
	FailureExit            = -27017
	ErrParamParseCodeKey   = 27001
	ErrCreateFailedCodeKey = 27002
)
View Source
const (
	DefaultTimeout = time.Second * 10
)

Variables

View Source
var (
	ErrorValidInserID = errors.New("invalid insertID")
	ErrNotFound       = errors.New("Data Not Found")
)

Functions

func CRUD

func CRUD(group *gin.RouterGroup, path string, resource interface{})

It defines

POST: /path
GET:  /path
PUT:  /path/:id
POST: /path/:id

func InitMongoManager

func InitMongoManager(conf MongoConf)

func NewContextWithTimeout

func NewContextWithTimeout(ctx context.Context) (context.Context, context.CancelFunc)

Types

type Base

type Base struct {
	// contains filtered or unexported fields
}

func NewBase

func NewBase(collection Collection) *Base

func (*Base) Count

func (b *Base) Count(ctx context.Context, filter interface{}) (int64, error)

func (*Base) Create

func (b *Base) Create(ctx context.Context, data interface{}) (string, error)

func (*Base) CreateMany

func (b *Base) CreateMany(ctx context.Context, documents []interface{}) ([]string, error)

批量创建, 返回 primitive.ObjectID 列表.

func (*Base) DeleteByFilter

func (b *Base) DeleteByFilter(ctx context.Context, filter interface{}) (int64, error)

func (*Base) DeleteByIds

func (b *Base) DeleteByIds(ctx context.Context, ids []string) (int64, error)

真删除.

func (*Base) DeleteByIdsOrgId

func (b *Base) DeleteByIdsOrgId(ctx context.Context, ids []string, orgId string) (int64, error)

真删除.

func (*Base) DeleteFalseByIds

func (b *Base) DeleteFalseByIds(ctx context.Context, ids []string) (int64, error)

假删除.

func (*Base) Find

func (b *Base) Find(ctx context.Context, filter interface{}, opt PageOrderIntfc, results interface{}) error

查询过滤, 返回所有结果,不分页.

func (*Base) FindByNameWithPage

func (b *Base) FindByNameWithPage(ctx context.Context, name string, pageOrder PageOrderIntfc, results interface{}) (int64, error)

通过Name过滤查询,进行分页.

func (*Base) FindOne

func (b *Base) FindOne(ctx context.Context, filter interface{}, result interface{}) error

返回第一条结果.

func (*Base) FindOneByID

func (b *Base) FindOneByID(ctx context.Context, id string, result interface{}) error

func (*Base) FindWithPage

func (b *Base) FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, results interface{}) (int64, error)

分页查询, 返回总记录个数. results 为切片指针.

func (*Base) IsExist

func (b *Base) IsExist(ctx context.Context, filter interface{}) (bool, error)

是否存在.

func (*Base) IsExistByName

func (b *Base) IsExistByName(ctx context.Context, name string) (bool, error)

func (*Base) ReplaceOneById

func (b *Base) ReplaceOneById(ctx context.Context, id string, data interface{}) error

替换文档,也相当于更新文档

data为model.

func (*Base) UpdateByID

func (b *Base) UpdateByID(ctx context.Context, id string, data interface{}) error

update by id

data 可以为结构体model,支持 bson tag

data 可以为 map[string]interface{}或bson.M.

func (*Base) UpdateMany

func (b *Base) UpdateMany(ctx context.Context, filter interface{}, data interface{}) error

func (*Base) UpdateOneByFilter

func (b *Base) UpdateOneByFilter(ctx context.Context, filter interface{}, data interface{}) error

func (*Base) Upsert

func (b *Base) Upsert(ctx context.Context, filter interface{}, data interface{}) (string, error)

存在则更新, 不存在则添加.

type BaseInterfc

type BaseInterfc interface {
	Create(ctx context.Context, data interface{}) (string, error)
	CreateMany(ctx context.Context, documents []interface{}) ([]string, error)       // 批量创建, 返回id列表
	DeleteFalseByIds(ctx context.Context, ids []string) (int64, error)               // 假删除
	DeleteByIds(ctx context.Context, ids []string) (int64, error)                    // 真删除
	DeleteByIdsOrgId(ctx context.Context, ids []string, orgId string) (int64, error) // 真删除
	DeleteByFilter(ctx context.Context, filter interface{}) (int64, error)
	UpdateByID(ctx context.Context, id string, data interface{}) error // 根据主键ID更新记录
	UpdateOneByFilter(ctx context.Context, filter interface{}, data interface{}) error
	UpdateMany(ctx context.Context, filter interface{}, data interface{}) error
	ReplaceOneById(ctx context.Context, id string, data interface{}) error
	Upsert(ctx context.Context, filter interface{}, data interface{}) (string, error)
	Find(ctx context.Context, filter interface{}, opt PageOrderIntfc, results interface{}) error
	FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, results interface{}) (int64, error)
	FindByNameWithPage(ctx context.Context, name string, pageOrder PageOrderIntfc, results interface{}) (int64, error)
	FindOne(ctx context.Context, filter interface{}, result interface{}) error
	FindOneByID(ctx context.Context, id string, result interface{}) error
	IsExist(ctx context.Context, filter interface{}) (bool, error)
	IsExistByName(ctx context.Context, name string) (bool, error)
	Count(ctx context.Context, filter interface{}) (int64, error)
}

type Collection

type Collection interface {
	Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
	Name() string
	Database() *mongo.Database
	BulkWrite(ctx context.Context, models []mongo.WriteModel,
		opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
	InsertOne(ctx context.Context, document interface{},
		opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	InsertMany(ctx context.Context, documents []interface{},
		opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
	DeleteOne(ctx context.Context, filter interface{},
		opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	DeleteMany(ctx context.Context, filter interface{},
		opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	UpdateByID(ctx context.Context, id interface{}, update interface{},
		opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	UpdateOne(ctx context.Context, filter interface{}, update interface{},
		opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	UpdateMany(ctx context.Context, filter interface{}, update interface{},
		opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	ReplaceOne(ctx context.Context, filter interface{},
		replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
	Aggregate(ctx context.Context, pipeline interface{},
		opts ...*options.AggregateOptions) (*mongo.Cursor, error)
	CountDocuments(ctx context.Context, filter interface{},
		opts ...*options.CountOptions) (int64, error)
	EstimatedDocumentCount(ctx context.Context,
		opts ...*options.EstimatedDocumentCountOptions) (int64, error)
	Distinct(ctx context.Context, fieldName string, filter interface{},
		opts ...*options.DistinctOptions) ([]interface{}, error)
	Find(ctx context.Context, filter interface{},
		opts ...*options.FindOptions) (*mongo.Cursor, error)
	FindOne(ctx context.Context, filter interface{},
		opts ...*options.FindOneOptions) *mongo.SingleResult
	FindOneAndDelete(ctx context.Context, filter interface{},
		opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
	FindOneAndReplace(ctx context.Context, filter interface{},
		replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
	FindOneAndUpdate(ctx context.Context, filter interface{},
		update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
	Watch(ctx context.Context, pipeline interface{},
		opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
	Indexes() mongo.IndexView
	Drop(ctx context.Context) error
}

type CreateSupported

type CreateSupported interface {
	CreateController(c *gin.Context)
}

All of the methods are the same type as HandlerFunc if you don't want to support any methods of CRUD, then don't implement it

type Crud

type Crud struct {
	// contains filtered or unexported fields
}

func NewCrud

func NewCrud(database, collection string, param Param) *Crud

CRUD 库 database 为空说明分多租户, 租户ID为数据库名称

func (*Crud) CreateController

func (d *Crud) CreateController(c *gin.Context)

func (*Crud) DeleteController

func (d *Crud) DeleteController(c *gin.Context)

func (*Crud) ExportExcelController

func (d *Crud) ExportExcelController(c *gin.Context)

导出 Excel

func (*Crud) InfoController

func (d *Crud) InfoController(c *gin.Context)

func (*Crud) ListController

func (d *Crud) ListController(c *gin.Context)

分页查询

只适用于 mongodb

查询参数结构体属性为指针, 反射遍历, 拼接mongo filter为 == Limit 与 offset 字段

func (*Crud) UpdateController

func (d *Crud) UpdateController(c *gin.Context)

func (*Crud) UploadExcelController

func (d *Crud) UploadExcelController(c *gin.Context)

type DeleteParam

type DeleteParam struct {
	IDs []string `json:"ids" form:"ids" bson:"ids"`
}

type DeleteSupported

type DeleteSupported interface {
	DeleteController(c *gin.Context)
}

type ExcelColumn

type ExcelColumn struct {
	Key          string
	Name         string
	ExportFormat func(interface{}) interface{} // TODO: 增加参数 elemValue, 见 ExportExcelController
	ImportFormat func(interface{}) interface{}
}

Tile对应的json key

type ExportExcelSupported

type ExportExcelSupported interface {
	ExportExcelController(c *gin.Context)
}

type InfoSupported

type InfoSupported interface {
	InfoController(c *gin.Context)
}

type ListSupported

type ListSupported interface {
	ListController(c *gin.Context)
}

type MongoBase

type MongoBase struct {
	C *mongo.Collection
}

func NewCrudBase

func NewCrudBase(collection *mongo.Collection) *MongoBase

func (*MongoBase) Create

func (b *MongoBase) Create(ctx context.Context, data interface{}) (string, error)

func (*MongoBase) DeleteByIds

func (b *MongoBase) DeleteByIds(ctx context.Context, ids []string) (int64, error)

func (*MongoBase) FindByID

func (b *MongoBase) FindByID(ctx context.Context, id string, result interface{}) error

func (*MongoBase) FindWithPage

func (b *MongoBase) FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, results interface{}) (int64, interface{}, error)

func (*MongoBase) UpdateByID

func (b *MongoBase) UpdateByID(ctx context.Context, id string, data interface{}) error

type MongoConf

type MongoConf struct {
	URI      string `json:"-"`                          // mongo server uri
	Addr     string `json:"addr" yaml:"ADDR"`           // ip:port
	Database string `json:"-"`                          // 数据库名称
	UserName string `json:"user_name" yaml:"USER_NAME"` // 用户名
	PWD      string `json:"pwd" yaml:"PWD"`             // 密码
}

type MongoManager

type MongoManager struct {
	// contains filtered or unexported fields
}

MongoConnMgr mongodb连接管理. 多租户下(目前是数据隔离), 不同租户连接的数据库不同.

var (
	DefaultMongoMgr *MongoManager
)

func NewMongoManager

func NewMongoManager(conf MongoConf) *MongoManager

func (*MongoManager) GetDB

func (m *MongoManager) GetDB(dbName string) (*mongo.Database, error)

所有租户同一把锁. 数据库名称一般为租户ID

type Order

type Order struct {
	OrderBy string `form:"order_by" json:"order_by" bson:"order_by"` // 排序字段,默认 _id
	Asc     bool   `form:"asc" json:"asc" bson:"asc"`                // true: 升序, false:降序, 默认降序
}

排序.

type Page

type Page struct {
	Limit  *int64 `form:"limit,default=10" json:"limit" bson:"limit"`   // 每页大小, 默认10
	Offset *int64 `form:"offset,default=0" json:"offset" bson:"offset"` // 偏移量,从0开始, 默认0
}

分页参数.

func (Page) GetMongoFindOptions

func (p Page) GetMongoFindOptions() *options.FindOptions

type PageOrder

type PageOrder struct {
	Page
	Order // TODO: 支持切片
}

分页与排序. 分页传0是查询所有.

func NewPageOrder

func NewPageOrder(limit, offset int64) PageOrder

func (PageOrder) GetMongoFindOptions

func (p PageOrder) GetMongoFindOptions() *options.FindOptions

分页函数返回 mongoDB 分页option, order排序.

type PageOrderIntfc

type PageOrderIntfc interface {
	GetMongoFindOptions() *options.FindOptions
}

type Param

type Param interface {
	NewModel(c *gin.Context) interface{}
	NewCreate(c *gin.Context) interface{}
	NewSearch(c *gin.Context) interface{}
	NewUpdate(c *gin.Context) interface{}
	NewListResult(c *gin.Context) interface{}
	ExcelColumns() []ExcelColumn
	ExcelName() string
}

type ResulListtVO

type ResulListtVO struct {
	// Data []model.Route `json:"data"`
	Data       interface{} `json:"data"` // 切片
	TotalCount int64       `json:"total_count"`
}

type UpdateSupported

type UpdateSupported interface {
	UpdateController(c *gin.Context)
}

type UploadExcelSupported

type UploadExcelSupported interface {
	UploadExcelController(c *gin.Context)
}

Jump to

Keyboard shortcuts

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