nestjs-paginate 2.4.2 → 2.5.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.
- package/README.md +1 -2
- package/lib/index.js +5 -1
- package/lib/index.js.map +1 -1
- package/lib/paginate.d.ts +9 -1
- package/lib/paginate.js +50 -12
- package/lib/paginate.js.map +1 -1
- package/lib/{decorator.spec.d.ts → test/decorator.spec.d.ts} +0 -0
- package/lib/{decorator.spec.js → test/decorator.spec.js} +2 -2
- package/lib/test/decorator.spec.js.map +1 -0
- package/lib/test/entity/cat-home.entity.d.ts +7 -0
- package/lib/test/entity/cat-home.entity.js +37 -0
- package/lib/test/entity/cat-home.entity.js.map +1 -0
- package/lib/test/entity/cat-toy.entity.d.ts +7 -0
- package/lib/test/entity/cat-toy.entity.js +38 -0
- package/lib/test/entity/cat-toy.entity.js.map +1 -0
- package/lib/test/entity/cat.entity.d.ts +11 -0
- package/lib/test/entity/cat.entity.js +51 -0
- package/lib/test/entity/cat.entity.js.map +1 -0
- package/lib/test/paginate.spec.d.ts +1 -0
- package/lib/{paginate.spec.js → test/paginate.spec.js} +261 -88
- package/lib/test/paginate.spec.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -16
- package/lib/decorator.spec.js.map +0 -1
- package/lib/paginate.spec.d.ts +0 -7
- package/lib/paginate.spec.js.map +0 -1
|
@@ -1,64 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.CatEntity = void 0;
|
|
13
3
|
const typeorm_1 = require("typeorm");
|
|
14
|
-
const
|
|
15
|
-
const typeorm_2 = require("typeorm");
|
|
4
|
+
const index_1 = require("../index");
|
|
16
5
|
const common_1 = require("@nestjs/common");
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
__metadata("design:type", Number)
|
|
22
|
-
], CatEntity.prototype, "id", void 0);
|
|
23
|
-
__decorate([
|
|
24
|
-
(0, typeorm_1.Column)(),
|
|
25
|
-
__metadata("design:type", String)
|
|
26
|
-
], CatEntity.prototype, "name", void 0);
|
|
27
|
-
__decorate([
|
|
28
|
-
(0, typeorm_1.Column)(),
|
|
29
|
-
__metadata("design:type", String)
|
|
30
|
-
], CatEntity.prototype, "color", void 0);
|
|
31
|
-
__decorate([
|
|
32
|
-
(0, typeorm_1.Column)({ nullable: true }),
|
|
33
|
-
__metadata("design:type", Number)
|
|
34
|
-
], CatEntity.prototype, "age", void 0);
|
|
35
|
-
__decorate([
|
|
36
|
-
(0, typeorm_2.CreateDateColumn)(),
|
|
37
|
-
__metadata("design:type", String)
|
|
38
|
-
], CatEntity.prototype, "createdAt", void 0);
|
|
39
|
-
CatEntity = __decorate([
|
|
40
|
-
(0, typeorm_2.Entity)()
|
|
41
|
-
], CatEntity);
|
|
42
|
-
exports.CatEntity = CatEntity;
|
|
6
|
+
const cat_entity_1 = require("./entity/cat.entity");
|
|
7
|
+
const cat_toy_entity_1 = require("./entity/cat-toy.entity");
|
|
8
|
+
const cat_home_entity_1 = require("./entity/cat-home.entity");
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
43
10
|
describe('paginate', () => {
|
|
44
11
|
let connection;
|
|
45
|
-
let
|
|
12
|
+
let catRepo;
|
|
13
|
+
let catToyRepo;
|
|
14
|
+
let catHomeRepo;
|
|
46
15
|
let cats;
|
|
16
|
+
let catToys;
|
|
17
|
+
let catHomes;
|
|
47
18
|
beforeAll(async () => {
|
|
48
19
|
connection = await (0, typeorm_1.createConnection)({
|
|
49
20
|
type: 'sqlite',
|
|
50
21
|
database: ':memory:',
|
|
51
22
|
synchronize: true,
|
|
52
23
|
logging: false,
|
|
53
|
-
entities: [CatEntity],
|
|
24
|
+
entities: [cat_entity_1.CatEntity, cat_toy_entity_1.CatToyEntity, cat_home_entity_1.CatHomeEntity],
|
|
54
25
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
26
|
+
catRepo = connection.getRepository(cat_entity_1.CatEntity);
|
|
27
|
+
catToyRepo = connection.getRepository(cat_toy_entity_1.CatToyEntity);
|
|
28
|
+
catHomeRepo = connection.getRepository(cat_home_entity_1.CatHomeEntity);
|
|
29
|
+
cats = await catRepo.save([
|
|
30
|
+
catRepo.create({ name: 'Milo', color: 'brown', age: 6 }),
|
|
31
|
+
catRepo.create({ name: 'Garfield', color: 'ginger', age: 5 }),
|
|
32
|
+
catRepo.create({ name: 'Shadow', color: 'black', age: 4 }),
|
|
33
|
+
catRepo.create({ name: 'George', color: 'white', age: 3 }),
|
|
34
|
+
catRepo.create({ name: 'Leche', color: 'white', age: null }),
|
|
35
|
+
]);
|
|
36
|
+
catToys = await catToyRepo.save([
|
|
37
|
+
catToyRepo.create({ name: 'Fuzzy Thing', cat: cats[0] }),
|
|
38
|
+
catToyRepo.create({ name: 'Stuffed Mouse', cat: cats[0] }),
|
|
39
|
+
catToyRepo.create({ name: 'Mouse', cat: cats[0] }),
|
|
40
|
+
catToyRepo.create({ name: 'String', cat: cats[1] }),
|
|
41
|
+
]);
|
|
42
|
+
catHomes = await catHomeRepo.save([
|
|
43
|
+
catHomeRepo.create({ name: 'Box', cat: cats[0] }),
|
|
44
|
+
catHomeRepo.create({ name: 'House', cat: cats[1] }),
|
|
62
45
|
]);
|
|
63
46
|
});
|
|
64
47
|
it('should return an instance of Paginated', async () => {
|
|
@@ -70,8 +53,8 @@ describe('paginate', () => {
|
|
|
70
53
|
const query = {
|
|
71
54
|
path: '',
|
|
72
55
|
};
|
|
73
|
-
const result = await (0,
|
|
74
|
-
expect(result).toBeInstanceOf(
|
|
56
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
57
|
+
expect(result).toBeInstanceOf(index_1.Paginated);
|
|
75
58
|
expect(result.data).toStrictEqual(cats.slice(0, 1));
|
|
76
59
|
});
|
|
77
60
|
it('should accept a query builder', async () => {
|
|
@@ -83,8 +66,8 @@ describe('paginate', () => {
|
|
|
83
66
|
const query = {
|
|
84
67
|
path: '',
|
|
85
68
|
};
|
|
86
|
-
const queryBuilder = await
|
|
87
|
-
const result = await (0,
|
|
69
|
+
const queryBuilder = await catRepo.createQueryBuilder('cats');
|
|
70
|
+
const result = await (0, index_1.paginate)(query, queryBuilder, config);
|
|
88
71
|
expect(result.data).toStrictEqual(cats.slice(0, 1));
|
|
89
72
|
});
|
|
90
73
|
it('should accept a query builder with custom condition', async () => {
|
|
@@ -99,9 +82,9 @@ describe('paginate', () => {
|
|
|
99
82
|
const queryBuilder = await connection
|
|
100
83
|
.createQueryBuilder()
|
|
101
84
|
.select('cats')
|
|
102
|
-
.from(CatEntity, 'cats')
|
|
85
|
+
.from(cat_entity_1.CatEntity, 'cats')
|
|
103
86
|
.where('cats.color = :color', { color: 'white' });
|
|
104
|
-
const result = await (0,
|
|
87
|
+
const result = await (0, index_1.paginate)(query, queryBuilder, config);
|
|
105
88
|
expect(result.data).toStrictEqual(cats.slice(3, 4));
|
|
106
89
|
});
|
|
107
90
|
it('should default to page 1, if negative page is given', async () => {
|
|
@@ -113,7 +96,7 @@ describe('paginate', () => {
|
|
|
113
96
|
path: '',
|
|
114
97
|
page: -1,
|
|
115
98
|
};
|
|
116
|
-
const result = await (0,
|
|
99
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
117
100
|
expect(result.meta.currentPage).toBe(1);
|
|
118
101
|
expect(result.data).toStrictEqual(cats.slice(0, 1));
|
|
119
102
|
});
|
|
@@ -128,10 +111,10 @@ describe('paginate', () => {
|
|
|
128
111
|
page: 1,
|
|
129
112
|
limit: 20,
|
|
130
113
|
};
|
|
131
|
-
const result = await (0,
|
|
114
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
132
115
|
expect(result.data).toStrictEqual(cats.slice(0, 2));
|
|
133
116
|
});
|
|
134
|
-
it('should return correct links', async () => {
|
|
117
|
+
it('should return correct links for some results', async () => {
|
|
135
118
|
const config = {
|
|
136
119
|
sortableColumns: ['id'],
|
|
137
120
|
};
|
|
@@ -140,13 +123,31 @@ describe('paginate', () => {
|
|
|
140
123
|
page: 2,
|
|
141
124
|
limit: 2,
|
|
142
125
|
};
|
|
143
|
-
const { links } = await (0,
|
|
126
|
+
const { links } = await (0, index_1.paginate)(query, catRepo, config);
|
|
144
127
|
expect(links.first).toBe('?page=1&limit=2&sortBy=id:ASC');
|
|
145
128
|
expect(links.previous).toBe('?page=1&limit=2&sortBy=id:ASC');
|
|
146
129
|
expect(links.current).toBe('?page=2&limit=2&sortBy=id:ASC');
|
|
147
130
|
expect(links.next).toBe('?page=3&limit=2&sortBy=id:ASC');
|
|
148
131
|
expect(links.last).toBe('?page=3&limit=2&sortBy=id:ASC');
|
|
149
132
|
});
|
|
133
|
+
it('should return only current link if zero results', async () => {
|
|
134
|
+
const config = {
|
|
135
|
+
sortableColumns: ['id'],
|
|
136
|
+
searchableColumns: ['name'],
|
|
137
|
+
};
|
|
138
|
+
const query = {
|
|
139
|
+
path: '',
|
|
140
|
+
page: 1,
|
|
141
|
+
limit: 2,
|
|
142
|
+
search: 'Pluto',
|
|
143
|
+
};
|
|
144
|
+
const { links } = await (0, index_1.paginate)(query, catRepo, config);
|
|
145
|
+
expect(links.first).toBe(undefined);
|
|
146
|
+
expect(links.previous).toBe(undefined);
|
|
147
|
+
expect(links.current).toBe('?page=1&limit=2&sortBy=id:ASC&search=Pluto');
|
|
148
|
+
expect(links.next).toBe(undefined);
|
|
149
|
+
expect(links.last).toBe(undefined);
|
|
150
|
+
});
|
|
150
151
|
it('should default to defaultSortBy if query sortBy does not exist', async () => {
|
|
151
152
|
const config = {
|
|
152
153
|
sortableColumns: ['id', 'createdAt'],
|
|
@@ -155,7 +156,7 @@ describe('paginate', () => {
|
|
|
155
156
|
const query = {
|
|
156
157
|
path: '',
|
|
157
158
|
};
|
|
158
|
-
const result = await (0,
|
|
159
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
159
160
|
expect(result.meta.sortBy).toStrictEqual([['id', 'DESC']]);
|
|
160
161
|
expect(result.data).toStrictEqual(cats.slice(0).reverse());
|
|
161
162
|
});
|
|
@@ -170,7 +171,7 @@ describe('paginate', () => {
|
|
|
170
171
|
['name', 'ASC'],
|
|
171
172
|
],
|
|
172
173
|
};
|
|
173
|
-
const result = await (0,
|
|
174
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
174
175
|
expect(result.meta.sortBy).toStrictEqual([
|
|
175
176
|
['color', 'DESC'],
|
|
176
177
|
['name', 'ASC'],
|
|
@@ -186,11 +187,110 @@ describe('paginate', () => {
|
|
|
186
187
|
path: '',
|
|
187
188
|
search: 'i',
|
|
188
189
|
};
|
|
189
|
-
const result = await (0,
|
|
190
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
190
191
|
expect(result.meta.search).toStrictEqual('i');
|
|
191
192
|
expect(result.data).toStrictEqual([cats[0], cats[1], cats[3], cats[4]]);
|
|
192
193
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&search=i');
|
|
193
194
|
});
|
|
195
|
+
it('should return result based on search term on many-to-one relation', async () => {
|
|
196
|
+
const config = {
|
|
197
|
+
relations: ['cat'],
|
|
198
|
+
sortableColumns: ['id', 'name'],
|
|
199
|
+
searchableColumns: ['name', 'cat.name'],
|
|
200
|
+
};
|
|
201
|
+
const query = {
|
|
202
|
+
path: '',
|
|
203
|
+
search: 'Milo',
|
|
204
|
+
};
|
|
205
|
+
const result = await (0, index_1.paginate)(query, catToyRepo, config);
|
|
206
|
+
expect(result.meta.search).toStrictEqual('Milo');
|
|
207
|
+
expect(result.data).toStrictEqual([catToys[0], catToys[1], catToys[2]]);
|
|
208
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&search=Milo');
|
|
209
|
+
});
|
|
210
|
+
it('should return result based on search term on one-to-many relation', async () => {
|
|
211
|
+
const config = {
|
|
212
|
+
relations: ['toys'],
|
|
213
|
+
sortableColumns: ['id', 'name'],
|
|
214
|
+
searchableColumns: ['name', 'toys.name'],
|
|
215
|
+
};
|
|
216
|
+
const query = {
|
|
217
|
+
path: '',
|
|
218
|
+
search: 'Mouse',
|
|
219
|
+
};
|
|
220
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
221
|
+
expect(result.meta.search).toStrictEqual('Mouse');
|
|
222
|
+
const toy = (0, lodash_1.clone)(catToys[1]);
|
|
223
|
+
delete toy.cat;
|
|
224
|
+
const toy2 = (0, lodash_1.clone)(catToys[2]);
|
|
225
|
+
delete toy2.cat;
|
|
226
|
+
expect(result.data).toStrictEqual([Object.assign((0, lodash_1.clone)(cats[0]), { toys: [toy, toy2] })]);
|
|
227
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&search=Mouse');
|
|
228
|
+
});
|
|
229
|
+
it('should return result based on search term on one-to-one relation', async () => {
|
|
230
|
+
const config = {
|
|
231
|
+
relations: ['cat'],
|
|
232
|
+
sortableColumns: ['id', 'name', 'cat.id'],
|
|
233
|
+
};
|
|
234
|
+
const query = {
|
|
235
|
+
path: '',
|
|
236
|
+
sortBy: [['cat.id', 'DESC']],
|
|
237
|
+
};
|
|
238
|
+
const result = await (0, index_1.paginate)(query, catHomeRepo, config);
|
|
239
|
+
expect(result.meta.sortBy).toStrictEqual([['cat.id', 'DESC']]);
|
|
240
|
+
expect(result.data).toStrictEqual([catHomes[0], catHomes[1]].sort((a, b) => b.cat.id - a.cat.id));
|
|
241
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=cat.id:DESC');
|
|
242
|
+
});
|
|
243
|
+
it('should return result based on sort and search on many-to-one relation', async () => {
|
|
244
|
+
const config = {
|
|
245
|
+
relations: ['cat'],
|
|
246
|
+
sortableColumns: ['id', 'name', 'cat.id'],
|
|
247
|
+
searchableColumns: ['name', 'cat.name'],
|
|
248
|
+
};
|
|
249
|
+
const query = {
|
|
250
|
+
path: '',
|
|
251
|
+
sortBy: [['cat.id', 'DESC']],
|
|
252
|
+
search: 'Milo',
|
|
253
|
+
};
|
|
254
|
+
const result = await (0, index_1.paginate)(query, catToyRepo, config);
|
|
255
|
+
expect(result.meta.search).toStrictEqual('Milo');
|
|
256
|
+
expect(result.data).toStrictEqual([catToys[0], catToys[1], catToys[2]].sort((a, b) => b.cat.id - a.cat.id));
|
|
257
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=cat.id:DESC&search=Milo');
|
|
258
|
+
});
|
|
259
|
+
it('should return result based on sort on one-to-many relation', async () => {
|
|
260
|
+
const config = {
|
|
261
|
+
relations: ['toys'],
|
|
262
|
+
sortableColumns: ['id', 'name', 'toys.id'],
|
|
263
|
+
searchableColumns: ['name', 'toys.name'],
|
|
264
|
+
};
|
|
265
|
+
const query = {
|
|
266
|
+
path: '',
|
|
267
|
+
sortBy: [['toys.id', 'DESC']],
|
|
268
|
+
search: 'Mouse',
|
|
269
|
+
};
|
|
270
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
271
|
+
expect(result.meta.search).toStrictEqual('Mouse');
|
|
272
|
+
const toy1 = (0, lodash_1.clone)(catToys[1]);
|
|
273
|
+
delete toy1.cat;
|
|
274
|
+
const toy2 = (0, lodash_1.clone)(catToys[2]);
|
|
275
|
+
delete toy2.cat;
|
|
276
|
+
expect(result.data).toStrictEqual([Object.assign((0, lodash_1.clone)(cats[0]), { toys: [toy2, toy1] })]);
|
|
277
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=toys.id:DESC&search=Mouse');
|
|
278
|
+
});
|
|
279
|
+
it('should return result based on sort on one-to-one relation', async () => {
|
|
280
|
+
const config = {
|
|
281
|
+
relations: ['cat'],
|
|
282
|
+
sortableColumns: ['id', 'name'],
|
|
283
|
+
searchableColumns: ['name', 'cat.name'],
|
|
284
|
+
};
|
|
285
|
+
const query = {
|
|
286
|
+
path: '',
|
|
287
|
+
search: 'Garfield',
|
|
288
|
+
};
|
|
289
|
+
const result = await (0, index_1.paginate)(query, catHomeRepo, config);
|
|
290
|
+
expect(result.meta.search).toStrictEqual('Garfield');
|
|
291
|
+
expect(result.data).toStrictEqual([catHomes[1]]);
|
|
292
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&search=Garfield');
|
|
293
|
+
});
|
|
194
294
|
it('should return result based on search term and searchBy columns', async () => {
|
|
195
295
|
const config = {
|
|
196
296
|
sortableColumns: ['id', 'name', 'color'],
|
|
@@ -203,7 +303,7 @@ describe('paginate', () => {
|
|
|
203
303
|
search: searchTerm,
|
|
204
304
|
searchBy: ['color'],
|
|
205
305
|
};
|
|
206
|
-
const result = await (0,
|
|
306
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
207
307
|
expect(result.meta.search).toStrictEqual(searchTerm);
|
|
208
308
|
expect(result.meta.searchBy).toStrictEqual(['color']);
|
|
209
309
|
expect(result.data).toStrictEqual(expectedResultData);
|
|
@@ -216,7 +316,7 @@ describe('paginate', () => {
|
|
|
216
316
|
color: 'white',
|
|
217
317
|
},
|
|
218
318
|
filterableColumns: {
|
|
219
|
-
name: [
|
|
319
|
+
name: [index_1.FilterOperator.NOT],
|
|
220
320
|
},
|
|
221
321
|
};
|
|
222
322
|
const query = {
|
|
@@ -225,13 +325,86 @@ describe('paginate', () => {
|
|
|
225
325
|
name: '$not:Leche',
|
|
226
326
|
},
|
|
227
327
|
};
|
|
228
|
-
const result = await (0,
|
|
328
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
229
329
|
expect(result.meta.filter).toStrictEqual({
|
|
230
330
|
name: '$not:Leche',
|
|
231
331
|
});
|
|
232
332
|
expect(result.data).toStrictEqual([cats[3]]);
|
|
233
333
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.name=$not:Leche');
|
|
234
334
|
});
|
|
335
|
+
it('should return result based on filter on many-to-one relation', async () => {
|
|
336
|
+
const config = {
|
|
337
|
+
relations: ['cat'],
|
|
338
|
+
sortableColumns: ['id', 'name'],
|
|
339
|
+
filterableColumns: {
|
|
340
|
+
'cat.name': [index_1.FilterOperator.NOT],
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
const query = {
|
|
344
|
+
path: '',
|
|
345
|
+
filter: {
|
|
346
|
+
'cat.name': '$not:Milo',
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
const result = await (0, index_1.paginate)(query, catToyRepo, config);
|
|
350
|
+
expect(result.meta.filter).toStrictEqual({
|
|
351
|
+
'cat.name': '$not:Milo',
|
|
352
|
+
});
|
|
353
|
+
expect(result.data).toStrictEqual([catToys[3]]);
|
|
354
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.cat.name=$not:Milo');
|
|
355
|
+
});
|
|
356
|
+
it('should return result based on filter on one-to-many relation', async () => {
|
|
357
|
+
const config = {
|
|
358
|
+
relations: ['toys'],
|
|
359
|
+
sortableColumns: ['id', 'name'],
|
|
360
|
+
filterableColumns: {
|
|
361
|
+
'toys.name': [index_1.FilterOperator.NOT],
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
const query = {
|
|
365
|
+
path: '',
|
|
366
|
+
filter: {
|
|
367
|
+
'toys.name': '$not:Stuffed Mouse',
|
|
368
|
+
},
|
|
369
|
+
};
|
|
370
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
371
|
+
const cat1 = (0, lodash_1.clone)(cats[0]);
|
|
372
|
+
const cat2 = (0, lodash_1.clone)(cats[1]);
|
|
373
|
+
const catToys1 = (0, lodash_1.clone)(catToys[0]);
|
|
374
|
+
const catToys2 = (0, lodash_1.clone)(catToys[2]);
|
|
375
|
+
const catToys3 = (0, lodash_1.clone)(catToys[3]);
|
|
376
|
+
delete catToys1.cat;
|
|
377
|
+
delete catToys2.cat;
|
|
378
|
+
delete catToys3.cat;
|
|
379
|
+
cat1.toys = [catToys1, catToys2];
|
|
380
|
+
cat2.toys = [catToys3];
|
|
381
|
+
expect(result.meta.filter).toStrictEqual({
|
|
382
|
+
'toys.name': '$not:Stuffed Mouse',
|
|
383
|
+
});
|
|
384
|
+
expect(result.data).toStrictEqual([cat1, cat2]);
|
|
385
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.toys.name=$not:Stuffed Mouse');
|
|
386
|
+
});
|
|
387
|
+
it('should return result based on where config and filter on one-to-one relation', async () => {
|
|
388
|
+
const config = {
|
|
389
|
+
relations: ['cat'],
|
|
390
|
+
sortableColumns: ['id', 'name'],
|
|
391
|
+
filterableColumns: {
|
|
392
|
+
'cat.name': [index_1.FilterOperator.NOT],
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
const query = {
|
|
396
|
+
path: '',
|
|
397
|
+
filter: {
|
|
398
|
+
'cat.name': '$not:Garfield',
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
const result = await (0, index_1.paginate)(query, catHomeRepo, config);
|
|
402
|
+
expect(result.meta.filter).toStrictEqual({
|
|
403
|
+
'cat.name': '$not:Garfield',
|
|
404
|
+
});
|
|
405
|
+
expect(result.data).toStrictEqual([catHomes[0]]);
|
|
406
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.cat.name=$not:Garfield');
|
|
407
|
+
});
|
|
235
408
|
it('should return result based on where array and filter', async () => {
|
|
236
409
|
const config = {
|
|
237
410
|
sortableColumns: ['id'],
|
|
@@ -244,7 +417,7 @@ describe('paginate', () => {
|
|
|
244
417
|
},
|
|
245
418
|
],
|
|
246
419
|
filterableColumns: {
|
|
247
|
-
name: [
|
|
420
|
+
name: [index_1.FilterOperator.NOT],
|
|
248
421
|
},
|
|
249
422
|
};
|
|
250
423
|
const query = {
|
|
@@ -253,7 +426,7 @@ describe('paginate', () => {
|
|
|
253
426
|
name: '$not:Leche',
|
|
254
427
|
},
|
|
255
428
|
};
|
|
256
|
-
const result = await (0,
|
|
429
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
257
430
|
expect(result.meta.filter).toStrictEqual({
|
|
258
431
|
name: '$not:Leche',
|
|
259
432
|
});
|
|
@@ -264,8 +437,8 @@ describe('paginate', () => {
|
|
|
264
437
|
const config = {
|
|
265
438
|
sortableColumns: ['id'],
|
|
266
439
|
filterableColumns: {
|
|
267
|
-
name: [
|
|
268
|
-
color: [
|
|
440
|
+
name: [index_1.FilterOperator.NOT],
|
|
441
|
+
color: [index_1.FilterOperator.EQ],
|
|
269
442
|
},
|
|
270
443
|
};
|
|
271
444
|
const query = {
|
|
@@ -275,7 +448,7 @@ describe('paginate', () => {
|
|
|
275
448
|
color: 'white',
|
|
276
449
|
},
|
|
277
450
|
};
|
|
278
|
-
const result = await (0,
|
|
451
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
279
452
|
expect(result.meta.filter).toStrictEqual({
|
|
280
453
|
name: '$not:Leche',
|
|
281
454
|
color: 'white',
|
|
@@ -288,7 +461,7 @@ describe('paginate', () => {
|
|
|
288
461
|
sortableColumns: ['id'],
|
|
289
462
|
searchableColumns: ['name', 'color'],
|
|
290
463
|
filterableColumns: {
|
|
291
|
-
id: [
|
|
464
|
+
id: [index_1.FilterOperator.NOT, index_1.FilterOperator.IN],
|
|
292
465
|
},
|
|
293
466
|
};
|
|
294
467
|
const query = {
|
|
@@ -298,7 +471,7 @@ describe('paginate', () => {
|
|
|
298
471
|
id: '$not:$in:1,2,5',
|
|
299
472
|
},
|
|
300
473
|
};
|
|
301
|
-
const result = await (0,
|
|
474
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
302
475
|
expect(result.meta.search).toStrictEqual('white');
|
|
303
476
|
expect(result.meta.filter).toStrictEqual({ id: '$not:$in:1,2,5' });
|
|
304
477
|
expect(result.data).toStrictEqual([cats[3]]);
|
|
@@ -311,7 +484,7 @@ describe('paginate', () => {
|
|
|
311
484
|
color: (0, typeorm_1.In)(['black', 'white']),
|
|
312
485
|
},
|
|
313
486
|
filterableColumns: {
|
|
314
|
-
id: [
|
|
487
|
+
id: [index_1.FilterOperator.NOT, index_1.FilterOperator.IN],
|
|
315
488
|
},
|
|
316
489
|
};
|
|
317
490
|
const query = {
|
|
@@ -320,7 +493,7 @@ describe('paginate', () => {
|
|
|
320
493
|
id: '$not:$in:1,2,5',
|
|
321
494
|
},
|
|
322
495
|
};
|
|
323
|
-
const result = await (0,
|
|
496
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
324
497
|
expect(result.data).toStrictEqual([cats[2], cats[3]]);
|
|
325
498
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.id=$not:$in:1,2,5');
|
|
326
499
|
});
|
|
@@ -328,7 +501,7 @@ describe('paginate', () => {
|
|
|
328
501
|
const config = {
|
|
329
502
|
sortableColumns: ['id'],
|
|
330
503
|
filterableColumns: {
|
|
331
|
-
age: [
|
|
504
|
+
age: [index_1.FilterOperator.GTE],
|
|
332
505
|
},
|
|
333
506
|
};
|
|
334
507
|
const query = {
|
|
@@ -337,7 +510,7 @@ describe('paginate', () => {
|
|
|
337
510
|
age: '$gte:4',
|
|
338
511
|
},
|
|
339
512
|
};
|
|
340
|
-
const result = await (0,
|
|
513
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
341
514
|
expect(result.data).toStrictEqual([cats[0], cats[1], cats[2]]);
|
|
342
515
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$gte:4');
|
|
343
516
|
});
|
|
@@ -345,7 +518,7 @@ describe('paginate', () => {
|
|
|
345
518
|
const config = {
|
|
346
519
|
sortableColumns: ['id'],
|
|
347
520
|
filterableColumns: {
|
|
348
|
-
age: [
|
|
521
|
+
age: [index_1.FilterOperator.BTW],
|
|
349
522
|
},
|
|
350
523
|
};
|
|
351
524
|
const query = {
|
|
@@ -354,7 +527,7 @@ describe('paginate', () => {
|
|
|
354
527
|
age: '$btw:4,5',
|
|
355
528
|
},
|
|
356
529
|
};
|
|
357
|
-
const result = await (0,
|
|
530
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
358
531
|
expect(result.data).toStrictEqual([cats[1], cats[2]]);
|
|
359
532
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$btw:4,5');
|
|
360
533
|
});
|
|
@@ -362,7 +535,7 @@ describe('paginate', () => {
|
|
|
362
535
|
const config = {
|
|
363
536
|
sortableColumns: ['id'],
|
|
364
537
|
filterableColumns: {
|
|
365
|
-
age: [
|
|
538
|
+
age: [index_1.FilterOperator.NULL],
|
|
366
539
|
},
|
|
367
540
|
};
|
|
368
541
|
const query = {
|
|
@@ -371,7 +544,7 @@ describe('paginate', () => {
|
|
|
371
544
|
age: '$null',
|
|
372
545
|
},
|
|
373
546
|
};
|
|
374
|
-
const result = await (0,
|
|
547
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
375
548
|
expect(result.data).toStrictEqual([cats[4]]);
|
|
376
549
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$null');
|
|
377
550
|
});
|
|
@@ -379,7 +552,7 @@ describe('paginate', () => {
|
|
|
379
552
|
const config = {
|
|
380
553
|
sortableColumns: ['id'],
|
|
381
554
|
filterableColumns: {
|
|
382
|
-
age: [
|
|
555
|
+
age: [index_1.FilterOperator.NOT, index_1.FilterOperator.NULL],
|
|
383
556
|
},
|
|
384
557
|
};
|
|
385
558
|
const query = {
|
|
@@ -388,7 +561,7 @@ describe('paginate', () => {
|
|
|
388
561
|
age: '$not:$null',
|
|
389
562
|
},
|
|
390
563
|
};
|
|
391
|
-
const result = await (0,
|
|
564
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
392
565
|
expect(result.data).toStrictEqual([cats[0], cats[1], cats[2], cats[3]]);
|
|
393
566
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$not:$null');
|
|
394
567
|
});
|
|
@@ -396,7 +569,7 @@ describe('paginate', () => {
|
|
|
396
569
|
const config = {
|
|
397
570
|
sortableColumns: ['id'],
|
|
398
571
|
filterableColumns: {
|
|
399
|
-
name: [
|
|
572
|
+
name: [index_1.FilterOperator.NOT, index_1.FilterOperator.NULL],
|
|
400
573
|
},
|
|
401
574
|
};
|
|
402
575
|
const query = {
|
|
@@ -405,7 +578,7 @@ describe('paginate', () => {
|
|
|
405
578
|
age: '$not:$null',
|
|
406
579
|
},
|
|
407
580
|
};
|
|
408
|
-
const result = await (0,
|
|
581
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
409
582
|
expect(result.data).toStrictEqual(cats);
|
|
410
583
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$not:$null');
|
|
411
584
|
});
|
|
@@ -413,7 +586,7 @@ describe('paginate', () => {
|
|
|
413
586
|
const config = {
|
|
414
587
|
sortableColumns: ['id'],
|
|
415
588
|
filterableColumns: {
|
|
416
|
-
age: [
|
|
589
|
+
age: [index_1.FilterOperator.NOT],
|
|
417
590
|
},
|
|
418
591
|
};
|
|
419
592
|
const query = {
|
|
@@ -422,7 +595,7 @@ describe('paginate', () => {
|
|
|
422
595
|
age: '$not:$null',
|
|
423
596
|
},
|
|
424
597
|
};
|
|
425
|
-
const result = await (0,
|
|
598
|
+
const result = await (0, index_1.paginate)(query, catRepo, config);
|
|
426
599
|
expect(result.data).toStrictEqual(cats);
|
|
427
600
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.age=$not:$null');
|
|
428
601
|
});
|
|
@@ -434,7 +607,7 @@ describe('paginate', () => {
|
|
|
434
607
|
path: '',
|
|
435
608
|
};
|
|
436
609
|
try {
|
|
437
|
-
await (0,
|
|
610
|
+
await (0, index_1.paginate)(query, catRepo, config);
|
|
438
611
|
}
|
|
439
612
|
catch (err) {
|
|
440
613
|
expect(err).toBeInstanceOf(common_1.HttpException);
|
|
@@ -452,7 +625,7 @@ describe('paginate', () => {
|
|
|
452
625
|
{ operator: '$not', result: true },
|
|
453
626
|
{ operator: '$fake', result: false },
|
|
454
627
|
])('should check operator "$operator" valid is $result', ({ operator, result }) => {
|
|
455
|
-
expect((0,
|
|
628
|
+
expect((0, index_1.isOperator)(operator)).toStrictEqual(result);
|
|
456
629
|
});
|
|
457
630
|
it.each([
|
|
458
631
|
{ operator: '$eq', name: 'Equal' },
|
|
@@ -465,7 +638,7 @@ describe('paginate', () => {
|
|
|
465
638
|
{ operator: '$btw', name: 'Between' },
|
|
466
639
|
{ operator: '$not', name: 'Not' },
|
|
467
640
|
])('should get operator function $name for "$operator"', ({ operator, name }) => {
|
|
468
|
-
const func =
|
|
641
|
+
const func = index_1.OperatorSymbolToFunction.get(operator);
|
|
469
642
|
expect(func.name).toStrictEqual(name);
|
|
470
643
|
});
|
|
471
644
|
it.each([
|
|
@@ -482,7 +655,7 @@ describe('paginate', () => {
|
|
|
482
655
|
{ string: '', tokens: [null, '$eq', ''] },
|
|
483
656
|
{ string: '$eq:$not:$in:value', tokens: [] },
|
|
484
657
|
])('should get filter tokens for "$string"', ({ string, tokens }) => {
|
|
485
|
-
expect((0,
|
|
658
|
+
expect((0, index_1.getFilterTokens)(string)).toStrictEqual(tokens);
|
|
486
659
|
});
|
|
487
660
|
});
|
|
488
661
|
//# sourceMappingURL=paginate.spec.js.map
|