Repository API
使用工厂函数
Section titled “使用工厂函数”import { createRepository } from '@esengine/database'
const playerRepo = createRepository<Player>(mongo, 'players')
// 启用软删除const playerRepo = createRepository<Player>(mongo, 'players', true)继承 Repository
Section titled “继承 Repository”import { Repository, BaseEntity } from '@esengine/database'
interface Player extends BaseEntity { name: string score: number}
class PlayerRepository extends Repository<Player> { constructor(connection: IMongoConnection) { super(connection, 'players', false) // 第三个参数:启用软删除 }
// 添加自定义方法 async findTopPlayers(limit: number): Promise<Player[]> { return this.findMany({ sort: { score: 'desc' }, limit }) }}BaseEntity 接口
Section titled “BaseEntity 接口”所有实体必须继承 BaseEntity:
interface BaseEntity { id: string createdAt: Date updatedAt: Date deletedAt?: Date // 软删除时使用}findById
Section titled “findById”const player = await repo.findById('player-123')findOne
Section titled “findOne”const player = await repo.findOne({ where: { name: 'John' }})
const topPlayer = await repo.findOne({ sort: { score: 'desc' }})findMany
Section titled “findMany”// 简单查询const players = await repo.findMany({ where: { rank: 'gold' }})
// 复杂查询const players = await repo.findMany({ where: { score: { $gte: 100 }, rank: { $in: ['gold', 'platinum'] } }, sort: { score: 'desc', name: 'asc' }, limit: 10, offset: 0})findPaginated
Section titled “findPaginated”const result = await repo.findPaginated( { page: 1, pageSize: 20 }, { where: { rank: 'gold' }, sort: { score: 'desc' } })
console.log(result.data) // Player[]console.log(result.total) // 总数量console.log(result.totalPages) // 总页数console.log(result.hasNext) // 是否有下一页console.log(result.hasPrev) // 是否有上一页const count = await repo.count({ where: { rank: 'gold' }})exists
Section titled “exists”const exists = await repo.exists({ where: { email: 'john@example.com' }})create
Section titled “create”const player = await repo.create({ name: 'John', score: 0})// 自动生成 id, createdAt, updatedAtcreateMany
Section titled “createMany”const players = await repo.createMany([ { name: 'Alice', score: 100 }, { name: 'Bob', score: 200 }, { name: 'Carol', score: 150 }])update
Section titled “update”const updated = await repo.update('player-123', { score: 200, rank: 'gold'})// 自动更新 updatedAtdelete
Section titled “delete”// 普通删除await repo.delete('player-123')
// 软删除(如果启用)// 实际是设置 deletedAt 字段deleteMany
Section titled “deleteMany”const count = await repo.deleteMany({ where: { score: { $lt: 10 } }})const repo = createRepository<Player>(mongo, 'players', true)// 默认排除软删除记录const players = await repo.findMany()
// 包含软删除记录const allPlayers = await repo.findMany({ includeSoftDeleted: true})await repo.restore('player-123')QueryOptions
Section titled “QueryOptions”interface QueryOptions<T> { /** 查询条件 */ where?: WhereCondition<T>
/** 排序 */ sort?: Partial<Record<keyof T, 'asc' | 'desc'>>
/** 限制数量 */ limit?: number
/** 偏移量 */ offset?: number
/** 包含软删除记录(仅在启用软删除时有效) */ includeSoftDeleted?: boolean}PaginatedResult
Section titled “PaginatedResult”interface PaginatedResult<T> { data: T[] total: number page: number pageSize: number totalPages: number hasNext: boolean hasPrev: boolean}