-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
251 lines (216 loc) · 8.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'use strict';
let { Op } = require('sequelize');
if (!Op) {
// Support older versions of sequelize
Op = {
and: '$and',
or: '$or',
lt: '$lt',
gt: '$gt'
};
}
function encodeCursor(obj, order, direction) {
if (!obj) return null;
// Get fields that uniquely identify the object in the order
const values = [];
for (const [field] of order) values.push(obj[field]);
// Put cursor into relevant format
const cursor = direction ? { values, direction } : values;
// Encode the cursor
return Buffer.from(JSON.stringify(cursor)).toString('base64');
}
function decodeCursor(cursor) {
return cursor ? JSON.parse(Buffer.from(cursor, 'base64').toString('utf8')) : null;
}
function getAdditionalWhereClause(order, cursor) {
const currentOp = order[0][1] === 'ASC' ? Op.gt : Op.lt;
if (order.length === 1) {
return {
[order[0][0]]: {
[currentOp]: cursor[0]
}
};
} else {
return {
[Op.or]: [
{
[order[0][0]]: {
[currentOp]: cursor[0]
}
},
{
[order[0][0]]: cursor[0],
...getAdditionalWhereClause(order.slice(1), cursor.slice(1))
}
]
};
}
}
module.exports.withSimplePagination = function ({
methodName = 'paginate',
primaryKeyField = 'id'
} = {}) {
return (model) => {
model[methodName] = ({
where = {},
order = [],
limit,
paginationField = primaryKeyField,
cursor,
...queryArgs
} = {}) => {
// Decode the cursor
cursor = decodeCursor(cursor);
const { values, direction } = cursor || { values: null, direction: null };
// Push primary key to sort to ensure that there is always a unique sort
order = [...order];
if (order.filter((field) => field[0] === paginationField).length === 0)
order.push([paginationField, 'ASC']);
if (values) {
// Check that the cursor is compatiable with this query.
if (values.length !== order.length) throw new Error('Cursor does not match query.');
// If the direction is prev then we need to flip the sorting to get the fields closest the cursor first as
// opposed to the very first values
if (direction === 'prev')
order = order.map(([field, value]) => [field, value === 'ASC' ? 'DESC' : 'ASC']);
// We now get the sequelize equivalent of (field1, field2) > (value1, value2) for a infinite number of fields
// AND do so in a way that allows for the fields to be sorted in different directions.
const paginationWhere = getAdditionalWhereClause(order, values);
// Add the generated where clause to the inputted where clause with the AND operator
where = { [Op.and]: [where, paginationWhere] };
}
return model
.findAll({
where,
order,
...(limit && { limit: limit + 1 }),
...queryArgs
})
.then((results) => {
// - If no cursor is provided then values are fetched from the start of the order and therefore there is no
// previous page. If there is more results than on the page then there is a next page.
// - If there is a cursor and the direction is next then there is a value before the page, i.e. the cursor's
// value, and therefore there is a previous page. If there is more results than on the page then there is a
// next page.
// - If there is a cursor and the direction is previous then there is a value after the page, i.e. the
// cursor's value, and therefore there is a next page. If there is more results than on the page then there
// is a previous page.
const [hasPrev, hasNext] = cursor
? direction === 'next'
? [true, results.length > limit]
: [results.length > limit, true]
: [false, results.length > limit];
// Remove the extra value used to check for additional values.
if (results.length > limit) results.pop();
// Reorder the results if we changed the order in the query.
if (direction === 'prev') results.reverse();
// Return page and cursor info.
return {
results,
cursors: {
hasPrev,
hasNext,
// Create cursors from the first and last value to use as cursors for the previous and next page.
prevCursor: results.length !== 0 ? encodeCursor(results[0], order, 'prev') : null,
nextCursor:
results.length !== 0
? encodeCursor(results[results.length - 1], order, 'next')
: null
}
};
});
};
return model;
};
};
module.exports.withRelayPagination = function ({
methodName = 'paginate',
primaryKeyField = 'id'
} = {}) {
return (model) => {
model[methodName] = ({
where = {},
order = [],
first,
after,
last,
before,
paginationField = primaryKeyField,
cursor,
...queryArgs
} = {}) => {
if ((first || after) && (last || before))
throw new Error(
'This package does not support both first/after and before/last simultaneously, as advised by spec.'
);
if (last && !before)
throw new Error('This package does not support the use of last without a before cursor.');
if ((first && first < 0) || (last && last < 0))
throw new Error('first/last must be a non-negative integer values.');
// Decode the cursor
const values = decodeCursor(after) || decodeCursor(before);
const direction = (!after && !before) || after ? 'next' : 'prev';
const limit = first || last;
// Push primary key to sort to ensure that there is always a unique sort
order = [...order];
if (order.filter((field) => field[0] === paginationField).length === 0)
order.push([paginationField, 'ASC']);
if (values) {
// Check that the cursor is compatiable with this query.
if (values.length !== order.length) throw new Error('Cursor does not match query.');
// If the direction is prev then we need to flip the sorting to get the fields closest the cursor first as
// opposed to the very first values
if (direction === 'prev')
order = order.map(([field, value]) => [field, value === 'ASC' ? 'DESC' : 'ASC']);
// We now get the sequelize equivalent of (field1, field2) > (value1, value2) for a infinite number of fields
// AND do so in a way that allows for the fields to be sorted in different directions.
const paginationWhere = getAdditionalWhereClause(order, values);
// Add the generated where clause to the inputted where clause with the AND operator
where = { [Op.and]: [where, paginationWhere] };
}
return model
.findAll({
where,
order,
...(limit && { limit: limit + 1 }),
...queryArgs
})
.then((results) => {
// - If no cursor is provided then values are fetched from the start of the order and therefore there is no
// previous page. If there is more results than on the page then there is a next page.
// - If there is a cursor and the direction is next then there is a value before the page, i.e. the cursor's
// value, and therefore there is a previous page. If there is more results than on the page then there is a
// next page.
// - If there is a cursor and the direction is previous then there is a value after the page, i.e. the
// cursor's value, and therefore there is a next page. If there is more results than on the page then there
// is a previous page.
const [hasPreviousPage, hasNextPage] =
after || before
? direction === 'next'
? [true, results.length > limit]
: [results.length > limit, true]
: [false, results.length > limit];
// Remove the extra value used to check for additional values.
if (results.length > limit) results.pop();
// Reorder the results if we changed the order in the query.
if (direction === 'prev') results.reverse();
// Map results into Relay spec format
const edges = results.map((result) => ({
cursor: encodeCursor(result, order),
node: result
}));
// Return page and cursor info.
return {
edges,
pageInfo: {
hasPreviousPage,
hasNextPage,
startCursor: edges.length !== 0 ? edges[0].cursor : null,
endCursor: edges.length !== 0 ? edges[edges.length - 1].cursor : null
}
};
});
};
return model;
};
};