mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-15 08:22:49 +00:00
优化eps
This commit is contained in:
parent
e4b9e8a685
commit
3925e4fedb
@ -67,6 +67,8 @@ export interface QueryOp {
|
|||||||
where?: Function;
|
where?: Function;
|
||||||
// 查询字段
|
// 查询字段
|
||||||
select?: string[];
|
select?: string[];
|
||||||
|
// 字段模糊查询
|
||||||
|
fieldLike?: string[] | FieldEq[] | (string | FieldEq)[];
|
||||||
// 字段相等
|
// 字段相等
|
||||||
fieldEq?: string[] | FieldEq[] | (string | FieldEq)[];
|
fieldEq?: string[] | FieldEq[] | (string | FieldEq)[];
|
||||||
// 添加排序条件
|
// 添加排序条件
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import {
|
|||||||
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
||||||
import { CoolUrlTagData } from '../tag/data';
|
import { CoolUrlTagData } from '../tag/data';
|
||||||
import { TagTypes } from '../decorator/tag';
|
import { TagTypes } from '../decorator/tag';
|
||||||
|
import { CurdOption, QueryOp } from '../decorator/controller';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实体路径
|
* 实体路径
|
||||||
@ -54,6 +55,7 @@ export class CoolEps {
|
|||||||
const appArr = [];
|
const appArr = [];
|
||||||
for (const controller of controllers) {
|
for (const controller of controllers) {
|
||||||
const { prefix, module, curdOption, routerOptions } = controller;
|
const { prefix, module, curdOption, routerOptions } = controller;
|
||||||
|
const pageQueryOp = await this.getPageOp(curdOption);
|
||||||
const name = curdOption?.entity?.name;
|
const name = curdOption?.entity?.name;
|
||||||
(_.startsWith(prefix, '/admin/') ? adminArr : appArr).push({
|
(_.startsWith(prefix, '/admin/') ? adminArr : appArr).push({
|
||||||
module,
|
module,
|
||||||
@ -66,6 +68,29 @@ export class CoolEps {
|
|||||||
api: routers[prefix],
|
api: routers[prefix],
|
||||||
name,
|
name,
|
||||||
columns: entitys[name] || [],
|
columns: entitys[name] || [],
|
||||||
|
pageQueryOp: {
|
||||||
|
keyWordLikeFields:
|
||||||
|
pageQueryOp?.keyWordLikeFields?.map(field =>
|
||||||
|
field.includes('.') ? field : `a.${field}`
|
||||||
|
) || [],
|
||||||
|
fieldEq:
|
||||||
|
pageQueryOp?.fieldEq?.map(field =>
|
||||||
|
typeof field === 'string'
|
||||||
|
? field.includes('.')
|
||||||
|
? field
|
||||||
|
: `a.${field}`
|
||||||
|
: field
|
||||||
|
) || [],
|
||||||
|
fieldLike:
|
||||||
|
pageQueryOp?.fieldLike?.map(field =>
|
||||||
|
typeof field === 'string'
|
||||||
|
? field.includes('.')
|
||||||
|
? field
|
||||||
|
: `a.${field}`
|
||||||
|
: field
|
||||||
|
) || [],
|
||||||
|
},
|
||||||
|
pageColumns: await this.pageColumns(entitys, curdOption),
|
||||||
prefix,
|
prefix,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -73,6 +98,111 @@ export class CoolEps {
|
|||||||
this.app = _.groupBy(appArr, 'module');
|
this.app = _.groupBy(appArr, 'module');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取分页查询配置
|
||||||
|
* @param curdOption
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async getPageOp(curdOption: CurdOption) {
|
||||||
|
let pageQueryOp: QueryOp | Function = curdOption?.pageQueryOp;
|
||||||
|
if (typeof pageQueryOp === 'function') {
|
||||||
|
pageQueryOp = await pageQueryOp();
|
||||||
|
}
|
||||||
|
return pageQueryOp as QueryOp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理列
|
||||||
|
* @param entitys
|
||||||
|
* @param entityColumns
|
||||||
|
* @param curdOption
|
||||||
|
*/
|
||||||
|
async pageColumns(entitys: Record<string, any[]>, curdOption: CurdOption) {
|
||||||
|
const pageQueryOp = await this.getPageOp(curdOption);
|
||||||
|
// 检查 pageQueryOp 是否为对象且具有 select 属性
|
||||||
|
if (
|
||||||
|
pageQueryOp &&
|
||||||
|
typeof pageQueryOp === 'object' &&
|
||||||
|
'select' in pageQueryOp &&
|
||||||
|
curdOption?.entity?.name
|
||||||
|
) {
|
||||||
|
const select = pageQueryOp.select;
|
||||||
|
const join = pageQueryOp.join || [];
|
||||||
|
// 所有的关联entitys
|
||||||
|
const joinEntitys: {
|
||||||
|
name: string;
|
||||||
|
alias: string;
|
||||||
|
}[] = [{ name: curdOption.entity.name, alias: 'a' }];
|
||||||
|
|
||||||
|
if (join.length > 0) {
|
||||||
|
joinEntitys.push(
|
||||||
|
...join.map(item => {
|
||||||
|
return { name: item.entity.name, alias: item.alias };
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 select
|
||||||
|
const result = [];
|
||||||
|
for (const selectItem of select) {
|
||||||
|
// 处理 'a.*' 这种情况
|
||||||
|
if (selectItem.endsWith('.*')) {
|
||||||
|
const alias = selectItem.split('.')[0];
|
||||||
|
const entity = joinEntitys.find(e => e.alias === alias);
|
||||||
|
if (entity) {
|
||||||
|
const entityColumns = entitys[entity.name] || [];
|
||||||
|
result.push(
|
||||||
|
...entityColumns.map(e => {
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
source: `${alias}.${e.propertyName}`,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理单个字段,如 'b.name' 或 'b.name as userName'
|
||||||
|
const asRegex = /\s+as\s+/i;
|
||||||
|
const [field, asName] = selectItem.split(asRegex).map(s => s.trim());
|
||||||
|
const [alias, fieldName] = field.split('.');
|
||||||
|
const entity = joinEntitys.find(e => e.alias === alias);
|
||||||
|
|
||||||
|
if (entity) {
|
||||||
|
const entityColumns = entitys[entity.name] || [];
|
||||||
|
const column = entityColumns.find(
|
||||||
|
col => col.propertyName === fieldName
|
||||||
|
);
|
||||||
|
if (column) {
|
||||||
|
result.push({
|
||||||
|
...column,
|
||||||
|
propertyName: asName || column.propertyName,
|
||||||
|
source: `${alias}.${column.propertyName}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将 createTime 和 updateTime 移到末尾
|
||||||
|
const finalResult = [...result];
|
||||||
|
const timeFields = ['createTime', 'updateTime'];
|
||||||
|
const timeColumns = [];
|
||||||
|
|
||||||
|
// 先找出并删除所有时间字段
|
||||||
|
for (let i = finalResult.length - 1; i >= 0; i--) {
|
||||||
|
if (timeFields.includes(finalResult[i].propertyName)) {
|
||||||
|
timeColumns.unshift(finalResult.splice(i, 1)[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将时间字段添加到末尾
|
||||||
|
finalResult.push(...timeColumns);
|
||||||
|
|
||||||
|
return finalResult;
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模块信息
|
* 模块信息
|
||||||
* @param module
|
* @param module
|
||||||
@ -150,13 +280,19 @@ export class CoolEps {
|
|||||||
length: e.length,
|
length: e.length,
|
||||||
comment: e.comment,
|
comment: e.comment,
|
||||||
nullable: e.isNullable,
|
nullable: e.isNullable,
|
||||||
|
defaultValue: e.default,
|
||||||
|
dict: e['dict'],
|
||||||
|
source: `a.${e.propertyName}`,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
o => {
|
o => {
|
||||||
if (['createTime', 'updateTime'].includes(o.propertyName)) {
|
if (['createTime', 'updateTime'].includes(o.propertyName)) {
|
||||||
commColums.push(o);
|
commColums.push(o);
|
||||||
}
|
}
|
||||||
return o && !['createTime', 'updateTime'].includes(o.propertyName);
|
return (
|
||||||
|
o &&
|
||||||
|
!['createTime', 'updateTime', 'tenantId'].includes(o.propertyName)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
).concat(commColums);
|
).concat(commColums);
|
||||||
result[entityMetadata.name] = columns;
|
result[entityMetadata.name] = columns;
|
||||||
|
|||||||
@ -470,6 +470,33 @@ export abstract class BaseMysqlService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 字段模糊查询
|
||||||
|
if (!_.isEmpty(option.fieldLike)) {
|
||||||
|
for (let key of option.fieldLike) {
|
||||||
|
// 如果key有包含.的情况下操作
|
||||||
|
if (typeof key === 'string' && key.includes('.')) {
|
||||||
|
const keys = key.split('.');
|
||||||
|
const lastKey = keys.pop();
|
||||||
|
key = { requestParam: lastKey, column: key };
|
||||||
|
}
|
||||||
|
// 单表字段无别名的情况下操作
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
if (query[key] || query[key] === 0) {
|
||||||
|
find.andWhere(`${key} like :${key}`, {
|
||||||
|
[key]: `%${query[key]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key]}%`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (query[key.requestParam] || query[key.requestParam] === 0) {
|
||||||
|
find.andWhere(`${key.column} like :${key.column}`, {
|
||||||
|
[key.column]: `%${query[key.requestParam]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key.requestParam]}%`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sqlArr.push(selects.join(','));
|
sqlArr.push(selects.join(','));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -532,6 +532,40 @@ export abstract class BasePgService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 字段模糊查询
|
||||||
|
if (!_.isEmpty(option.fieldLike)) {
|
||||||
|
for (let key of option.fieldLike) {
|
||||||
|
const c = {};
|
||||||
|
let column;
|
||||||
|
// 如果key有包含.的情况下操作
|
||||||
|
if (typeof key === 'string' && key.includes('.')) {
|
||||||
|
const keys = key.split('.');
|
||||||
|
const lastKey = keys.pop();
|
||||||
|
key = { requestParam: lastKey, column: key };
|
||||||
|
column = key;
|
||||||
|
} else {
|
||||||
|
column = `a.${key}`;
|
||||||
|
}
|
||||||
|
// 单表字段无别名的情况下操作
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
if (query[key] || query[key] == 0) {
|
||||||
|
c[key] = query[key];
|
||||||
|
find.andWhere(`${column} like :${key}`, {
|
||||||
|
[key]: `%${query[key]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key]}%`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (query[key.requestParam] || query[key.requestParam] == 0) {
|
||||||
|
c[key.column] = query[key.requestParam];
|
||||||
|
find.andWhere(`${key.column} like :${key.column}`, {
|
||||||
|
[key.column]: `%${query[key.requestParam]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key.requestParam]}%`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sqlArr.push(selects.join(','));
|
sqlArr.push(selects.join(','));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -523,7 +523,33 @@ export abstract class BaseSqliteService {
|
|||||||
} else {
|
} else {
|
||||||
find.andWhere(`${key.column} ${eq} :${key.column}`, c);
|
find.andWhere(`${key.column} ${eq} :${key.column}`, c);
|
||||||
}
|
}
|
||||||
// this.sqlParams.push(query[key.requestParam]);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 字段模糊查询
|
||||||
|
if (!_.isEmpty(option.fieldLike)) {
|
||||||
|
for (let key of option.fieldLike) {
|
||||||
|
// 如果key有包含.的情况下操作
|
||||||
|
if (typeof key === 'string' && key.includes('.')) {
|
||||||
|
const keys = key.split('.');
|
||||||
|
const lastKey = keys.pop();
|
||||||
|
key = { requestParam: lastKey, column: key };
|
||||||
|
}
|
||||||
|
// 单表字段无别名的情况下操作
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
if (query[key] || query[key] == 0) {
|
||||||
|
find.andWhere(`${key} like :${key}`, {
|
||||||
|
[key]: `%${query[key]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key]}%`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (query[key.requestParam] || query[key.requestParam] == 0) {
|
||||||
|
find.andWhere(`${key.column} like :${key.column}`, {
|
||||||
|
[key.column]: `%${query[key.requestParam]}%`,
|
||||||
|
});
|
||||||
|
this.sqlParams.push(`%${query[key.requestParam]}%`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,7 @@ function Column(typeOrOptions, options) {
|
|||||||
if (options.type === "hstore" && !options.hstoreType)
|
if (options.type === "hstore" && !options.hstoreType)
|
||||||
options.hstoreType =
|
options.hstoreType =
|
||||||
reflectMetadataType === Object ? "object" : "string";
|
reflectMetadataType === Object ? "object" : "string";
|
||||||
|
|
||||||
if (typeof typeOrOptions === "function") {
|
if (typeof typeOrOptions === "function") {
|
||||||
// register an embedded
|
// register an embedded
|
||||||
(0, globals_1.getMetadataArgsStorage)().embeddeds.push({
|
(0, globals_1.getMetadataArgsStorage)().embeddeds.push({
|
||||||
@ -61,6 +62,7 @@ function Column(typeOrOptions, options) {
|
|||||||
propertyName: propertyName,
|
propertyName: propertyName,
|
||||||
mode: "regular",
|
mode: "regular",
|
||||||
options: options,
|
options: options,
|
||||||
|
dict: options.dict,
|
||||||
});
|
});
|
||||||
if (options.generated) {
|
if (options.generated) {
|
||||||
(0, globals_1.getMetadataArgsStorage)().generations.push({
|
(0, globals_1.getMetadataArgsStorage)().generations.push({
|
||||||
|
|||||||
4
typeorm/decorator/options/ColumnOptions.d.ts
vendored
4
typeorm/decorator/options/ColumnOptions.d.ts
vendored
@ -9,6 +9,10 @@ export interface ColumnOptions extends ColumnCommonOptions {
|
|||||||
* Column type. Must be one of the value from the ColumnTypes class.
|
* Column type. Must be one of the value from the ColumnTypes class.
|
||||||
*/
|
*/
|
||||||
type?: ColumnType;
|
type?: ColumnType;
|
||||||
|
/**
|
||||||
|
* cool dict key
|
||||||
|
*/
|
||||||
|
dict?: string | string[];
|
||||||
/**
|
/**
|
||||||
* Column name in the database.
|
* Column name in the database.
|
||||||
*/
|
*/
|
||||||
|
|||||||
4
typeorm/metadata/ColumnMetadata.d.ts
vendored
4
typeorm/metadata/ColumnMetadata.d.ts
vendored
@ -11,6 +11,10 @@ import { ValueTransformer } from "../decorator/options/ValueTransformer";
|
|||||||
*/
|
*/
|
||||||
export declare class ColumnMetadata {
|
export declare class ColumnMetadata {
|
||||||
readonly "@instanceof": symbol;
|
readonly "@instanceof": symbol;
|
||||||
|
/**
|
||||||
|
* cool
|
||||||
|
*/
|
||||||
|
dict?: string | string[];
|
||||||
/**
|
/**
|
||||||
* Target class where column decorator is used.
|
* Target class where column decorator is used.
|
||||||
* This may not be always equal to entity metadata (for example embeds or inheritance cases).
|
* This may not be always equal to entity metadata (for example embeds or inheritance cases).
|
||||||
|
|||||||
@ -14,6 +14,7 @@ class ColumnMetadata {
|
|||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this["@instanceof"] = Symbol.for("ColumnMetadata");
|
this["@instanceof"] = Symbol.for("ColumnMetadata");
|
||||||
|
this.dict = options.args.options.dict;
|
||||||
/**
|
/**
|
||||||
* Type's length in the database.
|
* Type's length in the database.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -10,6 +10,7 @@ const UpdateValuesMissingError_1 = require("../error/UpdateValuesMissingError");
|
|||||||
const error_1 = require("../error");
|
const error_1 = require("../error");
|
||||||
const EntityPropertyNotFoundError_1 = require("../error/EntityPropertyNotFoundError");
|
const EntityPropertyNotFoundError_1 = require("../error/EntityPropertyNotFoundError");
|
||||||
const DriverUtils_1 = require("../driver/DriverUtils");
|
const DriverUtils_1 = require("../driver/DriverUtils");
|
||||||
|
const Broadcaster_1 = require("../subscriber/Broadcaster");
|
||||||
/**
|
/**
|
||||||
* Allows to build complex sql queries in a fashion way and execute those queries.
|
* Allows to build complex sql queries in a fashion way and execute those queries.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user