@tiledesk/tiledesk-server 3.1.0 → 3.2.2
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/CHANGELOG.md +13 -0
- package/models/dataTable.js +16 -0
- package/models/location.js +6 -12
- package/models/request.js +65 -61
- package/package.json +3 -3
- package/routes/dataTable.js +23 -0
- package/routes/project.js +1 -1
- package/services/dataTableService.js +436 -42
- package/test/dataTablesRoute.test.js +639 -0
- package/utils/dataTableUtils.js +99 -9
package/utils/dataTableUtils.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
|
|
3
|
-
const COLUMN_TYPES =
|
|
3
|
+
const COLUMN_TYPES = Object.freeze({
|
|
4
|
+
STRING: 'string',
|
|
5
|
+
NUMBER: 'number',
|
|
6
|
+
BOOLEAN: 'boolean',
|
|
7
|
+
DATETIME: 'datetime',
|
|
8
|
+
TTL: 'ttl',
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const COLUMN_TYPE_VALUES = Object.freeze(Object.values(COLUMN_TYPES));
|
|
12
|
+
|
|
13
|
+
// One year: well beyond any sensible retention, and far from the point where
|
|
14
|
+
// createdAt plus the ttl stops being a representable date.
|
|
15
|
+
const MAX_TTL_SECONDS = 365 * 24 * 60 * 60;
|
|
16
|
+
|
|
17
|
+
function isValidColumnType(type) {
|
|
18
|
+
return COLUMN_TYPE_VALUES.indexOf(type) > -1;
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
const MATCH_MODES = ['all', 'any'];
|
|
5
22
|
const FORBIDDEN_NAMES = ['__proto__', 'constructor', 'prototype', '$where'];
|
|
6
23
|
|
|
@@ -53,7 +70,7 @@ function normalizeSchemaForCreate(input) {
|
|
|
53
70
|
|
|
54
71
|
const name = typeof col.name === 'string' ? col.name.trim() : '';
|
|
55
72
|
assertValidColumnName(name, 'Invalid column at index ' + i);
|
|
56
|
-
if (!
|
|
73
|
+
if (!isValidColumnType(col.type)) {
|
|
57
74
|
throw new Error('Column type is required at index ' + i);
|
|
58
75
|
}
|
|
59
76
|
if (names[name]) throw new Error('Column name already exists: ' + name);
|
|
@@ -66,6 +83,7 @@ function normalizeSchemaForCreate(input) {
|
|
|
66
83
|
index: typeof col.index === 'number' ? col.index : i,
|
|
67
84
|
});
|
|
68
85
|
}
|
|
86
|
+
assertSingleTtlColumn(schema);
|
|
69
87
|
return schema;
|
|
70
88
|
}
|
|
71
89
|
|
|
@@ -100,7 +118,7 @@ function resolveSchemaUpdate(incoming, currentSchema) {
|
|
|
100
118
|
assertValidColumnName(name, 'Invalid column at index ' + i);
|
|
101
119
|
|
|
102
120
|
const type = col.type || prev.type;
|
|
103
|
-
if (!
|
|
121
|
+
if (!isValidColumnType(type)) {
|
|
104
122
|
throw new Error('Column type is required at index ' + i);
|
|
105
123
|
}
|
|
106
124
|
if (names[name]) throw new Error('Column name already exists: ' + name);
|
|
@@ -131,7 +149,15 @@ function resolveSchemaUpdate(incoming, currentSchema) {
|
|
|
131
149
|
deletes.push(remainingIds[id].name);
|
|
132
150
|
}
|
|
133
151
|
|
|
134
|
-
|
|
152
|
+
assertSingleTtlColumn(newSchema);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
schema: newSchema,
|
|
156
|
+
renames: renames,
|
|
157
|
+
deletes: deletes,
|
|
158
|
+
ttlBefore: findTtlColumn(currentSchema),
|
|
159
|
+
ttlAfter: findTtlColumn(newSchema),
|
|
160
|
+
};
|
|
135
161
|
}
|
|
136
162
|
|
|
137
163
|
function normalizeColumnToAdd(col, nextIndex, existingSchema) {
|
|
@@ -140,11 +166,14 @@ function normalizeColumnToAdd(col, nextIndex, existingSchema) {
|
|
|
140
166
|
|
|
141
167
|
const name = typeof col.name === 'string' ? col.name.trim() : '';
|
|
142
168
|
assertValidColumnName(name);
|
|
143
|
-
if (!
|
|
169
|
+
if (!isValidColumnType(col.type)) throw new Error('Column type is required');
|
|
144
170
|
|
|
145
171
|
for (let i = 0; i < existingSchema.length; i++) {
|
|
146
172
|
if (existingSchema[i].name === name) throw new Error('Column name already exists: ' + name);
|
|
147
173
|
}
|
|
174
|
+
if (col.type === COLUMN_TYPES.TTL && findTtlColumn(existingSchema)) {
|
|
175
|
+
throw new Error('Invalid column: only one ttl column is allowed per table');
|
|
176
|
+
}
|
|
148
177
|
|
|
149
178
|
return {
|
|
150
179
|
id: generateColumnId(),
|
|
@@ -168,15 +197,32 @@ function findColumnByName(schema, name) {
|
|
|
168
197
|
return null;
|
|
169
198
|
}
|
|
170
199
|
|
|
200
|
+
function findTtlColumn(schema) {
|
|
201
|
+
if (!schema) return null;
|
|
202
|
+
for (let i = 0; i < schema.length; i++) {
|
|
203
|
+
if (schema[i].type === COLUMN_TYPES.TTL) return schema[i];
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function assertSingleTtlColumn(schema) {
|
|
209
|
+
let found = 0;
|
|
210
|
+
for (let i = 0; i < schema.length; i++) {
|
|
211
|
+
if (schema[i].type === COLUMN_TYPES.TTL && ++found > 1) {
|
|
212
|
+
throw new Error('Invalid schema: only one ttl column is allowed per table');
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
171
217
|
function coerceValue(value, column) {
|
|
172
218
|
if (value === null || value === undefined) return value;
|
|
173
219
|
|
|
174
220
|
switch (column.type) {
|
|
175
|
-
case
|
|
221
|
+
case COLUMN_TYPES.STRING:
|
|
176
222
|
if (typeof value !== 'string') throw new Error('Invalid value for column ' + column.name + ': expected string');
|
|
177
223
|
return value;
|
|
178
224
|
|
|
179
|
-
case
|
|
225
|
+
case COLUMN_TYPES.NUMBER:
|
|
180
226
|
if (typeof value === 'number' && !isNaN(value)) return value;
|
|
181
227
|
if (typeof value === 'string' && value.trim() !== '') {
|
|
182
228
|
const num = Number(value);
|
|
@@ -184,7 +230,7 @@ function coerceValue(value, column) {
|
|
|
184
230
|
}
|
|
185
231
|
throw new Error('Invalid value for column ' + column.name + ': expected number');
|
|
186
232
|
|
|
187
|
-
case
|
|
233
|
+
case COLUMN_TYPES.BOOLEAN:
|
|
188
234
|
if (typeof value === 'boolean') return value;
|
|
189
235
|
if (typeof value === 'string') {
|
|
190
236
|
const lower = value.trim().toLowerCase();
|
|
@@ -193,7 +239,7 @@ function coerceValue(value, column) {
|
|
|
193
239
|
}
|
|
194
240
|
throw new Error('Invalid value for column ' + column.name + ': expected boolean');
|
|
195
241
|
|
|
196
|
-
case
|
|
242
|
+
case COLUMN_TYPES.DATETIME:
|
|
197
243
|
if (value instanceof Date && !isNaN(value.getTime())) return value;
|
|
198
244
|
if (typeof value === 'string' && value.length > 0) {
|
|
199
245
|
const date = new Date(value);
|
|
@@ -201,6 +247,25 @@ function coerceValue(value, column) {
|
|
|
201
247
|
}
|
|
202
248
|
throw new Error('Invalid value for column ' + column.name + ': expected datetime');
|
|
203
249
|
|
|
250
|
+
case COLUMN_TYPES.TTL: {
|
|
251
|
+
let seconds;
|
|
252
|
+
if (typeof value === 'number') seconds = value;
|
|
253
|
+
else if (typeof value === 'string' && value.trim() !== '') seconds = Number(value);
|
|
254
|
+
if (seconds === undefined || isNaN(seconds)) {
|
|
255
|
+
throw new Error('Invalid value for column ' + column.name + ': expected a number of seconds');
|
|
256
|
+
}
|
|
257
|
+
// 0 and negatives would make MongoDB drop the row on the next TTL pass
|
|
258
|
+
if (!Number.isInteger(seconds) || seconds <= 0) {
|
|
259
|
+
throw new Error('Invalid value for column ' + column.name + ': ttl must be a positive integer number of seconds');
|
|
260
|
+
}
|
|
261
|
+
// Past this the resulting date overflows and the row is refused by the
|
|
262
|
+
// model with a cast error, which surfaces as a 500 instead of a 400
|
|
263
|
+
if (seconds > MAX_TTL_SECONDS) {
|
|
264
|
+
throw new Error('Invalid value for column ' + column.name + ': ttl must not exceed ' + MAX_TTL_SECONDS + ' seconds');
|
|
265
|
+
}
|
|
266
|
+
return seconds;
|
|
267
|
+
}
|
|
268
|
+
|
|
204
269
|
default:
|
|
205
270
|
throw new Error('Unsupported column type: ' + column.type);
|
|
206
271
|
}
|
|
@@ -309,6 +374,29 @@ function fillRowDataFromSchema(data, schema) {
|
|
|
309
374
|
return result;
|
|
310
375
|
}
|
|
311
376
|
|
|
377
|
+
/**
|
|
378
|
+
* Rebind conditions to a single row of a batch. In a bulk upsert the
|
|
379
|
+
* conditions name the columns that identify a row, and the value to match
|
|
380
|
+
* comes from the row itself: a fixed value would make every row of the batch
|
|
381
|
+
* compete for the same match.
|
|
382
|
+
*/
|
|
383
|
+
function bindConditionsToRow(conditions, rowData, schema) {
|
|
384
|
+
return conditions.map(function (item, i) {
|
|
385
|
+
if (!item || typeof item !== 'object') throw new Error('Invalid condition at index ' + i);
|
|
386
|
+
if (NO_VALUE_OPERATORS.indexOf(item.operator) !== -1) return item;
|
|
387
|
+
|
|
388
|
+
const columnName = typeof item.column === 'string' ? item.column.trim() : '';
|
|
389
|
+
const colMeta = findColumnByName(schema, columnName);
|
|
390
|
+
if (!colMeta) throw new Error('Column ' + item.column + ' does not exist');
|
|
391
|
+
|
|
392
|
+
const value = rowData ? rowData[colMeta.name] : undefined;
|
|
393
|
+
if (value === undefined || value === null) {
|
|
394
|
+
throw new Error('a value for column ' + colMeta.name + ' is required to match an existing row');
|
|
395
|
+
}
|
|
396
|
+
return Object.assign({}, item, { value: value });
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
312
400
|
function buildRowQueryFilter(body, schema) {
|
|
313
401
|
if (body.id_row) {
|
|
314
402
|
return { _id: body.id_row };
|
|
@@ -339,6 +427,7 @@ function buildUpdateSet(data, schema) {
|
|
|
339
427
|
|
|
340
428
|
module.exports = {
|
|
341
429
|
COLUMN_TYPES: COLUMN_TYPES,
|
|
430
|
+
findTtlColumn: findTtlColumn,
|
|
342
431
|
generateColumnId: generateColumnId,
|
|
343
432
|
normalizeSchemaForCreate: normalizeSchemaForCreate,
|
|
344
433
|
normalizeColumnToAdd: normalizeColumnToAdd,
|
|
@@ -347,6 +436,7 @@ module.exports = {
|
|
|
347
436
|
findColumnByName: findColumnByName,
|
|
348
437
|
validateRowData: validateRowData,
|
|
349
438
|
buildSearchFilter: buildSearchFilter,
|
|
439
|
+
bindConditionsToRow: bindConditionsToRow,
|
|
350
440
|
buildRowQueryFilter: buildRowQueryFilter,
|
|
351
441
|
buildUpdateSet: buildUpdateSet,
|
|
352
442
|
fillRowDataFromSchema: fillRowDataFromSchema,
|