nestjs-paginate 4.14.0 → 5.0.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.
- package/README.md +36 -59
- package/lib/helper.d.ts +1 -1
- package/lib/helper.js +5 -5
- package/lib/helper.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -93,12 +93,6 @@ http://localhost:3000/cats?limit=5&page=2&sortBy=color:DESC&search=i&filter.age=
|
|
|
93
93
|
}
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
-
Array values for filter operators such as `$in` should be provided as comma-separated values:
|
|
97
|
-
|
|
98
|
-
```
|
|
99
|
-
http://localhost:3000/cats?filter.name=$in:George,Milo
|
|
100
|
-
```
|
|
101
|
-
|
|
102
96
|
#### Code
|
|
103
97
|
|
|
104
98
|
```ts
|
|
@@ -265,51 +259,17 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|
|
265
259
|
* Type: string
|
|
266
260
|
* Description: Allow user to choose between limit/offset and take/skip.
|
|
267
261
|
* Default: PaginationType.TAKE_AND_SKIP
|
|
262
|
+
*
|
|
263
|
+
* However, using limit/offset can return unexpected results.
|
|
264
|
+
* For more information see:
|
|
265
|
+
* [#477](https://github.com/ppetzold/nestjs-paginate/issues/477)
|
|
266
|
+
* [#4742](https://github.com/typeorm/typeorm/issues/4742)
|
|
267
|
+
* [#5670](https://github.com/typeorm/typeorm/issues/5670)
|
|
268
268
|
*/
|
|
269
269
|
paginationType: PaginationType.LIMIT_AND_OFFSET,
|
|
270
270
|
}
|
|
271
271
|
```
|
|
272
272
|
|
|
273
|
-
## Eager loading
|
|
274
|
-
|
|
275
|
-
Eager loading should work with typeorm's eager property out the box. Like so
|
|
276
|
-
|
|
277
|
-
```typescript
|
|
278
|
-
import { Entity, OneToMany } from 'typeorm'
|
|
279
|
-
|
|
280
|
-
@Entity()
|
|
281
|
-
export class CatEntity {
|
|
282
|
-
@PrimaryGeneratedColumn()
|
|
283
|
-
id: number
|
|
284
|
-
|
|
285
|
-
@Column('text')
|
|
286
|
-
name: string
|
|
287
|
-
|
|
288
|
-
@Column('text')
|
|
289
|
-
color: string
|
|
290
|
-
|
|
291
|
-
@Column('int')
|
|
292
|
-
age: number
|
|
293
|
-
|
|
294
|
-
@OneToMany(() => CatToyEntity, (catToy) => catToy.cat, {
|
|
295
|
-
eager: true,
|
|
296
|
-
})
|
|
297
|
-
toys: CatToyEntity[]
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// service
|
|
301
|
-
class CatService {
|
|
302
|
-
constructor(private readonly catsRepository: Repository<CatEntity>) {}
|
|
303
|
-
|
|
304
|
-
public findAll(query: PaginateQuery): Promise<Paginated<CatEntity>> {
|
|
305
|
-
return paginate(query, this.catsRepository, {
|
|
306
|
-
sortableColumns: ['id', 'name', 'color', 'age'],
|
|
307
|
-
loadEagerRelations: true, // set this property as true to enable the eager loading
|
|
308
|
-
})
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
```
|
|
312
|
-
|
|
313
273
|
## Usage with Query Builder
|
|
314
274
|
|
|
315
275
|
You can paginate custom queries by passing on the query builder:
|
|
@@ -351,16 +311,26 @@ const config: PaginateConfig<CatEntity> = {
|
|
|
351
311
|
const result = await paginate<CatEntity>(query, catRepo, config)
|
|
352
312
|
```
|
|
353
313
|
|
|
354
|
-
|
|
314
|
+
**Note:** Embedded columns on relations have to be wrapped with brackets:
|
|
355
315
|
|
|
356
|
-
|
|
316
|
+
```typescript
|
|
317
|
+
const config: PaginateConfig<CatEntity> = {
|
|
318
|
+
sortableColumns: ['id', 'name', 'toys.(size.height)', 'toys.(size.width)'],
|
|
319
|
+
searchableColumns: ['name'],
|
|
320
|
+
relations: ['toys'],
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## Usage with Nested Relations
|
|
325
|
+
|
|
326
|
+
Similar as with relations, you can specify nested relations for sorting, filtering and searching:
|
|
357
327
|
|
|
358
328
|
### Example
|
|
359
329
|
|
|
360
330
|
#### Endpoint
|
|
361
331
|
|
|
362
332
|
```url
|
|
363
|
-
http://localhost:3000/cats?filter.home.pillows.color
|
|
333
|
+
http://localhost:3000/cats?filter.home.pillows.color=pink
|
|
364
334
|
```
|
|
365
335
|
|
|
366
336
|
#### Code
|
|
@@ -376,29 +346,36 @@ const config: PaginateConfig<CatEntity> = {
|
|
|
376
346
|
const result = await paginate<CatEntity>(query, catRepo, config)
|
|
377
347
|
```
|
|
378
348
|
|
|
379
|
-
## Usage
|
|
349
|
+
## Usage with Eager Loading
|
|
380
350
|
|
|
381
|
-
|
|
351
|
+
Eager loading should work with TypeORM's eager property out the box:
|
|
382
352
|
|
|
383
353
|
### Example
|
|
384
354
|
|
|
385
355
|
#### Code
|
|
386
356
|
|
|
387
357
|
```typescript
|
|
358
|
+
@Entity()
|
|
359
|
+
export class CatEntity {
|
|
360
|
+
// ...
|
|
361
|
+
|
|
362
|
+
@OneToMany(() => CatToyEntity, (catToy) => catToy.cat, {
|
|
363
|
+
eager: true,
|
|
364
|
+
})
|
|
365
|
+
toys: CatToyEntity[]
|
|
366
|
+
}
|
|
367
|
+
|
|
388
368
|
const config: PaginateConfig<CatEntity> = {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
369
|
+
loadEagerRelations: true,
|
|
370
|
+
sortableColumns: ['id', 'name', 'toys.name'],
|
|
371
|
+
filterableColumns: {
|
|
372
|
+
'toys.name': [FilterOperator.IN],
|
|
373
|
+
},
|
|
392
374
|
}
|
|
393
375
|
|
|
394
376
|
const result = await paginate<CatEntity>(query, catRepo, config)
|
|
395
377
|
```
|
|
396
378
|
|
|
397
|
-
> However, using `limit`/`offset` can return unexpected results.
|
|
398
|
-
> For more information
|
|
399
|
-
> see [#477](https://github.com/ppetzold/nestjs-paginate/issues/477), [#4742](https://github.com/typeorm/typeorm/issues/4742)
|
|
400
|
-
> and [#5670](https://github.com/typeorm/typeorm/issues/5670).
|
|
401
|
-
|
|
402
379
|
## Single Filters
|
|
403
380
|
|
|
404
381
|
Filter operators must be whitelisted per column in `PaginateConfig`.
|
package/lib/helper.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare const positiveNumberOrDefault: (value: number | undefined, defaul
|
|
|
14
14
|
export type ColumnProperties = {
|
|
15
15
|
propertyPath?: string;
|
|
16
16
|
propertyName: string;
|
|
17
|
-
|
|
17
|
+
isNested: boolean;
|
|
18
18
|
column: string;
|
|
19
19
|
};
|
|
20
20
|
export declare function getPropertiesByColumnName(column: string): ColumnProperties;
|
package/lib/helper.js
CHANGED
|
@@ -7,20 +7,20 @@ function getPropertiesByColumnName(column) {
|
|
|
7
7
|
const propertyPath = column.split('.');
|
|
8
8
|
if (propertyPath.length > 1) {
|
|
9
9
|
const propertyNamePath = propertyPath.slice(1);
|
|
10
|
-
let
|
|
10
|
+
let isNested = false, propertyName = propertyNamePath.join('.');
|
|
11
11
|
if (!propertyName.startsWith('(') && propertyNamePath.length > 1) {
|
|
12
|
-
|
|
12
|
+
isNested = true;
|
|
13
13
|
}
|
|
14
14
|
propertyName = propertyName.replace('(', '').replace(')', '');
|
|
15
15
|
return {
|
|
16
16
|
propertyPath: propertyPath[0],
|
|
17
17
|
propertyName,
|
|
18
|
-
|
|
18
|
+
isNested,
|
|
19
19
|
column: `${propertyPath[0]}.${propertyName}`,
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
|
-
return { propertyName: propertyPath[0],
|
|
23
|
+
return { propertyName: propertyPath[0], isNested: false, column: propertyPath[0] };
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.getPropertiesByColumnName = getPropertiesByColumnName;
|
|
@@ -73,7 +73,7 @@ function fixColumnAlias(properties, alias, isRelation = false, isVirtualProperty
|
|
|
73
73
|
if (isVirtualProperty && query) {
|
|
74
74
|
return `(${query(`${alias}_${properties.propertyPath}`)})`; // () is needed to avoid parameter conflict
|
|
75
75
|
}
|
|
76
|
-
else if ((isVirtualProperty && !query) || properties.
|
|
76
|
+
else if ((isVirtualProperty && !query) || properties.isNested) {
|
|
77
77
|
return `${alias}_${properties.propertyPath}_${properties.propertyName}`;
|
|
78
78
|
}
|
|
79
79
|
else {
|
package/lib/helper.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":";;;AAoCO,MAAM,uBAAuB,GAAG,CAAC,KAAyB,EAAE,YAAoB,EAAE,WAAkB,CAAC,EAAE,EAAE,CAC5G,KAAK,KAAK,SAAS,IAAI,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAA;AADrD,QAAA,uBAAuB,2BAC8B;AAIlE,SAAgB,yBAAyB,CAAC,MAAc;IACpD,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACtC,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;QACzB,MAAM,gBAAgB,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAC9C,IAAI,
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":";;;AAoCO,MAAM,uBAAuB,GAAG,CAAC,KAAyB,EAAE,YAAoB,EAAE,WAAkB,CAAC,EAAE,EAAE,CAC5G,KAAK,KAAK,SAAS,IAAI,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAA;AADrD,QAAA,uBAAuB,2BAC8B;AAIlE,SAAgB,yBAAyB,CAAC,MAAc;IACpD,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACtC,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;QACzB,MAAM,gBAAgB,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QA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