强制设置实体

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", "name": "@cool-midway/core",
"version": "7.1.6", "version": "7.1.7",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

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

View File

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