nestjs-paginate 7.1.3 → 8.1.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 +27 -5
- package/lib/paginate.d.ts +6 -0
- package/lib/paginate.js +24 -10
- package/lib/paginate.js.map +1 -1
- package/lib/paginate.spec.js +83 -0
- package/lib/paginate.spec.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -98,7 +98,7 @@ http://localhost:3000/cats?limit=5&page=2&sortBy=color:DESC&search=i&filter.age=
|
|
|
98
98
|
```ts
|
|
99
99
|
import { Controller, Injectable, Get } from '@nestjs/common'
|
|
100
100
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
101
|
-
import { FilterOperator, Paginate, PaginateQuery, paginate, Paginated } from 'nestjs-paginate'
|
|
101
|
+
import { FilterOperator, FilterSuffix, Paginate, PaginateQuery, paginate, Paginated } from 'nestjs-paginate'
|
|
102
102
|
import { Repository, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
|
|
103
103
|
|
|
104
104
|
@Entity()
|
|
@@ -114,6 +114,12 @@ export class CatEntity {
|
|
|
114
114
|
|
|
115
115
|
@Column('int')
|
|
116
116
|
age: number
|
|
117
|
+
|
|
118
|
+
@Column({ nullable: true })
|
|
119
|
+
lastVetVisit: Date | null
|
|
120
|
+
|
|
121
|
+
@CreateDateColumn()
|
|
122
|
+
createdAt: string
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
@Injectable()
|
|
@@ -127,10 +133,12 @@ export class CatsService {
|
|
|
127
133
|
return paginate(query, this.catsRepository, {
|
|
128
134
|
sortableColumns: ['id', 'name', 'color', 'age'],
|
|
129
135
|
nullSort: 'last',
|
|
130
|
-
searchableColumns: ['name', 'color', 'age'],
|
|
131
136
|
defaultSortBy: [['id', 'DESC']],
|
|
137
|
+
searchableColumns: ['name', 'color', 'age'],
|
|
138
|
+
select: ['id', 'name', 'color', 'age', 'lastVetVisit'],
|
|
132
139
|
filterableColumns: {
|
|
133
|
-
|
|
140
|
+
name: [FilterOperator.EQ, FilterSuffix.NOT],
|
|
141
|
+
age: true,
|
|
134
142
|
},
|
|
135
143
|
})
|
|
136
144
|
}
|
|
@@ -184,12 +192,13 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|
|
184
192
|
|
|
185
193
|
/**
|
|
186
194
|
* Required: false
|
|
187
|
-
* Type:
|
|
195
|
+
* Type: (keyof CatEntity)[]
|
|
188
196
|
* Default: None
|
|
197
|
+
* Description: TypeORM partial selection. Limit selection further by using `select` query param.
|
|
189
198
|
* https://typeorm.io/select-query-builder#partial-selection
|
|
190
199
|
* Note: You must include the primary key in the selection.
|
|
191
200
|
*/
|
|
192
|
-
select: ['name', 'color'],
|
|
201
|
+
select: ['id', 'name', 'color'],
|
|
193
202
|
|
|
194
203
|
/**
|
|
195
204
|
* Required: false
|
|
@@ -247,6 +256,19 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|
|
247
256
|
*/
|
|
248
257
|
withDeleted: false,
|
|
249
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Required: false
|
|
261
|
+
* Type: string
|
|
262
|
+
* Description: Allow user to choose between limit/offset and take/skip.
|
|
263
|
+
* Default: PaginationType.LIMIT_AND_OFFSET
|
|
264
|
+
*
|
|
265
|
+
* However, using take/skip can cause problems with sorting and selections.
|
|
266
|
+
* For more information see:
|
|
267
|
+
* [#4742](https://github.com/typeorm/typeorm/issues/4742)
|
|
268
|
+
* [#5670](https://github.com/typeorm/typeorm/issues/5670)
|
|
269
|
+
*/
|
|
270
|
+
paginationType: PaginationType.TAKE_AND_SKIP,
|
|
271
|
+
|
|
250
272
|
/**
|
|
251
273
|
* Required: false
|
|
252
274
|
* Type: boolean
|
package/lib/paginate.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare class Paginated<T> {
|
|
|
13
13
|
sortBy: SortBy<T>;
|
|
14
14
|
searchBy: Column<T>[];
|
|
15
15
|
search: string;
|
|
16
|
+
select: string[];
|
|
16
17
|
filter?: {
|
|
17
18
|
[column: string]: string | string[];
|
|
18
19
|
};
|
|
@@ -25,6 +26,10 @@ export declare class Paginated<T> {
|
|
|
25
26
|
last?: string;
|
|
26
27
|
};
|
|
27
28
|
}
|
|
29
|
+
export declare enum PaginationType {
|
|
30
|
+
LIMIT_AND_OFFSET = "limit",
|
|
31
|
+
TAKE_AND_SKIP = "take"
|
|
32
|
+
}
|
|
28
33
|
export interface PaginateConfig<T> {
|
|
29
34
|
relations?: FindOptionsRelations<T> | RelationColumn<T>[];
|
|
30
35
|
sortableColumns: Column<T>[];
|
|
@@ -40,6 +45,7 @@ export interface PaginateConfig<T> {
|
|
|
40
45
|
};
|
|
41
46
|
loadEagerRelations?: boolean;
|
|
42
47
|
withDeleted?: boolean;
|
|
48
|
+
paginationType?: PaginationType;
|
|
43
49
|
relativePath?: boolean;
|
|
44
50
|
origin?: string;
|
|
45
51
|
}
|
package/lib/paginate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.paginate = exports.NO_PAGINATION = exports.DEFAULT_LIMIT = exports.DEFAULT_MAX_LIMIT = exports.Paginated = exports.FilterSuffix = exports.FilterOperator = void 0;
|
|
3
|
+
exports.paginate = exports.NO_PAGINATION = exports.DEFAULT_LIMIT = exports.DEFAULT_MAX_LIMIT = exports.PaginationType = exports.Paginated = exports.FilterSuffix = exports.FilterOperator = void 0;
|
|
4
4
|
const typeorm_1 = require("typeorm");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const lodash_1 = require("lodash");
|
|
@@ -13,10 +13,16 @@ const logger = new common_1.Logger('nestjs-paginate');
|
|
|
13
13
|
class Paginated {
|
|
14
14
|
}
|
|
15
15
|
exports.Paginated = Paginated;
|
|
16
|
+
var PaginationType;
|
|
17
|
+
(function (PaginationType) {
|
|
18
|
+
PaginationType["LIMIT_AND_OFFSET"] = "limit";
|
|
19
|
+
PaginationType["TAKE_AND_SKIP"] = "take";
|
|
20
|
+
})(PaginationType = exports.PaginationType || (exports.PaginationType = {}));
|
|
16
21
|
exports.DEFAULT_MAX_LIMIT = 100;
|
|
17
22
|
exports.DEFAULT_LIMIT = 20;
|
|
18
23
|
exports.NO_PAGINATION = 0;
|
|
19
24
|
async function paginate(query, repo, config) {
|
|
25
|
+
var _a;
|
|
20
26
|
const page = (0, helper_1.positiveNumberOrDefault)(query.page, 1, 1);
|
|
21
27
|
const defaultLimit = config.defaultLimit || exports.DEFAULT_LIMIT;
|
|
22
28
|
const maxLimit = (0, helper_1.positiveNumberOrDefault)(config.maxLimit, exports.DEFAULT_MAX_LIMIT);
|
|
@@ -33,7 +39,14 @@ async function paginate(query, repo, config) {
|
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
if (isPaginated) {
|
|
36
|
-
|
|
42
|
+
// Allow user to choose between limit/offset and take/skip.
|
|
43
|
+
// However, using take/skip can cause problems with sorting and selections.
|
|
44
|
+
if (config.paginationType === PaginationType.TAKE_AND_SKIP) {
|
|
45
|
+
queryBuilder.take(limit).skip((page - 1) * limit);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
queryBuilder.limit(limit).offset((page - 1) * limit);
|
|
49
|
+
}
|
|
37
50
|
}
|
|
38
51
|
if (config.relations) {
|
|
39
52
|
// relations: ["relation"]
|
|
@@ -88,15 +101,12 @@ async function paginate(query, repo, config) {
|
|
|
88
101
|
}
|
|
89
102
|
// When we partial select the columns (main or relation) we must add the primary key column otherwise
|
|
90
103
|
// typeorm will not be able to map the result.
|
|
91
|
-
const selectParams = config.select
|
|
104
|
+
const selectParams = config.select && query.select ? config.select.filter((column) => query.select.includes(column)) : config.select;
|
|
92
105
|
if ((selectParams === null || selectParams === void 0 ? void 0 : selectParams.length) > 0 && (0, helper_1.includesAllPrimaryKeyColumns)(queryBuilder, selectParams)) {
|
|
93
106
|
const cols = selectParams.reduce((cols, currentCol) => {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const isRelation = (0, helper_1.checkIsRelation)(queryBuilder, columnProperties.propertyPath);
|
|
98
|
-
cols.push((0, helper_1.fixColumnAlias)(columnProperties, queryBuilder.alias, isRelation));
|
|
99
|
-
}
|
|
107
|
+
const columnProperties = (0, helper_1.getPropertiesByColumnName)(currentCol);
|
|
108
|
+
const isRelation = (0, helper_1.checkIsRelation)(queryBuilder, columnProperties.propertyPath);
|
|
109
|
+
cols.push((0, helper_1.fixColumnAlias)(columnProperties, queryBuilder.alias, isRelation));
|
|
100
110
|
return cols;
|
|
101
111
|
}, []);
|
|
102
112
|
queryBuilder.select(cols);
|
|
@@ -164,11 +174,14 @@ async function paginate(query, repo, config) {
|
|
|
164
174
|
const sortByQuery = sortBy.map((order) => `&sortBy=${order.join(':')}`).join('');
|
|
165
175
|
const searchQuery = query.search ? `&search=${query.search}` : '';
|
|
166
176
|
const searchByQuery = query.searchBy && searchBy.length ? searchBy.map((column) => `&searchBy=${column}`).join('') : '';
|
|
177
|
+
// Only expose select in meta data if query select differs from config select
|
|
178
|
+
const isQuerySelected = (selectParams === null || selectParams === void 0 ? void 0 : selectParams.length) !== ((_a = config.select) === null || _a === void 0 ? void 0 : _a.length);
|
|
179
|
+
const selectQuery = isQuerySelected ? `&select=${selectParams.join(',')}` : '';
|
|
167
180
|
const filterQuery = query.filter
|
|
168
181
|
? '&' +
|
|
169
182
|
(0, querystring_1.stringify)((0, lodash_1.mapKeys)(query.filter, (_param, name) => 'filter.' + name), '&', '=', { encodeURIComponent: (str) => str })
|
|
170
183
|
: '';
|
|
171
|
-
const options = `&limit=${limit}${sortByQuery}${searchQuery}${searchByQuery}${filterQuery}`;
|
|
184
|
+
const options = `&limit=${limit}${sortByQuery}${searchQuery}${searchByQuery}${selectQuery}${filterQuery}`;
|
|
172
185
|
const buildLink = (p) => path + '?page=' + p + options;
|
|
173
186
|
const totalPages = isPaginated ? Math.ceil(totalItems / limit) : 1;
|
|
174
187
|
const results = {
|
|
@@ -181,6 +194,7 @@ async function paginate(query, repo, config) {
|
|
|
181
194
|
sortBy,
|
|
182
195
|
search: query.search,
|
|
183
196
|
searchBy: query.search ? searchBy : undefined,
|
|
197
|
+
select: isQuerySelected ? selectParams : undefined,
|
|
184
198
|
filter: query.filter,
|
|
185
199
|
},
|
|
186
200
|
links: {
|
package/lib/paginate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paginate.js","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":";;;AAAA,qCAQgB;AAEhB,2CAAoE;AACpE,mCAAgC;AAChC,6CAAuC;AAEvC,qCAciB;AACjB,qCAAkE;AAIzD,+FAJW,uBAAc,OAIX;AAAE,6FAJW,qBAAY,OAIX;AAFrC,MAAM,MAAM,GAAW,IAAI,eAAM,CAAC,iBAAiB,CAAC,CAAA;AAIpD,MAAa,SAAS;
|
|
1
|
+
{"version":3,"file":"paginate.js","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":";;;AAAA,qCAQgB;AAEhB,2CAAoE;AACpE,mCAAgC;AAChC,6CAAuC;AAEvC,qCAciB;AACjB,qCAAkE;AAIzD,+FAJW,uBAAc,OAIX;AAAE,6FAJW,qBAAY,OAIX;AAFrC,MAAM,MAAM,GAAW,IAAI,eAAM,CAAC,iBAAiB,CAAC,CAAA;AAIpD,MAAa,SAAS;CAoBrB;AApBD,8BAoBC;AAED,IAAY,cAGX;AAHD,WAAY,cAAc;IACtB,4CAA0B,CAAA;IAC1B,wCAAsB,CAAA;AAC1B,CAAC,EAHW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAGzB;AAsBY,QAAA,iBAAiB,GAAG,GAAG,CAAA;AACvB,QAAA,aAAa,GAAG,EAAE,CAAA;AAClB,QAAA,aAAa,GAAG,CAAC,CAAA;AAEvB,KAAK,UAAU,QAAQ,CAC1B,KAAoB,EACpB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E,IAAI,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,GAAG,CAAC,CAAC;YAC7D,IAAI,EAAE,IAAI,IAAI,UAAU,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC;SAC9E;KACJ,CAAA;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,SAAS,EAAK,EAAE,OAAO,CAAC,CAAA;AACrD,CAAC;AAhPD,4BAgPC"}
|
package/lib/paginate.spec.js
CHANGED
|
@@ -230,6 +230,19 @@ describe('paginate', () => {
|
|
|
230
230
|
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
231
231
|
expect(result.data).toStrictEqual(cats.slice(0, 2));
|
|
232
232
|
});
|
|
233
|
+
it('should limit cats by query', async () => {
|
|
234
|
+
const config = {
|
|
235
|
+
sortableColumns: ['id'],
|
|
236
|
+
maxLimit: Number.MAX_SAFE_INTEGER,
|
|
237
|
+
defaultLimit: Number.MAX_SAFE_INTEGER,
|
|
238
|
+
};
|
|
239
|
+
const query = {
|
|
240
|
+
path: '',
|
|
241
|
+
limit: 2,
|
|
242
|
+
};
|
|
243
|
+
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
244
|
+
expect(result.data).toStrictEqual(cats.slice(0, 2));
|
|
245
|
+
});
|
|
233
246
|
it('should return correct links for some results', async () => {
|
|
234
247
|
const config = {
|
|
235
248
|
sortableColumns: ['id'],
|
|
@@ -624,6 +637,26 @@ describe('paginate', () => {
|
|
|
624
637
|
expect(result.data).toStrictEqual([cats[3]]);
|
|
625
638
|
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.name=$not:Leche');
|
|
626
639
|
});
|
|
640
|
+
it('should return based on a nested many-to-one where condition', async () => {
|
|
641
|
+
const config = {
|
|
642
|
+
sortableColumns: ['id'],
|
|
643
|
+
relations: ['cat'],
|
|
644
|
+
where: {
|
|
645
|
+
cat: {
|
|
646
|
+
id: cats[0].id,
|
|
647
|
+
},
|
|
648
|
+
},
|
|
649
|
+
};
|
|
650
|
+
const query = {
|
|
651
|
+
path: '',
|
|
652
|
+
};
|
|
653
|
+
const result = await (0, paginate_1.paginate)(query, catToyRepo, config);
|
|
654
|
+
expect(result.meta.totalItems).toBe(3);
|
|
655
|
+
result.data.forEach((toy) => {
|
|
656
|
+
expect(toy.cat.id).toBe(cats[0].id);
|
|
657
|
+
});
|
|
658
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC');
|
|
659
|
+
});
|
|
627
660
|
it('should return result based on filter on many-to-one relation', async () => {
|
|
628
661
|
const config = {
|
|
629
662
|
relations: ['cat'],
|
|
@@ -1665,6 +1698,8 @@ describe('paginate', () => {
|
|
|
1665
1698
|
};
|
|
1666
1699
|
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
1667
1700
|
expect(result.data).toStrictEqual(cats);
|
|
1701
|
+
expect(result.meta.select).toStrictEqual(undefined);
|
|
1702
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC');
|
|
1668
1703
|
});
|
|
1669
1704
|
it('should return all items even if deleted', async () => {
|
|
1670
1705
|
const config = {
|
|
@@ -1702,8 +1737,48 @@ describe('paginate', () => {
|
|
|
1702
1737
|
};
|
|
1703
1738
|
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
1704
1739
|
result.data.forEach((cat) => {
|
|
1740
|
+
expect(cat.id).toBeDefined();
|
|
1741
|
+
expect(cat.name).toBeDefined();
|
|
1705
1742
|
expect(cat.color).not.toBeDefined();
|
|
1706
1743
|
});
|
|
1744
|
+
expect(result.meta.select).toBe(undefined);
|
|
1745
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC');
|
|
1746
|
+
});
|
|
1747
|
+
it('should ignore query select', async () => {
|
|
1748
|
+
const config = {
|
|
1749
|
+
sortableColumns: ['id'],
|
|
1750
|
+
};
|
|
1751
|
+
const query = {
|
|
1752
|
+
path: '',
|
|
1753
|
+
select: ['id', 'name'],
|
|
1754
|
+
};
|
|
1755
|
+
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
1756
|
+
result.data.forEach((cat) => {
|
|
1757
|
+
expect(cat.id).toBeDefined();
|
|
1758
|
+
expect(cat.name).toBeDefined();
|
|
1759
|
+
expect(cat.color).toBeDefined();
|
|
1760
|
+
});
|
|
1761
|
+
expect(result.meta.select).toEqual(undefined);
|
|
1762
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC');
|
|
1763
|
+
});
|
|
1764
|
+
it('should only query select columns which have been config selected', async () => {
|
|
1765
|
+
const config = {
|
|
1766
|
+
sortableColumns: ['id'],
|
|
1767
|
+
select: ['id', 'name', 'color'],
|
|
1768
|
+
};
|
|
1769
|
+
const query = {
|
|
1770
|
+
path: '',
|
|
1771
|
+
select: ['id', 'color', 'age'],
|
|
1772
|
+
};
|
|
1773
|
+
const result = await (0, paginate_1.paginate)(query, catRepo, config);
|
|
1774
|
+
result.data.forEach((cat) => {
|
|
1775
|
+
expect(cat.id).toBeDefined();
|
|
1776
|
+
expect(cat.name).not.toBeDefined();
|
|
1777
|
+
expect(cat.color).toBeDefined();
|
|
1778
|
+
expect(cat.age).not.toBeDefined();
|
|
1779
|
+
});
|
|
1780
|
+
expect(result.meta.select).toEqual(['id', 'color']);
|
|
1781
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&select=id,color');
|
|
1707
1782
|
});
|
|
1708
1783
|
it('should return the specified relationship columns only', async () => {
|
|
1709
1784
|
const config = {
|
|
@@ -1724,6 +1799,8 @@ describe('paginate', () => {
|
|
|
1724
1799
|
expect(toy.id).not.toBeDefined();
|
|
1725
1800
|
});
|
|
1726
1801
|
});
|
|
1802
|
+
expect(result.meta.select).toBe(undefined);
|
|
1803
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=name:ASC');
|
|
1727
1804
|
});
|
|
1728
1805
|
it('should return selected columns', async () => {
|
|
1729
1806
|
const config = {
|
|
@@ -1751,6 +1828,8 @@ describe('paginate', () => {
|
|
|
1751
1828
|
expect(cat.toys).toHaveLength(0);
|
|
1752
1829
|
}
|
|
1753
1830
|
});
|
|
1831
|
+
expect(result.meta.select).toStrictEqual(['id', 'toys.(size.height)']);
|
|
1832
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&select=id,toys.(size.height)');
|
|
1754
1833
|
});
|
|
1755
1834
|
it('should only select columns via query which are selected in config', async () => {
|
|
1756
1835
|
const config = {
|
|
@@ -1773,6 +1852,8 @@ describe('paginate', () => {
|
|
|
1773
1852
|
expect(cat.home).toBeNull();
|
|
1774
1853
|
}
|
|
1775
1854
|
});
|
|
1855
|
+
expect(result.meta.select).toStrictEqual(['id', 'home.id']);
|
|
1856
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&select=id,home.id');
|
|
1776
1857
|
});
|
|
1777
1858
|
it('should return the specified nested relationship columns only', async () => {
|
|
1778
1859
|
const config = {
|
|
@@ -1800,6 +1881,8 @@ describe('paginate', () => {
|
|
|
1800
1881
|
expect(cat.home).toBeNull();
|
|
1801
1882
|
}
|
|
1802
1883
|
});
|
|
1884
|
+
expect(result.meta.select).toBe(undefined);
|
|
1885
|
+
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC');
|
|
1803
1886
|
});
|
|
1804
1887
|
it('should return the right amount of results if a many to many relation is involved', async () => {
|
|
1805
1888
|
const config = {
|