-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathquery.js
More file actions
206 lines (188 loc) · 5.89 KB
/
query.js
File metadata and controls
206 lines (188 loc) · 5.89 KB
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
const objectPath = require('object-path');
const bsonUrlEncoding = require('./bsonUrlEncoding');
/**
* Helper function to encode pagination tokens.
*
* NOTE: this function modifies the passed-in `response` argument directly.
*
* @param {Object} params
* @param {String} paginatedField
* @param {boolean} sortCaseInsensitive
*
* @param {Object} response The response
* @param {String?} previous
* @param {String?} next
*
* @returns void
*/
function encodePaginationTokens(params, response) {
const shouldSecondarySortOnId = params.paginatedField !== '_id';
if (response.previous) {
let previousPaginatedField = objectPath.get(response.previous, params.paginatedField);
if (params.sortCaseInsensitive) {
previousPaginatedField = previousPaginatedField?.toLowerCase?.() ?? '';
}
if (shouldSecondarySortOnId) {
response.previous = bsonUrlEncoding.encode([previousPaginatedField, response.previous._id]);
} else {
response.previous = bsonUrlEncoding.encode(previousPaginatedField);
}
}
if (response.next) {
let nextPaginatedField = objectPath.get(response.next, params.paginatedField);
if (params.sortCaseInsensitive) {
nextPaginatedField = nextPaginatedField?.toLowerCase?.() ?? '';
}
if (shouldSecondarySortOnId) {
response.next = bsonUrlEncoding.encode([nextPaginatedField, response.next._id]);
} else {
response.next = bsonUrlEncoding.encode(nextPaginatedField);
}
}
}
module.exports = {
/**
* Parses the raw results from a find or aggregate query and generates a response object that
* contain the various pagination properties
*
* @param {Object[]} results the results from a query
* @param {Object} params The params originally passed to `find` or `aggregate`
*
* @return {Object} The object containing pagination properties
*/
prepareResponse(results, params) {
const hasMore = results.length > params.limit;
// Remove the extra element that we added to 'peek' to see if there were more entries.
if (hasMore) results.pop();
const hasPrevious = !!params.next || !!(params.previous && hasMore);
const hasNext = !!params.previous || hasMore;
// If we sorted reverse to get the previous page, correct the sort order.
if (params.previous) results = results.reverse();
const response = {
results,
previous: results[0],
hasPrevious,
next: results[results.length - 1],
hasNext,
};
encodePaginationTokens(params, response);
return response;
},
encodePaginationTokens,
/**
* Generates a `$sort` object given the parameters
*
* @param {Object} params The params originally passed to `find` or `aggregate`
*
* @return {Object} a sort object
*/
generateSort(params) {
const sortAsc =
(!params.sortAscending && params.previous) || (params.sortAscending && !params.previous);
const sortDir = sortAsc ? 1 : -1;
if (params.paginatedField == '_id') {
return {
_id: sortDir,
};
} else {
const field = params.sortCaseInsensitive ? '__lc' : params.paginatedField;
return {
[field]: sortDir,
_id: sortDir,
};
}
},
/**
* Generates a cursor query that provides the offset capabilities
*
* @param {Object} params The params originally passed to `find` or `aggregate`
*
* @return {Object} a cursor offset query
*/
generateCursorQuery(params) {
if (!params.next && !params.previous) return {};
const sortAsc =
(!params.sortAscending && params.previous) || (params.sortAscending && !params.previous);
// a `next` cursor will have precedence over a `previous` cursor.
const op = params.next || params.previous;
if (params.paginatedField == '_id') {
if (sortAsc) {
return { _id: { $gt: op } };
} else {
return { _id: { $lt: op } };
}
} else {
const field = params.sortCaseInsensitive ? '__lc' : params.paginatedField;
const notUndefined = { [field]: { $exists: true } };
const onlyUndefs = { [field]: { $exists: false } };
const notNullNorUndefined = { [field]: { $ne: null } };
const nullOrUndefined = { [field]: null };
const onlyNulls = { $and: [{ [field]: { $exists: true } }, { [field]: null }] };
const [paginatedFieldValue, idValue] = op;
switch (paginatedFieldValue) {
case null:
if (sortAsc) {
return {
$or: [
notNullNorUndefined,
{
...onlyNulls,
_id: { $gt: idValue },
},
],
};
} else {
return {
$or: [
onlyUndefs,
{
...onlyNulls,
_id: { $lt: idValue },
},
],
};
}
case undefined:
if (sortAsc) {
return {
$or: [
notUndefined,
{
...onlyUndefs,
_id: { $gt: idValue },
},
],
};
} else {
return {
...onlyUndefs,
_id: { $lt: idValue },
};
}
default:
if (sortAsc) {
return {
$or: [
{ [field]: { $gt: paginatedFieldValue } },
{
[field]: { $eq: paginatedFieldValue },
_id: { $gt: idValue },
},
],
};
} else {
return {
$or: [
{ [field]: { $lt: paginatedFieldValue } },
nullOrUndefined,
{
[field]: { $eq: paginatedFieldValue },
_id: { $lt: idValue },
},
],
};
}
}
}
},
};