mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:38:13 +00:00
chore: patch sequelize query generator
This commit is contained in:
parent
8bbf6bc65b
commit
63be6f65f5
@ -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();
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user