diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 70c1c463cf..d31c5f3075 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -81,7 +81,7 @@ import { UpdateWithAssociationsListener, ValidateListener, } from './types'; -import { patchSequelizeQueryInterface, snakeCase } from './utils'; +import { patchSequelizeQueryGenerator, patchSequelizeQueryInterface, snakeCase } from './utils'; import { BaseValueParser, registerFieldValueParsers } from './value-parsers'; import { ViewCollection } from './view-collection'; @@ -327,6 +327,7 @@ export class Database extends EventEmitter implements AsyncEmitter { this.initListener(); patchSequelizeQueryInterface(this); + patchSequelizeQueryGenerator(this); this.registerCollectionType(); } diff --git a/packages/core/database/src/options-parser.ts b/packages/core/database/src/options-parser.ts index 5ac11a3b17..60e65c66e2 100644 --- a/packages/core/database/src/options-parser.ts +++ b/packages/core/database/src/options-parser.ts @@ -205,17 +205,9 @@ export class OptionsParser { exclude: [], }; // out put all fields by default - if (this.collection.isParent()) { - OptionsParser.appendInheritInspectAttribute(attributes.include, this.collection); - } - if (this.options?.fields) { attributes = []; - if (this.collection.isParent()) { - OptionsParser.appendInheritInspectAttribute(attributes, this.collection); - } - // 将fields拆分为 attributes 和 appends for (const field of this.options.fields) { if (this.isAssociationPath(field)) { @@ -242,11 +234,24 @@ export class OptionsParser { } return { - attributes, + attributes: this.handleRawAttributes(attributes), ...this.parseExcept(except, this.parseAppends(appends, filterParams)), }; } + protected handleRawAttributes(attributes) { + if (this.collection.isParent()) { + OptionsParser.appendInheritInspectAttribute( + attributes.include ? attributes.include : attributes, + this.collection, + ); + } + + this.collection.emit('beforeParseAttributes', attributes); + + return attributes; + } + protected parseExcept(except: Except, filterParams: any) { if (!except) return filterParams; const setExcept = (queryParams: any, except: string) => { diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index d088178dd8..ddbb80cb76 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -100,6 +100,32 @@ export function patchSequelizeQueryInterface(db: Database) { } } +export function patchSequelizeQueryGenerator(db: Database) { + // @ts-ignore + const oldSelectQuery = db.sequelize.dialect.queryGenerator.selectQuery; + // add hook after selectQuery called + // @ts-ignore + db.sequelize.dialect.queryGenerator.selectQuery = function (...args) { + const result = oldSelectQuery.apply(this, args); + + const model = args[2]; + const collection = db.modelCollection.get(model); + + if (collection) { + const eventArgs = { + sql: result, + collection, + }; + + collection.emit(`selectQuery`, eventArgs); + + return eventArgs.sql; + } + + return result; + }; +} + export function percent2float(value: string) { if (!value.endsWith('%')) { return NaN;