@@ -14,7 +14,10 @@ import {
14
14
pascalToUnderscore ,
15
15
} from './utils' ;
16
16
17
- export type Order = 'ASC' | 'DESC' ;
17
+ export enum Order {
18
+ ASC = 'ASC' ,
19
+ DESC = 'DESC' ,
20
+ }
18
21
19
22
export type EscapeFn = ( name : string ) => string ;
20
23
@@ -45,13 +48,13 @@ export default class Paginator<Entity> {
45
48
46
49
private limit = 100 ;
47
50
48
- private order : Order = ' DESC' ;
51
+ private order : Order = Order . DESC ;
49
52
50
53
public constructor (
51
54
private entity : ObjectType < Entity > ,
52
55
private paginationKeys : Extract < keyof Entity , string > [ ] ,
53
56
private paginationUniqueKey : Extract < keyof Entity , string > ,
54
- ) { }
57
+ ) { }
55
58
56
59
public setAlias ( alias : string ) : void {
57
60
this . alias = alias ;
@@ -73,7 +76,9 @@ export default class Paginator<Entity> {
73
76
this . order = order ;
74
77
}
75
78
76
- public async paginate ( builder : SelectQueryBuilder < Entity > ) : Promise < PagingResult < Entity > > {
79
+ public async paginate (
80
+ builder : SelectQueryBuilder < Entity > ,
81
+ ) : Promise < PagingResult < Entity > > {
77
82
const entities = await this . appendPagingQuery ( builder ) . getMany ( ) ;
78
83
const hasMore = entities . length > this . limit ;
79
84
@@ -107,7 +112,9 @@ export default class Paginator<Entity> {
107
112
} ;
108
113
}
109
114
110
- private appendPagingQuery ( builder : SelectQueryBuilder < Entity > ) : SelectQueryBuilder < Entity > {
115
+ private appendPagingQuery (
116
+ builder : SelectQueryBuilder < Entity > ,
117
+ ) : SelectQueryBuilder < Entity > {
111
118
const cursors : CursorParam = { } ;
112
119
113
120
if ( this . hasAfterCursor ( ) ) {
@@ -117,7 +124,9 @@ export default class Paginator<Entity> {
117
124
}
118
125
119
126
if ( Object . keys ( cursors ) . length > 0 ) {
120
- builder . andWhere ( new Brackets ( ( where ) => this . buildCursorQuery ( where , cursors ) ) ) ;
127
+ builder . andWhere (
128
+ new Brackets ( ( where ) => this . buildCursorQuery ( where , cursors ) ) ,
129
+ ) ;
121
130
}
122
131
123
132
builder . take ( this . limit + 1 ) ;
@@ -126,31 +135,36 @@ export default class Paginator<Entity> {
126
135
return builder ;
127
136
}
128
137
129
- private buildCursorQuery ( where : WhereExpressionBuilder , cursors : CursorParam ) : void {
138
+ private buildCursorQuery (
139
+ where : WhereExpressionBuilder ,
140
+ cursors : CursorParam ,
141
+ ) : void {
130
142
const operator = this . getOperator ( ) ;
131
143
const params : CursorParam = { } ;
132
144
this . paginationKeys . forEach ( ( key ) => {
133
145
params [ key ] = cursors [ key ] ;
134
- where . andWhere ( new Brackets ( ( qb ) => {
135
- const paramsHolder = {
136
- [ `${ key } _1` ] : params [ key ] ,
137
- [ `${ key } _2` ] : params [ key ] ,
138
- } ;
139
- qb . where ( `${ this . alias } .${ key } ${ operator } :${ key } _1` , paramsHolder ) ;
140
- if ( this . paginationUniqueKey !== key ) {
141
- qb . orWhere ( `${ this . alias } .${ key } = :${ key } _2` , paramsHolder ) ;
142
- }
143
- } ) ) ;
146
+ where . andWhere (
147
+ new Brackets ( ( qb ) => {
148
+ const paramsHolder = {
149
+ [ `${ key } _1` ] : params [ key ] ,
150
+ [ `${ key } _2` ] : params [ key ] ,
151
+ } ;
152
+ qb . where ( `${ this . alias } .${ key } ${ operator } :${ key } _1` , paramsHolder ) ;
153
+ if ( this . paginationUniqueKey !== key ) {
154
+ qb . orWhere ( `${ this . alias } .${ key } = :${ key } _2` , paramsHolder ) ;
155
+ }
156
+ } ) ,
157
+ ) ;
144
158
} ) ;
145
159
}
146
160
147
161
private getOperator ( ) : string {
148
162
if ( this . hasAfterCursor ( ) ) {
149
- return this . order === ' ASC' ? '>' : '<' ;
163
+ return this . order === Order . ASC ? '>' : '<' ;
150
164
}
151
165
152
166
if ( this . hasBeforeCursor ( ) ) {
153
- return this . order === ' ASC' ? '<' : '>' ;
167
+ return this . order === Order . ASC ? '<' : '>' ;
154
168
}
155
169
156
170
return '=' ;
@@ -180,11 +194,13 @@ export default class Paginator<Entity> {
180
194
}
181
195
182
196
private encode ( entity : Entity ) : string {
183
- const payload = this . paginationKeys . map ( ( key ) => {
184
- const type = this . getEntityPropertyType ( key ) ;
185
- const value = encodeByType ( type , entity [ key ] ) ;
186
- return `${ key } :${ value } ` ;
187
- } ) . join ( ',' ) ;
197
+ const payload = this . paginationKeys
198
+ . map ( ( key ) => {
199
+ const type = this . getEntityPropertyType ( key ) ;
200
+ const value = encodeByType ( type , entity [ key ] ) ;
201
+ return `${ key } :${ value } ` ;
202
+ } )
203
+ . join ( ',' ) ;
188
204
189
205
return btoa ( payload ) ;
190
206
}
@@ -203,13 +219,15 @@ export default class Paginator<Entity> {
203
219
}
204
220
205
221
private getEntityPropertyType ( key : string ) : string {
206
- return Reflect . getMetadata ( 'design:type' , this . entity . prototype , key ) . name . toLowerCase ( ) ;
222
+ return Reflect . getMetadata (
223
+ 'design:type' ,
224
+ this . entity . prototype ,
225
+ key ,
226
+ ) . name . toLowerCase ( ) ;
207
227
}
208
228
209
229
private flipOrder ( order : Order ) : Order {
210
- return order === 'ASC'
211
- ? 'DESC'
212
- : 'ASC' ;
230
+ return order === Order . ASC ? Order . DESC : Order . ASC ;
213
231
}
214
232
215
233
private toPagingResult < Entity > ( entities : Entity [ ] ) : PagingResult < Entity > {
0 commit comments