Skip to content

Commit

Permalink
fix(count): remove useless joins in basic count (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngriselle authored Mar 2, 2022
1 parent ad19d59 commit 86d30ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/services/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ function QueryBuilder() {
.map((name) => name.replace(`${association.as}.`, ''))
.filter((fieldName) => targetFields.includes(fieldName));

if (!fieldNamesRequested
|| fieldNamesRequested.includes(association.as)
if (fieldNamesRequested?.includes(association.as)
|| explicitAttributes.length) {
// NOTICE: For performance reasons, we only request the keys
// as they're the only needed fields for the interface
Expand Down
4 changes: 2 additions & 2 deletions src/services/query-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class QueryOptions {
get _sequelizeInclude() {
const fields = [...this._requestedFields, ...this._requestedRelations, ...this._neededFields];
const include = [
...new QueryBuilder().getIncludes(this._model, fields.length ? fields : null),
...new QueryBuilder().getIncludes(this._model, fields),
...this._customerIncludes,
];

Expand All @@ -83,7 +83,7 @@ class QueryOptions {

/** Compute sequelize query `.order` property */
get _sequelizeOrder() {
if (isMSSQL(this._model.sequelize) && this._sequelizeInclude?.length) {
if (isMSSQL(this._model.sequelize)) {
// Work around sequelize bug: https://github.com/sequelize/sequelize/issues/11258
const primaryKeys = Object.keys(this._model.primaryKeys);
return this._order.filter((order) => !primaryKeys.includes(order[0]));
Expand Down
5 changes: 4 additions & 1 deletion src/services/resources-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class ResourcesGetter {
const scopeFilters = await scopeManager.getScopeForUser(this._user, this._model.name, true);

const requestedFields = extractRequestedFields(fields, this._model, Schemas.schemas);
const queryOptions = new QueryOptions(this._model, { tableAlias });
const queryOptions = new QueryOptions(this._model, {
tableAlias,
includeRelations: searchExtended,
});
if (!forCount) await queryOptions.requireFields(requestedFields);
await queryOptions.search(search, searchExtended);
await queryOptions.filterByConditionTree(filters, timezone);
Expand Down

0 comments on commit 86d30ef

Please sign in to comment.