Documentation
¶
Index ¶
- func IsNotFoundError(err error) bool
- func IsUniqueConstraintError(err error) bool
- func SyncIndexes(ctx context.Context, col *mongo.Collection, expected []mongo.IndexModel) error
- type AutoIDModel
- type BeforeDeleter
- type BeforeFinder
- type BeforeInserter
- type BeforeUpdater
- type Collection
- func (c *Collection[T]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, ...) (*mongo.Cursor, error)
- func (c *Collection[T]) BulkWrite(ctx context.Context, models []mongo.WriteModel, ...) (*mongo.BulkWriteResult, error)
- func (c *Collection[T]) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (c *Collection[T]) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (c *Collection[T]) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (c *Collection[T]) Distinct(ctx context.Context, fieldName string, filter interface{}, ...) ([]interface{}, error)
- func (c *Collection[T]) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error)
- func (c *Collection[T]) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*T, error)
- func (c *Collection[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
- func (c *Collection[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
- func (c *Collection[T]) Native() *mongo.Collection
- func (c *Collection[T]) UpdateMany(ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- func (c *Collection[T]) UpdateOne(ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- type DB
- func (db *DB) Client() *mongo.Client
- func (db *DB) CollectionWithTenant(ctx context.Context, collName string) *mongo.Collection
- func (db *DB) Database() *mongo.Database
- func (db *DB) DatabaseWithTenant(ctx context.Context) *mongo.Database
- func (db *DB) GetBatchIdGenerator(collection string, count int) (int64, error)
- func (db *DB) Use(plugin Plugin)
- type Filter
- type IModel
- type IMongoPlugin
- type MapStr
- type Model
- type Mongo
- func (m *Mongo) Collection(collName string) *mongo.Collection
- func (m *Mongo) Database() *mongo.Database
- func (m *Mongo) GetBatchIdGenerator(collection string, count int) (startID int64, err error)
- func (m *Mongo) GetIdGenerator(collection string) int64
- func (m *Mongo) InsertManyWithAutoID(ctx context.Context, collectionName string, docs []AutoIDModel) (*mongo.InsertManyResult, error)
- func (m *Mongo) InsertOneWithAutoID(ctx context.Context, collectionName string, doc AutoIDModel) (*mongo.InsertOneResult, error)
- type Plugin
- type Querier
- type Statement
- type TenantModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNotFoundError ¶ added in v1.10.0
IsNotFoundError 检查是否是数据不存在错误
func IsUniqueConstraintError ¶ added in v1.10.0
IsUniqueConstraintError 检查是否是唯一索引冲突错误
func SyncIndexes ¶ added in v1.11.1
func SyncIndexes(ctx context.Context, col *mongo.Collection, expected []mongo.IndexModel) error
SyncIndexes 声明式索引同步器,对比活跃索引与代码预期配置,自动增删对齐
Types ¶
type AutoIDModel ¶ added in v1.10.0
AutoIDModel 定义需要自动生成 ID 的模型接口 (兼容旧版)
type BeforeDeleter ¶ added in v1.11.1
BeforeDeleter 拦截删除前置节点(DeleteOne, DeleteMany 等)
type BeforeFinder ¶ added in v1.11.1
BeforeFinder 拦截查询前置节点(FindOne, Find, CountDocuments, Distinct 等)
type BeforeInserter ¶ added in v1.11.1
BeforeInserter 拦截写入前置节点(InsertOne, InsertMany 等)
type BeforeUpdater ¶ added in v1.11.1
BeforeUpdater 拦截更新前置节点(UpdateOne, UpdateMany, ReplaceOne 等)
type Collection ¶
type Collection[T any] struct { // contains filtered or unexported fields }
Collection 泛型强类型数据集包装器
func NewCollection ¶ added in v1.11.1
func NewCollection[T any](db *DB, collName string) *Collection[T]
NewCollection 实例化泛型 Collection
func (*Collection[T]) Aggregate ¶ added in v1.11.1
func (c *Collection[T]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
Aggregate 聚合查询方法,自动在管道最前端织入租户过滤阶段以保障租户隔离
func (*Collection[T]) BulkWrite ¶ added in v1.11.1
func (c *Collection[T]) BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
BulkWrite 批量写入/更新/删除操作,自动为每个子操作应用多租户插件
func (*Collection[T]) CountDocuments ¶ added in v1.11.1
func (c *Collection[T]) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
CountDocuments 统计文档数量,自动触发 BeforeFinder 拦截
func (*Collection[T]) DeleteMany ¶ added in v1.11.1
func (c *Collection[T]) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany 批量删除文档,自动拦截并追加写隔离校验
func (*Collection[T]) DeleteOne ¶ added in v1.11.1
func (c *Collection[T]) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne 删除单条文档,自动拦截并追加写隔离校验
func (*Collection[T]) Distinct ¶ added in v1.11.1
func (c *Collection[T]) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
Distinct 获取去重字段集,自动拦截查询并追加租户隔离
func (*Collection[T]) Find ¶ added in v1.11.1
func (c *Collection[T]) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error)
Find 查询多条记录,应用生命周期拦截并自动转换为强类型切片
func (*Collection[T]) FindOne ¶ added in v1.11.1
func (c *Collection[T]) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*T, error)
FindOne 查询单条记录,应用生命周期拦截并自动反序列化
func (*Collection[T]) InsertMany ¶ added in v1.11.1
func (c *Collection[T]) InsertMany(ctx context.Context, docs []*T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
InsertMany 批量写入文档,自动强转并运行批量 ID 审计拦截
func (*Collection[T]) InsertOne ¶ added in v1.11.1
func (c *Collection[T]) InsertOne(ctx context.Context, doc *T, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
InsertOne 写入单条文档,执行 BeforeInsert 拦截(自动填充 TenantID 与自增 ID)
func (*Collection[T]) Native ¶ added in v1.11.1
func (c *Collection[T]) Native() *mongo.Collection
Native 直接获取底层的 *mongo.Collection 以进行操作
func (*Collection[T]) UpdateMany ¶ added in v1.11.1
func (c *Collection[T]) UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany 批量更新文档,自动拦截并追加租户隔离条件
func (*Collection[T]) UpdateOne ¶ added in v1.11.1
func (c *Collection[T]) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne 更新单条文档,自动拦截并追加防越权租户隔离条件
type DB ¶ added in v1.11.1
type DB struct {
// contains filtered or unexported fields
}
DB 是支持插件生命周期管理与泛型数据集代理的 MongoDB 核心管理器
func (*DB) CollectionWithTenant ¶ added in v1.11.1
CollectionWithTenant 物理隔离专用:获取租户物理隔离库的原生集合(不做逻辑拦截)
func (*DB) DatabaseWithTenant ¶ added in v1.11.1
DatabaseWithTenant 物理隔离专用:根据 Context 动态路由并返回租户的物理数据库
func (*DB) GetBatchIdGenerator ¶ added in v1.11.1
GetBatchIdGenerator 从 c_id_generator 中原子地为指定集合申请 count 个连续自增 ID,供手动批量 Upsert 场景使用
type IModel ¶ added in v1.11.1
IModel 基础数据模型规范接口,AutoIDPlugin 通过此接口检测和设置 ID。
大多数场景推荐直接内嵌 Model 结构体,自动满足此接口,无需手写方法。
type IMongoPlugin ¶ added in v1.11.1
type IMongoPlugin interface {
Name() string
}
IMongoPlugin 定义多插件拦截规范名
type Model ¶ added in v1.11.1
type Model struct {
ID int64 `bson:"id"`
}
Model 基础模型结构体,内嵌后自动满足 IModel 接口。
用法:
type Task struct {
mongox.Model
Name string `bson:"name"`
}
Task 自动拥有 SetID/GetID 方法,AutoIDPlugin 可正常识别并注入 ID。
type Mongo ¶
type Mongo struct {
DBClient *mongo.Client
Sess mongo.Session
// contains filtered or unexported fields
}
Mongo 旧版 Mongo 核心处理器
func (*Mongo) Collection ¶
func (m *Mongo) Collection(collName string) *mongo.Collection
func (*Mongo) GetBatchIdGenerator ¶ added in v1.10.0
GetBatchIdGenerator 批量获取自增 ID,提高批量插入性能
func (*Mongo) GetIdGenerator ¶
func (*Mongo) InsertManyWithAutoID ¶ added in v1.10.0
func (m *Mongo) InsertManyWithAutoID(ctx context.Context, collectionName string, docs []AutoIDModel) (*mongo.InsertManyResult, error)
InsertManyWithAutoID 批量插入文档,自动生成 ID
func (*Mongo) InsertOneWithAutoID ¶ added in v1.10.0
func (m *Mongo) InsertOneWithAutoID(ctx context.Context, collectionName string, doc AutoIDModel) (*mongo.InsertOneResult, error)
InsertOneWithAutoID 插入单个文档,自动生成 ID
type Querier ¶
type Querier[T any] interface { FindOne(ctx context.Context) (*T, error) FindMany(ctx context.Context) ([]*T, error) }
Querier 兼容 Querier
type Statement ¶ added in v1.11.1
type Statement struct {
CollectionName string
Model interface{} // 单个实体的指针,或者切片指针(例如 &User 或 &[]User)
Filter bson.M // 最终传递给底层驱动的查询过滤器
Update interface{} // 更新指令(如 bson.M{"$set": ...})
Context context.Context
}
Statement 承载 MongoDB CRUD 操作生命周期的运行期元数据
type TenantModel ¶ added in v1.11.1
TenantModel 租户模型结构体,内嵌后 TenantPlugin 自动识别并注入 TenantID。
用法:
type Order struct {
mongox.TenantModel
Title string `bson:"title"`
}
不需要实现任何方法,TenantPlugin 通过反射自动检测和设置 TenantID 字段。