lite-ts-db
v11.24.8
Published
```typescript const dbFactory: DbFactoryBase;
Downloads
69
Readme
代码
DbFactoryBase - 数据库工厂
const dbFactory: DbFactoryBase;
// 创建数据仓储
class MyModel extends DbModel {}
const res = dbFactory.db<MyModel>(
modelDbOption(MyModel),
);
// 创建工作单元
const res = dbFactory.uow();
- ProjectDbFactory - 项目数据库
const globalDbFactory: DbFactoryBase;
const getAllProjectDbFactoryFunc: () => Promise<{
[projectNo: number]: {
[areaNo: number]: DbFactoryBase;
};
}>;
const dbFactory: DbFactoryBase = new ProjectDbFactory(
globalDbFactory,
getAllProjectDbFactoryFunc,
);
// 创建区服数据库
const areaDbFactory = await dbFactory.buildAreaFactory(区服编号, 项目编号);
IDbQuery - 数据查询
const dbFactory: DbFactoryBase;
const dbQuery = dbFactory.db(
modelDbOption(表模型),
).query();
// 查询数量
const count = await db.query().count(查询条件);
// 查询行
const rows = await db.query().toArray({
skip: 跳过[可选],
take: 获取多少行[可选],
where: 查询条件[可选],
// 可选
order: [正序字段1, ..., 正序字段n],
// 可选
orderByDesc: [倒序字段1, ..., 倒序字段n]
});
- ProjectDbQuery<T extends DbModel> - 项目数据查询
class MyModel extends DbModel {}
const dbRepository: DbRepository<MyModel>;
const projectDbFactory: ProjectDbFactory;
const dbQuery: = new ProjectDbQuery<MyModel>(dbRepository, projectDbFactory);
IDbRepository<T extends DbModel> - 数据仓储
const dbFactory: DbFactoryBase;
const dbRepo = dbFactory.db(
modelDbOption(表模型),
);
// 新增
await db.add(表模型实体);
// 更新
await db.save(表模型实体);
// 删除
await db.remove(表模型实体);
- DbRepository<T extends DbModel> - 数据仓储
class MyModel extends DbModel {}
const dbRepo: IDbRepository<MyModel> = new DbRepository<MyModel>();
UnitOfWorkBase - 工作单元(事务)
const dbFactory: DbFactoryBase;
const uow = dbFactory.uow();
const dbRepo = dbFactory.db(
modelDbOption(表模型),
uowDbOption(uow),
);
await db.add(表模型实体);
await db.save(表模型实体);
await db.remove(表模型实体);
// 提交后数据才会更新到数据库
await uow.commit();
UnitOfWorkRepositoryBase - 工作单元仓储(基类)
const uow: UnitOfWorkRepositoryBase;
// 注册新增
uow.registerAdd(模型名, 实体);
// 注册删除
uow.registerRemove(模型名, 实体);
// 注册保存
uow.registerSave(模型名, 实体);
- ProjectUnitOfWork - 项目工作单元
const projectDbFactory: ProjectDbFactory;
const uow: UnitOfWorkBase = new ProjectUnitOfWork(projectDbFactory);