Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8736907
feat: added minifyAlias option in findOne, findAll, and findAndCountAll
frostzt Dec 5, 2022
913a54b
feat: implement minifyAlias option
frostzt Dec 19, 2022
44014a6
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Jan 9, 2023
a163e29
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Jan 16, 2023
96697df
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Jan 18, 2023
3577bb3
fix: typo on postgres query.js
frostzt Jan 18, 2023
d52f322
tests: added integration test for minification
frostzt Jan 21, 2023
909bc75
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Jan 21, 2023
2d4f054
fix: removed logging from sequelize tests
frostzt Jan 21, 2023
d0d19c9
feat: replace aliases in mysql query runner
frostzt Jan 23, 2023
19c1361
chore: revert back to previous
frostzt Jan 24, 2023
4fa4f2d
chore: review changes
frostzt Jan 25, 2023
c7cb0f5
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Jan 25, 2023
7170f88
fix: use local options instead of global checks
frostzt Jan 25, 2023
c6a261b
fix: use local options instead of global checks for generateJoin
frostzt Jan 25, 2023
d47909e
fix: typo for minifyAliases
frostzt Jan 25, 2023
d0ba8bf
fix: add minifyAliases to escapeAttributes
frostzt Jan 25, 2023
3ef4424
fix: updated and replaced typos
frostzt Jan 25, 2023
ba96d52
refactor: move alias minification over to queryInterface
frostzt Jan 26, 2023
7bec4ba
chore: remove extra instance created
frostzt Jan 26, 2023
3716c38
fix: use internal minifyAliases
frostzt Feb 3, 2023
ee7108e
fix: pass minifyAliases in subQuery
frostzt Feb 3, 2023
7f366dd
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Feb 3, 2023
50eb1e7
chore: review changes
frostzt Feb 3, 2023
57c51b3
Merge remote-tracking branch 'sequelize/main' into feat/minify-alias-…
frostzt Mar 3, 2023
fd338d3
chore: review changes
frostzt Mar 3, 2023
f51dc83
tests: added integration tests for qi.select
frostzt Mar 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/core/src/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
let query;

