@recursyve/nestjs-data-filter 11.5.0 → 11.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,6 +59,7 @@ export declare class FilterService<Data> {
59
59
  private generateHavingOptions;
60
60
  private addSearchCondition;
61
61
  private addOrderCondition;
62
+ private getOptionsForCountAndIds;
62
63
  private countTotalValues;
63
64
  private findValues;
64
65
  private getAccessControlWhereCondition;
@@ -113,8 +113,9 @@ let FilterService = FilterService_1 = class FilterService {
113
113
  if (options.groupBy) {
114
114
  countOptions.group = [sequelize_utils_1.SequelizeUtils.getGroupLiteral(this.repository.model, options.groupBy)];
115
115
  }
116
- const total = await this.countTotalValues({ options: countOptions, user });
117
- const values = await this.findValues({ filter: options, options: countOptions, request, user });
116
+ const optionsForCountAndIds = this.getOptionsForCountAndIds(countOptions);
117
+ const total = await this.countTotalValues({ options: optionsForCountAndIds, user });
118
+ const values = await this.findValues({ filter: options, options: optionsForCountAndIds, request, user });
118
119
  return {
119
120
  total,
120
121
  page: options.page,
@@ -346,6 +347,16 @@ let FilterService = FilterService_1 = class FilterService {
346
347
  options.include = sequelize_utils_1.SequelizeUtils.mergeIncludes(options.include, includes);
347
348
  }
348
349
  }
350
+ getOptionsForCountAndIds(options) {
351
+ const include = options.include;
352
+ if (!include || (Array.isArray(include) && include.length === 0)) {
353
+ return options;
354
+ }
355
+ return {
356
+ ...options,
357
+ include: sequelize_utils_1.SequelizeUtils.stripIncludeAttributes(include)
358
+ };
359
+ }
349
360
  async countTotalValues(params) {
350
361
  var _a;
351
362
  const { options } = params;
@@ -354,9 +365,12 @@ let FilterService = FilterService_1 = class FilterService {
354
365
  options.where = await this.getAccessControlWhereCondition(options.where, user);
355
366
  }
356
367
  if (options.having) {
368
+ const modelName = this.repository.model.name;
357
369
  const data = await this.repository.model.findAll({
358
370
  ...options,
359
- subQuery: false
371
+ attributes: [[sequelize_typescript_1.Sequelize.literal(`DISTINCT \`${modelName}\`.\`id\``), "id"]],
372
+ subQuery: false,
373
+ raw: true
360
374
  });
361
375
  return data.length;
362
376
  }
@@ -11,6 +11,7 @@ export declare class M extends Model {
11
11
  }
12
12
  export declare class SequelizeUtils {
13
13
  static reduceIncludes(includes: Array<IncludeOptions | IncludeOptions[]>, ignoreAttributes?: boolean): Includeable[];
14
+ static stripIncludeAttributes(includes: IncludeOptions[] | IncludeOptions): IncludeOptions[];
14
15
  static mergeIncludes(a?: IncludeOptions | IncludeOptions[], b?: IncludeOptions | IncludeOptions[], ignoreAttributes?: boolean): IncludeOptions[];
15
16
  static mergeAttributes(a: FindAttributeOptions | undefined, b: FindAttributeOptions | undefined): FindAttributeOptions | undefined;
16
17
  static ensureAttributesValidity(attribute: FindAttributeOptions): FindAttributeOptions;
@@ -15,6 +15,17 @@ class SequelizeUtils {
15
15
  }
16
16
  return include;
17
17
  }
18
+ static stripIncludeAttributes(includes) {
19
+ const arr = !includes ? [] : Array.isArray(includes) ? includes : [includes];
20
+ if (!arr.length) {
21
+ return [];
22
+ }
23
+ return arr.map((include) => ({
24
+ ...include,
25
+ attributes: [],
26
+ include: include.include ? this.stripIncludeAttributes(include.include) : [],
27
+ }));
28
+ }
18
29
  static mergeIncludes(a = [], b = [], ignoreAttributes = false) {
19
30
  var _a, _b, _c, _d;
20
31
  if (!Array.isArray(a)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recursyve/nestjs-data-filter",
3
- "version": "11.5.0",
3
+ "version": "11.5.1",
4
4
  "description": "NestJs DataFilter library",
5
5
  "main": "index.js",
6
6
  "scripts": {