Documentation ¶
Index ¶
- Constants
- Variables
- func CRUD(group *gin.RouterGroup, path string, resource interface{})
- func InitMongoManager(conf MongoConf)
- func NewContextWithTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- type Base
- func (b *Base) Count(ctx context.Context, filter interface{}) (int64, error)
- func (b *Base) Create(ctx context.Context, data interface{}) (string, error)
- func (b *Base) CreateMany(ctx context.Context, documents []interface{}) ([]string, error)
- func (b *Base) DeleteByFilter(ctx context.Context, filter interface{}) (int64, error)
- func (b *Base) DeleteByIds(ctx context.Context, ids []string) (int64, error)
- func (b *Base) DeleteByIdsOrgId(ctx context.Context, ids []string, orgId string) (int64, error)
- func (b *Base) DeleteFalseByIds(ctx context.Context, ids []string) (int64, error)
- func (b *Base) Find(ctx context.Context, filter interface{}, opt PageOrderIntfc, ...) error
- func (b *Base) FindByNameWithPage(ctx context.Context, name string, pageOrder PageOrderIntfc, ...) (int64, error)
- func (b *Base) FindOne(ctx context.Context, filter interface{}, result interface{}) error
- func (b *Base) FindOneByID(ctx context.Context, id string, result interface{}) error
- func (b *Base) FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, ...) (int64, error)
- func (b *Base) IsExist(ctx context.Context, filter interface{}) (bool, error)
- func (b *Base) IsExistByName(ctx context.Context, name string) (bool, error)
- func (b *Base) ReplaceOneById(ctx context.Context, id string, data interface{}) error
- func (b *Base) UpdateByID(ctx context.Context, id string, data interface{}) error
- func (b *Base) UpdateMany(ctx context.Context, filter interface{}, data interface{}) error
- func (b *Base) UpdateOneByFilter(ctx context.Context, filter interface{}, data interface{}) error
- func (b *Base) Upsert(ctx context.Context, filter interface{}, data interface{}) (string, error)
- type BaseInterfc
- type Collection
- type CreateSupported
- type Crud
- func (d *Crud) CreateController(c *gin.Context)
- func (d *Crud) DeleteController(c *gin.Context)
- func (d *Crud) ExportExcelController(c *gin.Context)
- func (d *Crud) InfoController(c *gin.Context)
- func (d *Crud) ListController(c *gin.Context)
- func (d *Crud) UpdateController(c *gin.Context)
- func (d *Crud) UploadExcelController(c *gin.Context)
- type DeleteParam
- type DeleteSupported
- type ExcelColumn
- type ExportExcelSupported
- type InfoSupported
- type ListSupported
- type MongoBase
- func (b *MongoBase) Create(ctx context.Context, data interface{}) (string, error)
- func (b *MongoBase) DeleteByIds(ctx context.Context, ids []string) (int64, error)
- func (b *MongoBase) FindByID(ctx context.Context, id string, result interface{}) error
- func (b *MongoBase) FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, ...) (int64, interface{}, error)
- func (b *MongoBase) UpdateByID(ctx context.Context, id string, data interface{}) error
- type MongoConf
- type MongoManager
- type Order
- type Page
- type PageOrder
- type PageOrderIntfc
- type Param
- type ResulListtVO
- type UpdateSupported
- type UploadExcelSupported
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 ¶
Types ¶
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
func NewBase ¶
func NewBase(collection Collection) *Base
func (*Base) CreateMany ¶
批量创建, 返回 primitive.ObjectID 列表.
func (*Base) DeleteByFilter ¶
func (*Base) DeleteByIds ¶
真删除.
func (*Base) DeleteByIdsOrgId ¶
真删除.
func (*Base) DeleteFalseByIds ¶
假删除.
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) FindOneByID ¶
func (*Base) FindWithPage ¶
func (b *Base) FindWithPage(ctx context.Context, filter interface{}, pageOrder PageOrderIntfc, results interface{}) (int64, error)
分页查询, 返回总记录个数. results 为切片指针.
func (*Base) IsExistByName ¶
func (*Base) UpdateByID ¶
update by id
data 可以为结构体model,支持 bson tag
data 可以为 map[string]interface{}或bson.M.
func (*Base) UpdateMany ¶
func (*Base) UpdateOneByFilter ¶
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 ¶
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 (*Crud) CreateController ¶
func (*Crud) DeleteController ¶
func (*Crud) InfoController ¶
func (*Crud) ListController ¶
分页查询
只适用于 mongodb
查询参数结构体属性为指针, 反射遍历, 拼接mongo filter为 == Limit 与 offset 字段
func (*Crud) UpdateController ¶
func (*Crud) UploadExcelController ¶
type DeleteParam ¶
type DeleteParam struct {
IDs []string `json:"ids" form:"ids" bson:"ids"`
}
type DeleteSupported ¶
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 InfoSupported ¶
type ListSupported ¶
type MongoBase ¶
type MongoBase struct {
C *mongo.Collection
}
func NewCrudBase ¶
func NewCrudBase(collection *mongo.Collection) *MongoBase
func (*MongoBase) DeleteByIds ¶
func (*MongoBase) FindWithPage ¶
type MongoManager ¶
type MongoManager struct {
// contains filtered or unexported fields
}
MongoConnMgr mongodb连接管理. 多租户下(目前是数据隔离), 不同租户连接的数据库不同.
var (
DefaultMongoMgr *MongoManager
)
func NewMongoManager ¶
func NewMongoManager(conf MongoConf) *MongoManager
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 ¶
分页与排序. 分页传0是查询所有.
func NewPageOrder ¶
func (PageOrder) GetMongoFindOptions ¶
func (p PageOrder) GetMongoFindOptions() *options.FindOptions
分页函数返回 mongoDB 分页option, order排序.
type PageOrderIntfc ¶
type PageOrderIntfc interface {
GetMongoFindOptions() *options.FindOptions
}
type ResulListtVO ¶
type ResulListtVO struct { // Data []model.Route `json:"data"` Data interface{} `json:"data"` // 切片 TotalCount int64 `json:"total_count"` }
type UpdateSupported ¶
type UploadExcelSupported ¶
Click to show internal directories.
Click to hide internal directories.