49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.kingecg.top/kingecg/gomog/pkg/types"
|
|
)
|
|
|
|
// PageResult 分页查询结果
|
|
type PageResult struct {
|
|
Documents []types.Document
|
|
HasMore bool // 是否还有更多数据
|
|
Total *int // 总数(可选)
|
|
}
|
|
|
|
// DatabaseAdapter 数据库适配器接口
|
|
type DatabaseAdapter interface {
|
|
// 连接管理
|
|
Connect(ctx context.Context, dsn string) error
|
|
Close() error
|
|
Ping(ctx context.Context) error
|
|
|
|
// 表/集合管理
|
|
CreateCollection(ctx context.Context, name string) error
|
|
DropCollection(ctx context.Context, name string) error
|
|
CollectionExists(ctx context.Context, name string) (bool, error)
|
|
ListCollections(ctx context.Context) ([]string, error)
|
|
|
|
// 数据操作(批量)
|
|
InsertMany(ctx context.Context, collection string, docs []types.Document) error
|
|
UpdateMany(ctx context.Context, collection string, ids []string, doc types.Update) error
|
|
DeleteMany(ctx context.Context, collection string, ids []string) error
|
|
|
|
// 全量查询(用于加载到内存)
|
|
FindAll(ctx context.Context, collection string) ([]types.Document, error)
|
|
|
|
// 分页查询(用于懒加载)
|
|
FindPage(ctx context.Context, collection string, skip, limit int) (PageResult, error)
|
|
|
|
// 事务支持
|
|
BeginTx(ctx context.Context) (Transaction, error)
|
|
}
|
|
|
|
// Transaction 事务接口
|
|
type Transaction interface {
|
|
Commit() error
|
|
Rollback() error
|
|
}
|