info 强制匹配

This commit is contained in:
cool 2024-03-12 18:41:44 +08:00
parent 7ccb903290
commit 3ee0695785
3 changed files with 27 additions and 24 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "7.1.7", "version": "7.1.8",
"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.7", "version": "7.1.8",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -6,7 +6,7 @@ import { Application, Context } from "@midwayjs/koa";
import * as SqlString from "sqlstring"; import * as SqlString from "sqlstring";
import { CoolConfig } from "../interface"; import { CoolConfig } from "../interface";
import { TypeORMDataSourceManager } from "@midwayjs/typeorm"; import { TypeORMDataSourceManager } from "@midwayjs/typeorm";
import { Brackets, In, Repository, SelectQueryBuilder } from "typeorm"; import { Brackets, Equal, In, Repository, SelectQueryBuilder } from "typeorm";
import { QueryOp } from "../decorator/controller"; import { QueryOp } from "../decorator/controller";
import * as _ from "lodash"; import * as _ from "lodash";
import { CoolEventManager } from "../event"; import { CoolEventManager } from "../event";
@ -243,7 +243,7 @@ export abstract class BaseMysqlService {
if (!id) { if (!id) {
throw new CoolValidateException(ERRINFO.NOID); throw new CoolValidateException(ERRINFO.NOID);
} }
const info = await this.entity.findOneBy({ id }); const info = await this.entity.findOneBy({ id: Equal(id) });
if (info && infoIgnoreProperty) { if (info && infoIgnoreProperty) {
for (const property of infoIgnoreProperty) { for (const property of infoIgnoreProperty) {
delete info[property]; delete info[property];
@ -295,33 +295,37 @@ export abstract class BaseMysqlService {
* | * |
* @param param * @param param
*/ */
async addOrUpdate(param: any | any[], type: 'add' | 'update' = 'add') { async addOrUpdate(param: any | any[], type: "add" | "update" = "add") {
if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY); if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY);
delete param.createTime; delete param.createTime;
// 判断是否是批量操作 // 判断是否是批量操作
if (param instanceof Array) { if (param instanceof Array) {
param.forEach((item) => { param.forEach((item) => {
item.updateTime = new Date(); item.updateTime = new Date();
item.createTime = new Date(); item.createTime = new Date();
}); });
await this.entity.save(param); await this.entity.save(param);
} else{ } else {
const upsert = this._coolConfig.crud?.upsert || 'normal'; const upsert = this._coolConfig.crud?.upsert || "normal";
if (type == 'update') { if (type == "update") {
if(upsert == 'save') { if (upsert == "save") {
const info = await this.entity.findOneBy({id: param.id}) const info = await this.entity.findOneBy({ id: param.id });
param = { param = {
...info, ...info,
...param ...param,
} };
} }
param.updateTime = new Date(); param.updateTime = new Date();
upsert == 'normal'? await this.entity.update(param.id, param): await this.entity.save(param); upsert == "normal"
? await this.entity.update(param.id, param)
: await this.entity.save(param);
} }
if(type =='add'){ if (type == "add") {
param.createTime = new Date(); param.createTime = new Date();
param.updateTime = new Date(); param.updateTime = new Date();
upsert == 'normal'? await this.entity.insert(param): await this.entity.save(param); upsert == "normal"
? await this.entity.insert(param)
: await this.entity.save(param);
} }
} }
} }
@ -436,10 +440,10 @@ export abstract class BaseMysqlService {
for (let key of option.fieldEq) { for (let key of option.fieldEq) {
const c = {}; const c = {};
// 如果key有包含.的情况下操作 // 如果key有包含.的情况下操作
if(typeof key === "string" && key.includes('.')){ if (typeof key === "string" && key.includes(".")) {
const keys = key.split('.'); const keys = key.split(".");
const lastKey = keys.pop(); const lastKey = keys.pop();
key = {requestParam: lastKey, column: key}; key = { requestParam: lastKey, column: key };
} }
// 单表字段无别名的情况下操作 // 单表字段无别名的情况下操作
if (typeof key === "string") { if (typeof key === "string") {
@ -496,5 +500,4 @@ export abstract class BaseMysqlService {
sqlArr.push(sqls[sqls.length - 1]); sqlArr.push(sqls[sqls.length - 1]);
return sqlArr.join(" "); return sqlArr.join(" ");
} }
} }