mongox

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotFoundError added in v1.10.0

func IsNotFoundError(err error) bool

IsNotFoundError 检查是否是数据不存在错误

func IsUniqueConstraintError added in v1.10.0

func IsUniqueConstraintError(err error) bool

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

type AutoIDModel interface {
	SetID(id int64)
	GetID() int64
}

AutoIDModel 定义需要自动生成 ID 的模型接口 (兼容旧版)

type BeforeDeleter added in v1.11.1

type BeforeDeleter interface {
	BeforeDelete(stmt *Statement) error
}

BeforeDeleter 拦截删除前置节点(DeleteOne, DeleteMany 等)

type BeforeFinder added in v1.11.1

type BeforeFinder interface {
	BeforeFind(stmt *Statement) error
}

BeforeFinder 拦截查询前置节点(FindOne, Find, CountDocuments, Distinct 等)

type BeforeInserter added in v1.11.1

type BeforeInserter interface {
	BeforeInsert(stmt *Statement) error
}

BeforeInserter 拦截写入前置节点(InsertOne, InsertMany 等)

type BeforeUpdater added in v1.11.1

type BeforeUpdater interface {
	BeforeUpdate(stmt *Statement) error
}

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 NewDB added in v1.11.1

func NewDB(client *mongo.Client, dbName string) *DB

NewDB 实例化支持生命周期拦截的 DB 管理器

func (*DB) Client added in v1.11.1

func (db *DB) Client() *mongo.Client

Client 返回底层原生 *mongo.Client

func (*DB) CollectionWithTenant added in v1.11.1

func (db *DB) CollectionWithTenant(ctx context.Context, collName string) *mongo.Collection

CollectionWithTenant 物理隔离专用:获取租户物理隔离库的原生集合(不做逻辑拦截)

func (*DB) Database added in v1.11.1

func (db *DB) Database() *mongo.Database

Database 返回默认原生 *mongo.Database

func (*DB) DatabaseWithTenant added in v1.11.1

func (db *DB) DatabaseWithTenant(ctx context.Context) *mongo.Database

DatabaseWithTenant 物理隔离专用:根据 Context 动态路由并返回租户的物理数据库

func (*DB) GetBatchIdGenerator added in v1.11.1

func (db *DB) GetBatchIdGenerator(collection string, count int) (int64, error)

GetBatchIdGenerator 从 c_id_generator 中原子地为指定集合申请 count 个连续自增 ID,供手动批量 Upsert 场景使用

func (*DB) Use added in v1.11.1

func (db *DB) Use(plugin Plugin)

Use 注册插件拦截器(类似 GORM 的 db.Use())

type Filter

type Filter interface{}

Filter 兼容 Filter

type IModel added in v1.11.1

type IModel interface {
	// SetID 设置主键 ID
	SetID(id int64)

	// GetID 获取主键 ID
	GetID() int64
}

IModel 基础数据模型规范接口,AutoIDPlugin 通过此接口检测和设置 ID。

大多数场景推荐直接内嵌 Model 结构体,自动满足此接口,无需手写方法。

type IMongoPlugin added in v1.11.1

type IMongoPlugin interface {
	Name() string
}

IMongoPlugin 定义多插件拦截规范名

type MapStr

type MapStr map[string]interface{}

MapStr 兼容 map[string]interface{}

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。

func (*Model) GetID added in v1.11.1

func (m *Model) GetID() int64

GetID 实现 IModel 接口

func (*Model) SetID added in v1.11.1

func (m *Model) SetID(id int64)

SetID 实现 IModel 接口

type Mongo

type Mongo struct {
	DBClient *mongo.Client
	Sess     mongo.Session
	// contains filtered or unexported fields
}

Mongo 旧版 Mongo 核心处理器

func NewMongo

func NewMongo(client *mongo.Client, dbName string) *Mongo

NewMongo 兼容旧版初始化 (注意:返回 *Mongo 而非泛型新版 *DB)

func (*Mongo) Collection

func (m *Mongo) Collection(collName string) *mongo.Collection

func (*Mongo) Database

func (m *Mongo) Database() *mongo.Database

func (*Mongo) GetBatchIdGenerator added in v1.10.0

func (m *Mongo) GetBatchIdGenerator(collection string, count int) (startID int64, err error)

GetBatchIdGenerator 批量获取自增 ID,提高批量插入性能

func (*Mongo) GetIdGenerator

func (m *Mongo) GetIdGenerator(collection string) int64

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 Plugin added in v1.11.1

type Plugin IMongoPlugin

Plugin 代表全局 MongoDB 插件拦截器(IMongoPlugin 别名)

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

type TenantModel struct {
	Model
	TenantID int64 `bson:"tenant_id"`
}

TenantModel 租户模型结构体,内嵌后 TenantPlugin 自动识别并注入 TenantID。

用法:

type Order struct {
    mongox.TenantModel
    Title string `bson:"title"`
}

不需要实现任何方法,TenantPlugin 通过反射自动检测和设置 TenantID 字段。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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