// Aliases can be passed through subqueries and we don't want to reset them
if (this.options.minifyAliases && !options.aliasesMapping) {
if (options.minifyAliases && !options.aliasesMapping) {
options.aliasesMapping = new Map();
options.aliasesByTable = {};
options.includeAliases = new Map();
Expand Down Expand Up @@ -1268,7 +1268,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
continue;
}

const joinQueries = this.generateInclude(include, { externalAs: mainTable.as, internalAs: mainTable.as }, topLevelInfo, { replacements: options.replacements });
const joinQueries = this.generateInclude(include, { externalAs: mainTable.as, internalAs: mainTable.as }, topLevelInfo, { replacements: options.replacements, minifyAliases: options.minifyAliases });

subJoinQueries = subJoinQueries.concat(joinQueries.subQuery);
mainJoinQueries = mainJoinQueries.concat(joinQueries.mainQuery);
Expand Down Expand Up @@ -1372,6 +1372,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
offset: options.offset,
limit: options.groupedLimit.limit,
order: groupedLimitOrder,
minifyAliases: options.minifyAliases,
aliasesMapping: options.aliasesMapping,
aliasesByTable: options.aliasesByTable,
where,
Expand Down Expand Up @@ -1540,7 +1541,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {

let alias = attr[1];

if (this.options.minifyAliases) {
if (options.minifyAliases) {
alias = this._getMinifiedAlias(alias, mainTableAs, options);
}

Expand Down Expand Up @@ -1630,7 +1631,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {

let alias = `${includeAs.externalAs}.${attrAs}`;

if (this.options.minifyAliases) {
if (options.minifyAliases) {
alias = this._getMinifiedAlias(alias, includeAs.internalAs, topLevelInfo.options);
}

Expand All @@ -1653,7 +1654,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {

let joinQuery;
if (include.through) {
joinQuery = this.generateThroughJoin(include, includeAs, parentTableName.internalAs, topLevelInfo);
joinQuery = this.generateThroughJoin(include, includeAs, parentTableName.internalAs, topLevelInfo, { minifyAliases: options.minifyAliases });
} else {
this._generateSubQueryFilter(include, includeAs, topLevelInfo);
joinQuery = this.generateJoin(include, topLevelInfo, options);
Expand Down Expand Up @@ -1750,7 +1751,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
}

_getAliasForField(tableName, field, options) {
if (this.options.minifyAliases && options.aliasesByTable[`${tableName}${field}`]) {
if (options.minifyAliases && options.aliasesByTable[`${tableName}${field}`]) {
return options.aliasesByTable[`${tableName}${field}`];
}

Expand Down Expand Up @@ -1852,7 +1853,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
}
}

if (this.options.minifyAliases && asRight.length > 63) {
if (options?.minifyAliases && asRight.length > 63) {
const alias = `%${topLevelInfo.options.includeAliases.size}`;

topLevelInfo.options.includeAliases.set(alias, asRight);
Expand Down Expand Up @@ -1937,7 +1938,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
return { outputFragment, returnFields, returningFragment, tmpTable };
}

generateThroughJoin(include, includeAs, parentTableName, topLevelInfo) {
generateThroughJoin(include, includeAs, parentTableName, topLevelInfo, options) {
const through = include.through;
const throughTable = through.model.getTableName();
const throughAs = `${includeAs.internalAs}->${through.as}`;
Expand All @@ -1946,7 +1947,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
const throughAttributes = through.attributes.map(attr => {
let alias = `${externalThroughAs}.${Array.isArray(attr) ? attr[1] : attr}`;

if (this.options.minifyAliases) {
if (options.minifyAliases) {
Comment thread
WikiRik marked this conversation as resolved.
Resolved
alias = this._getMinifiedAlias(alias, throughAs, topLevelInfo.options);
}

Expand Down Expand Up @@ -1977,7 +1978,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
let throughWhere;
let targetWhere;

if (this.options.minifyAliases && throughAs.length > 63) {
if (options.minifyAliases && throughAs.length > 63) {
topLevelInfo.options.includeAliases.set(`%${topLevelInfo.options.includeAliases.size}`, throughAs);
if (includeAs.internalAs.length > 63) {
topLevelInfo.options.includeAliases.set(`%${topLevelInfo.options.includeAliases.size}`, includeAs.internalAs);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dialects/abstract/query-interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface QiInsertOptions extends QueryRawOptions, Replaceable {
}

export interface QiSelectOptions extends QueryRawOptions, Replaceable, Filterable<any> {

minifyAliases?: boolean;
}

export interface QiUpdateOptions extends QueryRawOptions, Replaceable {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/dialects/abstract/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,8 @@ export class AbstractQueryInterface extends AbstractQueryInterfaceTypeScript {
}

async select(model, tableName, optionsArg) {
const options = { ...optionsArg, type: QueryTypes.SELECT, model };
const minifyAliases = optionsArg.minifyAliases ?? this.sequelize.options.minifyAliases;
const options = { ...optionsArg, type: QueryTypes.SELECT, model, minifyAliases };

const sql = this.queryGenerator.selectQuery(tableName, options, model);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const { logger } = require('../../utils/logger');
const debug = logger.debugContext('sql:pg');

export class PostgresQuery extends AbstractQuery {
async run(sql, parameters) {
async run(sql, parameters, options) {
const { connection } = this;

if (!_.isEmpty(this.options.searchPath)) {
sql = this.sequelize.getQueryInterface().queryGenerator.setSearchPath(this.options.searchPath) + sql;
}

if (this.sequelize.options.minifyAliases && this.options.includeAliases) {
if (options?.minifyAliases && this.options.includeAliases) {
for (const [alias, original] of _.toPairs(this.options.includeAliases)
// Sorting to replace the longest aliases first to prevent alias collision
.sort((a, b) => b[1].length - a[1].length)) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class PostgresQuery extends AbstractQuery {
)
: queryResult.rowCount || 0;

if (this.sequelize.options.minifyAliases && this.options.aliasesMapping) {
if (options?.minifyAliases && this.options.aliasesMapping) {
rows = rows
.map(row => _.toPairs(row)
.reduce((acc, [key, value]) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,12 @@ export interface FindOptions<TAttributes = any>
*/
raw?: boolean;

/**
* Controls whether aliases are minified in this query.
* This overrides the global option
*/
minifyAliases?: boolean;

/**
* Select group rows after groups and aggregates are computed.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ Use Sequelize#query if you wish to use replacements.`);
await this.hooks.runAsync('beforeQuery', options, query);
checkTransaction();

return await query.run(sql, bindParameters);
return await query.run(sql, bindParameters, { minifyAliases: options.minifyAliases });
} finally {
await this.hooks.runAsync('afterQuery', options, query);
if (!options.transaction) {
Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/integration/query-interface/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'chai';
import { DataTypes } from '@sequelize/core';
import { getTestDialect, createSequelizeInstance } from '../support';

const dialect = getTestDialect();

const supportedByDialect = ['postgres'].includes(dialect);

describe('QueryInterface#select', () => {
if (supportedByDialect) {
it('fetches records with alias minification', async () => {
const instance = createSequelizeInstance();
const User = instance.define('user', {
name: { type: DataTypes.TEXT },
}, { timestamps: false });

await User.sync({ force: true });

await User.create({ name: 'Sourav' });

const qi = instance.getQueryInterface();

const result = await qi.select(User, 'users', { where: { name: 'Sourav' }, minifyAliases: true });

// @ts-expect-error -- queryInterface.select returns `object[]` can't really make it infer to user
expect(result[0].name).to.equal('Sourav');
});
}
});
11 changes: 3 additions & 8 deletions packages/core/test/unit/query-generator/select-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import type { InferAttributes, Model } from '@sequelize/core';
import { Op, literal, DataTypes, or, fn, where, cast, col } from '@sequelize/core';
import { _validateIncludedElements } from '@sequelize/core/_non-semver-use-at-your-own-risk_/model-internals.js';
import { createSequelizeInstance, expectsql, sequelize } from '../../support';
import { expectsql, sequelize } from '../../support';

describe('QueryGenerator#selectQuery', () => {
const queryGenerator = sequelize.getQueryInterface().queryGenerator;
Expand Down Expand Up @@ -508,14 +508,9 @@ Only named replacements (:name) are allowed in literal() because we cannot guara
});

describe('minifyAliases', () => {
const minifyAliasesSequelize = createSequelizeInstance({
Comment thread
WikiRik marked this conversation as resolved.
Resolved
minifyAliases: true,
});

const minifyQueryGenerator = minifyAliasesSequelize.queryInterface.queryGenerator;

it('minifies custom attributes', () => {
const sql = minifyQueryGenerator.selectQuery(User.tableName, {
const sql = queryGenerator.selectQuery(User.tableName, {
minifyAliases: true,
model: User,
attributes: [
[literal('1'), 'customAttr'],
Expand Down