@recursyve/nestjs-data-filter 11.5.0 → 11.5.2
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.
|
@@ -27,6 +27,7 @@ export declare class DataFilterRepository<Data> {
|
|
|
27
27
|
findAll(options?: FindOptions, conditions?: object): Promise<Data[]>;
|
|
28
28
|
findAllFromUser(user: DataFilterUserModel, options?: FindOptions, conditions?: object): Promise<Data[]>;
|
|
29
29
|
count(where?: WhereOptions, conditions?: object): Promise<number>;
|
|
30
|
+
countFromUser(user: DataFilterUserModel, where?: WhereOptions, conditions?: object): Promise<number>;
|
|
30
31
|
search(search: string, language?: string | null, options?: FindOptions, conditions?: object): Promise<Data[]>;
|
|
31
32
|
searchFromUser(user: DataFilterUserModel, search: string, options?: FindOptions, conditions?: object): Promise<Data[]>;
|
|
32
33
|
generateFindOptions(options?: FindOptions, conditions?: object): FindOptions;
|
|
@@ -92,6 +92,11 @@ let DataFilterRepository = class DataFilterRepository {
|
|
|
92
92
|
const options = this.generateFindOptions({ where }, conditions);
|
|
93
93
|
return this.model.count(options);
|
|
94
94
|
}
|
|
95
|
+
async countFromUser(user, where, conditions) {
|
|
96
|
+
const options = this.generateFindOptions({ where }, conditions);
|
|
97
|
+
options.where = await this.mergeAccessControlCondition(options.where, user);
|
|
98
|
+
return this.model.count(options);
|
|
99
|
+
}
|
|
95
100
|
async search(search, language = null, options, conditions) {
|
|
96
101
|
const findOptions = this.generateFindOptions(options, conditions);
|
|
97
102
|
this.addSearchCondition(search, findOptions, language);
|
|
@@ -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
|
|
117
|
-
const
|
|
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
|
-
|
|
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
|
}
|
package/lib/sequelize.utils.d.ts
CHANGED
|
@@ -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;
|
package/lib/sequelize.utils.js
CHANGED
|
@@ -15,6 +15,18 @@ 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
|
+
through: { attributes: [] },
|
|
27
|
+
include: include.include ? this.stripIncludeAttributes(include.include) : [],
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
18
30
|
static mergeIncludes(a = [], b = [], ignoreAttributes = false) {
|
|
19
31
|
var _a, _b, _c, _d;
|
|
20
32
|
if (!Array.isArray(a)) {
|