强制设置实体

This commit is contained in:
cool 2024-03-11 19:14:59 +08:00
parent 0b4546e965
commit 7ccb903290
3 changed files with 24 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.6",
"version": "7.1.7",
"description": "",
"main": "dist/index.js",
"typings": "index.d.ts",

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.6",
"version": "7.1.7",
"description": "",
"main": "index.js",
"typings": "index.d.ts",

View File

@ -55,14 +55,14 @@ export abstract class BaseService {
baseApp: Application;
@Init()
async init(){
async init() {
const services = {
mysql: this.baseMysqlService,
postgres: this.basePgService,
sqlite: this.baseSqliteService
sqlite: this.baseSqliteService,
};
this.service = services[this.ormType];
if(!this.service) throw new CoolCoreException('暂不支持当前数据库类型');
if (!this.service) throw new CoolCoreException("暂不支持当前数据库类型");
this.sqlParams = this.service.sqlParams;
await this.service.init();
}
@ -79,7 +79,6 @@ export abstract class BaseService {
this.service.setCtx(ctx);
}
// 设置应用对象
setApp(app: Application) {
this.baseApp = app;
@ -104,11 +103,11 @@ export abstract class BaseService {
return this.service.getCountSql(sql);
}
/**
/**
*
* @param params
*/
async paramSafetyCheck(params) {
async paramSafetyCheck(params) {
return await this.service.paramSafetyCheck(params);
}
@ -153,7 +152,12 @@ export abstract class BaseService {
* @param connectionName
*/
async sqlRenderPage(sql, query, autoSort = true, connectionName?) {
return await this.service.sqlRenderPage(sql, query, autoSort, connectionName);
return await this.service.sqlRenderPage(
sql,
query,
autoSort,
connectionName
);
}
/**
@ -162,6 +166,7 @@ export abstract class BaseService {
* @param infoIgnoreProperty
*/
async info(id: any, infoIgnoreProperty?: string[]) {
this.service.setEntity(this.entity);
return await this.service.info(id, infoIgnoreProperty);
}
@ -170,6 +175,7 @@ export abstract class BaseService {
* @param ids ID集合 [1,2,3] 1,2,3
*/
async delete(ids: any) {
this.service.setEntity(this.entity);
await this.modifyBefore(ids, "delete");
await this.service.delete(ids);
await this.modifyAfter(ids, "delete");
@ -181,6 +187,7 @@ export abstract class BaseService {
* @param entity
*/
async softDelete(ids: number[], entity?: Repository<any>) {
this.service.setEntity(this.entity);
await this.service.softDelete(ids, entity);
}
@ -189,10 +196,11 @@ export abstract class BaseService {
* @param param
*/
async update(param: any) {
this.service.setEntity(this.entity);
if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY);
if (!param.id && !(param instanceof Array))
throw new CoolValidateException(ERRINFO.NOID);
await this.addOrUpdate(param,'update')
await this.addOrUpdate(param, "update");
}
/**
@ -201,7 +209,7 @@ export abstract class BaseService {
*/
async add(param: any | any[]): Promise<Object> {
if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY);
await this.addOrUpdate(param, 'add');
await this.addOrUpdate(param, "add");
return {
id:
param instanceof Array
@ -218,7 +226,8 @@ export abstract class BaseService {
* |
* @param param
*/
async addOrUpdate(param: any | any[], type: 'add' | 'update' = 'add') {
async addOrUpdate(param: any | any[], type: "add" | "update" = "add") {
this.service.setEntity(this.entity);
await this.modifyBefore(param, type);
await this.service.addOrUpdate(param, type);
await this.modifyAfter(param, type);
@ -231,6 +240,7 @@ export abstract class BaseService {
* @param connectionName
*/
async list(query, option, connectionName?): Promise<any> {
this.service.setEntity(this.entity);
return await this.service.list(query, option, connectionName);
}
@ -241,6 +251,7 @@ export abstract class BaseService {
* @param connectionName
*/
async page(query, option, connectionName?) {
this.service.setEntity(this.entity);
return await this.service.page(query, option, connectionName);
}
@ -250,6 +261,7 @@ export abstract class BaseService {
* @param option
*/
async getOptionFind(query, option: QueryOp) {
this.service.setEntity(this.entity);
return await this.service.getOptionFind(query, option);
}
@ -270,5 +282,4 @@ export abstract class BaseService {
data: any,
type: "delete" | "update" | "add"
): Promise<void> {}
}