@recursyve/nestjs-data-filter 11.3.1 → 11.4.0
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.
|
@@ -45,12 +45,12 @@ export declare class FilterService<Data> {
|
|
|
45
45
|
forData<T>(dataDef: Type<T>): FilterService<T>;
|
|
46
46
|
getConfig(request: any, user?: DataFilterUserModel): Promise<FilterConfigurationModel[]>;
|
|
47
47
|
searchConfigValues(request: any, search: FilterConfigurationSearchModel, user?: DataFilterUserModel): Promise<SelectFilterValue[]>;
|
|
48
|
-
private convertNumber;
|
|
49
48
|
findResourceValueById(request: any, search: FilterResourceValueModel, user?: DataFilterUserModel): Promise<SelectFilterValue | null>;
|
|
50
49
|
count<Users extends DataFilterUserModel>(params: CountParams<Users>): Promise<number>;
|
|
51
50
|
filter<Request, Users extends DataFilterUserModel>(params: FilterParams<DataFilterUserModel, Request>): Promise<FilterResultModel<Data>>;
|
|
52
51
|
downloadData<Request>(params: DownloadDataParams<DataFilterUserModel, Request>): Promise<Buffer | string>;
|
|
53
52
|
getFindOptions(model: typeof M, query: QueryModel | undefined, data?: object): Promise<FindOptions>;
|
|
53
|
+
private convertNumber;
|
|
54
54
|
private init;
|
|
55
55
|
private getInclude;
|
|
56
56
|
private getConditionInclude;
|
|
@@ -58,7 +58,6 @@ export declare class FilterService<Data> {
|
|
|
58
58
|
private generateHavingOptions;
|
|
59
59
|
private addSearchCondition;
|
|
60
60
|
private addOrderCondition;
|
|
61
|
-
private addGroupOption;
|
|
62
61
|
private countTotalValues;
|
|
63
62
|
private findValues;
|
|
64
63
|
private getAccessControlWhereCondition;
|
|
@@ -13,6 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
13
13
|
exports.FilterService = void 0;
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
15
|
const sequelize_1 = require("sequelize");
|
|
16
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
16
17
|
const access_control_adapter_1 = require("../adapters/access-control.adapter");
|
|
17
18
|
const translate_adapter_1 = require("../adapters/translate.adapter");
|
|
18
19
|
const data_filter_service_1 = require("../data-filter.service");
|
|
@@ -70,18 +71,11 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
70
71
|
user,
|
|
71
72
|
request,
|
|
72
73
|
page: this.convertNumber(search.page),
|
|
73
|
-
pageSize: this.convertNumber(search.pageSize)
|
|
74
|
+
pageSize: this.convertNumber(search.pageSize)
|
|
74
75
|
});
|
|
75
76
|
}
|
|
76
77
|
return [];
|
|
77
78
|
}
|
|
78
|
-
convertNumber(value) {
|
|
79
|
-
if (!value) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
const parsedNumber = Number(value);
|
|
83
|
-
return isNaN(parsedNumber) ? null : parsedNumber;
|
|
84
|
-
}
|
|
85
79
|
async findResourceValueById(request, search, user) {
|
|
86
80
|
if (!this.definitions.hasOwnProperty(search.id)) {
|
|
87
81
|
return null;
|
|
@@ -116,7 +110,9 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
116
110
|
if (options.order) {
|
|
117
111
|
this.addOrderCondition(options.order, countOptions, options.data);
|
|
118
112
|
}
|
|
119
|
-
|
|
113
|
+
if (options.groupBy) {
|
|
114
|
+
countOptions.group = [sequelize_utils_1.SequelizeUtils.getGroupLiteral(this.repository.model, options.groupBy)];
|
|
115
|
+
}
|
|
120
116
|
const total = await this.countTotalValues({ options: countOptions, user });
|
|
121
117
|
const values = await this.findValues({ filter: options, options: countOptions, request, user });
|
|
122
118
|
return {
|
|
@@ -139,7 +135,13 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
139
135
|
this.addOrderCondition(options.order, findOptions, options.data);
|
|
140
136
|
}
|
|
141
137
|
delete options.page;
|
|
142
|
-
const values = await this.findValues({
|
|
138
|
+
const values = await this.findValues({
|
|
139
|
+
filter: options,
|
|
140
|
+
options: findOptions,
|
|
141
|
+
repository: this.exportRepository,
|
|
142
|
+
request,
|
|
143
|
+
user
|
|
144
|
+
});
|
|
143
145
|
const headers = await this.model.getExportedFieldsKeys(type);
|
|
144
146
|
if (headers.length) {
|
|
145
147
|
const data = await Promise.all(values.map((value) => { var _a; return this.model.getExportedFields(value, (_a = user === null || user === void 0 ? void 0 : user.language) !== null && _a !== void 0 ? _a : "fr", type); }));
|
|
@@ -176,6 +178,13 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
176
178
|
}
|
|
177
179
|
return option;
|
|
178
180
|
}
|
|
181
|
+
convertNumber(value) {
|
|
182
|
+
if (!value) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const parsedNumber = Number(value);
|
|
186
|
+
return isNaN(parsedNumber) ? null : parsedNumber;
|
|
187
|
+
}
|
|
179
188
|
init() {
|
|
180
189
|
var _a;
|
|
181
190
|
this.repository = this.dataFilter.for(this.model.dataDefinition);
|
|
@@ -337,34 +346,6 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
337
346
|
options.include = sequelize_utils_1.SequelizeUtils.mergeIncludes(options.include, includes);
|
|
338
347
|
}
|
|
339
348
|
}
|
|
340
|
-
addGroupOption(filter, options) {
|
|
341
|
-
const model = this.repository.model;
|
|
342
|
-
options.group = [`${model.name}.id`];
|
|
343
|
-
if (filter.groupBy) {
|
|
344
|
-
options.group.push(sequelize_utils_1.SequelizeUtils.getGroupLiteral(this.repository.model, filter.groupBy));
|
|
345
|
-
}
|
|
346
|
-
if (!filter.order || (filter.order instanceof Array && !filter.order.length)) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
const orders = this.normalizeOrder(filter.order);
|
|
350
|
-
for (const order of orders) {
|
|
351
|
-
const rule = this.definitions[order.column];
|
|
352
|
-
if (rule && order_rule_1.OrderRule.validate(rule)) {
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
if (this.repository.hasCustomAttribute(order.column)) {
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
const values = order.column.split(".");
|
|
359
|
-
const column = values.pop();
|
|
360
|
-
if (!values.length) {
|
|
361
|
-
options.group.push(`${model.name}.${sequelize_utils_1.SequelizeUtils.findColumnFieldName(model, column)}`);
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
options.group.push(...this.sequelizeModelScanner.getGroup(model, order));
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
349
|
async countTotalValues(params) {
|
|
369
350
|
var _a;
|
|
370
351
|
const { options } = params;
|
|
@@ -394,7 +375,7 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
394
375
|
}
|
|
395
376
|
}
|
|
396
377
|
async findValues(params) {
|
|
397
|
-
var _a, _b, _c, _d, _e, _f
|
|
378
|
+
var _a, _b, _c, _d, _e, _f;
|
|
398
379
|
const { filter, options, request } = params;
|
|
399
380
|
const user = (_a = params.user) !== null && _a !== void 0 ? _a : null;
|
|
400
381
|
const repository = (_b = params.repository) !== null && _b !== void 0 ? _b : this.repository;
|
|
@@ -402,27 +383,14 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
402
383
|
options.where = await this.getAccessControlWhereCondition(options.where, user);
|
|
403
384
|
}
|
|
404
385
|
const order = this.getOrderOptions((_c = filter.order) !== null && _c !== void 0 ? _c : [], { request, user });
|
|
405
|
-
const nonNestedOrderColumns = (Array.isArray(filter.order) ? filter.order : [filter.order])
|
|
406
|
-
.filter((order) => !!order)
|
|
407
|
-
.map(order => {
|
|
408
|
-
var _a;
|
|
409
|
-
const rule = this.definitions[order.column];
|
|
410
|
-
if (!rule || !order_rule_1.OrderRule.validate(rule)) {
|
|
411
|
-
return order.column;
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
return [(_a = rule.path) !== null && _a !== void 0 ? _a : "", rule.attribute].join(".");
|
|
415
|
-
}
|
|
416
|
-
})
|
|
417
|
-
.filter(column => column && !column.includes(".") && !repository.hasCustomAttribute(column));
|
|
418
386
|
const customAttributes = filter.order ? this.getOrderCustomAttribute(filter.order, filter.data) : [];
|
|
419
387
|
const values = await repository.model.findAll({
|
|
420
388
|
...options,
|
|
421
|
-
attributes: [
|
|
389
|
+
attributes: [[sequelize_typescript_1.Sequelize.literal(`DISTINCT \`${this.repository.model.name}\`.\`id\``), "_id"], ...customAttributes],
|
|
422
390
|
limit: (_d = filter.page) === null || _d === void 0 ? void 0 : _d.size,
|
|
423
391
|
offset: filter.page ? filter.page.number * filter.page.size + ((_e = filter.page.offset) !== null && _e !== void 0 ? _e : 0) : undefined,
|
|
424
392
|
subQuery: false,
|
|
425
|
-
|
|
393
|
+
raw: true,
|
|
426
394
|
order
|
|
427
395
|
});
|
|
428
396
|
const group = this.generateRepositoryGroupBy(filter);
|
|
@@ -433,7 +401,7 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
433
401
|
order,
|
|
434
402
|
paranoid: options.paranoid,
|
|
435
403
|
group
|
|
436
|
-
}, (
|
|
404
|
+
}, (_f = filter.data) !== null && _f !== void 0 ? _f : {});
|
|
437
405
|
}
|
|
438
406
|
async getAccessControlWhereCondition(where, user) {
|
|
439
407
|
var _a;
|
|
@@ -539,25 +507,6 @@ let FilterService = FilterService_1 = class FilterService {
|
|
|
539
507
|
return group;
|
|
540
508
|
}
|
|
541
509
|
group.push(...groupBy);
|
|
542
|
-
if (!filter.order || (filter.order instanceof Array && !filter.order.length)) {
|
|
543
|
-
return group;
|
|
544
|
-
}
|
|
545
|
-
const orders = this.normalizeOrder(filter.order);
|
|
546
|
-
for (const order of orders) {
|
|
547
|
-
if (!(order === null || order === void 0 ? void 0 : order.column) || !order.direction) {
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
if (this.repository.hasCustomAttribute(order.column)) {
|
|
551
|
-
continue;
|
|
552
|
-
}
|
|
553
|
-
const rule = this.definitions[order.column];
|
|
554
|
-
if (!rule || !order_rule_1.OrderRule.validate(rule)) {
|
|
555
|
-
group.push(`${this.repository.model.name}.${sequelize_utils_1.SequelizeUtils.findColumnFieldName(this.repository.model, order.column)}`);
|
|
556
|
-
}
|
|
557
|
-
else {
|
|
558
|
-
group.push(...this.sequelizeModelScanner.getGroup(this.repository.model, order));
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
510
|
return group;
|
|
562
511
|
}
|
|
563
512
|
addDefaultFilter(query) {
|
|
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.FilterConfigurationSearchModel = void 0;
|
|
13
|
-
const common_1 = require("@nestjs/common");
|
|
14
13
|
const swagger_1 = require("@nestjs/swagger");
|
|
15
14
|
const class_transformer_1 = require("class-transformer");
|
|
16
15
|
const class_validator_1 = require("class-validator");
|
|
@@ -24,20 +23,20 @@ __decorate([
|
|
|
24
23
|
__metadata("design:type", String)
|
|
25
24
|
], FilterConfigurationSearchModel.prototype, "id", void 0);
|
|
26
25
|
__decorate([
|
|
27
|
-
(0,
|
|
26
|
+
(0, class_validator_1.IsOptional)(),
|
|
28
27
|
(0, class_validator_1.IsString)(),
|
|
29
28
|
(0, swagger_1.ApiProperty)({ type: () => String }),
|
|
30
29
|
__metadata("design:type", Object)
|
|
31
30
|
], FilterConfigurationSearchModel.prototype, "value", void 0);
|
|
32
31
|
__decorate([
|
|
33
|
-
(0,
|
|
32
|
+
(0, class_validator_1.IsOptional)(),
|
|
34
33
|
(0, class_validator_1.IsInt)(),
|
|
35
34
|
(0, swagger_1.ApiPropertyOptional)({ type: () => Number }),
|
|
36
35
|
(0, class_transformer_1.Transform)(({ value }) => typeof value === "string" ? parseInt(value) : value),
|
|
37
36
|
__metadata("design:type", Number)
|
|
38
37
|
], FilterConfigurationSearchModel.prototype, "pageSize", void 0);
|
|
39
38
|
__decorate([
|
|
40
|
-
(0,
|
|
39
|
+
(0, class_validator_1.IsOptional)(),
|
|
41
40
|
(0, class_validator_1.IsInt)(),
|
|
42
41
|
(0, swagger_1.ApiPropertyOptional)({ type: () => Number }),
|
|
43
42
|
(0, class_transformer_1.Transform)(({ value }) => typeof value === "string" ? parseInt(value) : value),
|