@formio/appserver 2.0.0-rc.8 → 2.0.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/lib/actions/index.d.ts +3 -156
- package/lib/actions/login/LoginAction.d.ts +2 -109
- package/lib/actions/login/LoginAction.js +124 -213
- package/lib/actions/role/RoleAction.d.ts +2 -61
- package/lib/actions/role/RoleAction.js +13 -20
- package/lib/actions/save/SaveAction.d.ts +2 -28
- package/lib/actions/save/SaveAction.js +27 -45
- package/lib/modules/index.d.ts +1 -3
- package/lib/modules/index.js +2 -4
- package/lib/modules/mongodb/db.d.ts +31 -32
- package/lib/modules/mongodb/db.js +220 -94
- package/lib/server.d.ts +5 -171
- package/lib/server.js +59 -66
- package/package.json +18 -20
- package/lib/actions/login/LoginAction.spec.d.ts +0 -1
- package/lib/actions/login/LoginAction.spec.js +0 -15
- package/lib/actions/role/RoleAction.spec.d.ts +0 -1
- package/lib/actions/role/RoleAction.spec.js +0 -15
- package/lib/actions/save/SaveAction.spec.d.ts +0 -1
- package/lib/actions/save/SaveAction.spec.js +0 -15
- package/lib/modules/auth/auth.d.ts +0 -5
- package/lib/modules/auth/auth.js +0 -70
- package/lib/modules/auth/auth.spec.d.ts +0 -1
- package/lib/modules/auth/auth.spec.js +0 -93
- package/lib/modules/mongodb/db.spec.d.ts +0 -1
- package/lib/modules/mongodb/db.spec.js +0 -714
- package/server.js +0 -1
|
@@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
11
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
23
|
exports.Database = void 0;
|
|
13
24
|
const mongodb_1 = require("mongodb");
|
|
@@ -19,6 +30,7 @@ class Database {
|
|
|
19
30
|
constructor(config) {
|
|
20
31
|
this.config = config;
|
|
21
32
|
this.prefix = '';
|
|
33
|
+
this.authQuery = null;
|
|
22
34
|
this.db = null;
|
|
23
35
|
this.client = null; // Add this line
|
|
24
36
|
this.currentCollectionName = 'submissions';
|
|
@@ -41,7 +53,6 @@ class Database {
|
|
|
41
53
|
}
|
|
42
54
|
/**
|
|
43
55
|
* Connect to the database.
|
|
44
|
-
* @param {*} scope
|
|
45
56
|
* @returns
|
|
46
57
|
*/
|
|
47
58
|
connect() {
|
|
@@ -135,7 +146,7 @@ class Database {
|
|
|
135
146
|
}
|
|
136
147
|
// Generic load record method.
|
|
137
148
|
load(collectionName_1) {
|
|
138
|
-
return __awaiter(this, arguments, void 0, function* (collectionName,
|
|
149
|
+
return __awaiter(this, arguments, void 0, function* (collectionName, dbQuery = {}) {
|
|
139
150
|
if (!this.db) {
|
|
140
151
|
return null;
|
|
141
152
|
}
|
|
@@ -146,7 +157,7 @@ class Database {
|
|
|
146
157
|
}
|
|
147
158
|
let item;
|
|
148
159
|
try {
|
|
149
|
-
item = yield collection.findOne(
|
|
160
|
+
item = yield collection.findOne(dbQuery);
|
|
150
161
|
}
|
|
151
162
|
catch (err) {
|
|
152
163
|
error(err.message);
|
|
@@ -154,9 +165,118 @@ class Database {
|
|
|
154
165
|
return item;
|
|
155
166
|
});
|
|
156
167
|
}
|
|
168
|
+
// Provides a suplemental query for authentication purposes.
|
|
169
|
+
indexAuth(query) {
|
|
170
|
+
this.authQuery = query || null;
|
|
171
|
+
}
|
|
172
|
+
getQueryValue(name, value, selector) {
|
|
173
|
+
if (name === 'created' || name === 'modified') {
|
|
174
|
+
return new Date(value);
|
|
175
|
+
}
|
|
176
|
+
if ((typeof value === 'string') && (!selector || (selector === 'eq' || selector === 'ne'))) {
|
|
177
|
+
const lcValue = value.toLowerCase();
|
|
178
|
+
if (lcValue === 'null') {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (lcValue === '"null"') {
|
|
182
|
+
return 'null';
|
|
183
|
+
}
|
|
184
|
+
if (lcValue === 'true') {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
if (lcValue === '"true"') {
|
|
188
|
+
return 'true';
|
|
189
|
+
}
|
|
190
|
+
if (lcValue === 'false') {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
if (lcValue === '"false"') {
|
|
194
|
+
return 'false';
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Converts the url query to query parameters.
|
|
201
|
+
* @param query
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
dbQuery(query = {}) {
|
|
205
|
+
const dbQuery = {};
|
|
206
|
+
const { limit, skip, sort } = query, filters = __rest(query, ["limit", "skip", "sort"]);
|
|
207
|
+
if (limit !== undefined) {
|
|
208
|
+
dbQuery.limit = limit ? Number(limit) : 10;
|
|
209
|
+
}
|
|
210
|
+
if (skip !== undefined) {
|
|
211
|
+
dbQuery.skip = skip ? Number(skip) : 0;
|
|
212
|
+
}
|
|
213
|
+
if (sort) {
|
|
214
|
+
dbQuery.sort = {};
|
|
215
|
+
const sortParts = sort.split(',');
|
|
216
|
+
sortParts.forEach((part) => {
|
|
217
|
+
const desc = part.charAt(0) === '-';
|
|
218
|
+
const field = desc ? part.substring(1) : part;
|
|
219
|
+
dbQuery.sort[field] = desc ? -1 : 1;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
// Iterate through each filter.
|
|
223
|
+
Object.entries(filters).forEach(([name, value]) => {
|
|
224
|
+
// Get the filter object.
|
|
225
|
+
const filter = (0, lodash_1.zipObject)(['name', 'selector'], name.split('__'));
|
|
226
|
+
if (filter.selector) {
|
|
227
|
+
var filterQuery = dbQuery[filter.name];
|
|
228
|
+
// Init the filter.
|
|
229
|
+
if (!filterQuery) {
|
|
230
|
+
filterQuery = {};
|
|
231
|
+
}
|
|
232
|
+
if (filter.selector === 'exists') {
|
|
233
|
+
value = ((value === 'true') || (value === '1')) ? true : value;
|
|
234
|
+
value = ((value === 'false') || (value === '0')) ? false : value;
|
|
235
|
+
value = !!value;
|
|
236
|
+
}
|
|
237
|
+
else if (filter.selector === 'regex') {
|
|
238
|
+
const parts = value.match(/\/?([^/]+)\/?([^/]+)?/);
|
|
239
|
+
if (!parts) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
value = new RegExp((0, lodash_1.escapeRegExp)(parts[1]), (parts[2] || 'i'));
|
|
244
|
+
}
|
|
245
|
+
catch (err) {
|
|
246
|
+
error(err);
|
|
247
|
+
value = null;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// Special case for in filter with multiple values.
|
|
251
|
+
else if (['in', 'nin'].includes(filter.selector)) {
|
|
252
|
+
value = Array.isArray(value) ? value : value.split(',');
|
|
253
|
+
value = value.map((item) => this.getQueryValue(filter.name, item, filter.selector));
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
// Set the selector for this filter name.
|
|
257
|
+
value = this.getQueryValue(filter.name, value, filter.selector);
|
|
258
|
+
}
|
|
259
|
+
filterQuery[`$${filter.selector}`] = value;
|
|
260
|
+
dbQuery[filter.name] = filterQuery;
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
// Set the find query to this value.
|
|
265
|
+
value = this.getQueryValue(filter.name, value, filter.selector);
|
|
266
|
+
dbQuery[filter.name] = value;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
// If an authQuery is provided, merge it in.
|
|
271
|
+
if (this.authQuery && (0, lodash_1.isPlainObject)(this.authQuery)) {
|
|
272
|
+
return (0, lodash_1.merge)(dbQuery, this.authQuery);
|
|
273
|
+
}
|
|
274
|
+
// Return the dbQuery.
|
|
275
|
+
return dbQuery;
|
|
276
|
+
}
|
|
157
277
|
// Generic delete record method.
|
|
158
278
|
remove(collectionName_1) {
|
|
159
|
-
return __awaiter(this, arguments, void 0, function* (collectionName,
|
|
279
|
+
return __awaiter(this, arguments, void 0, function* (collectionName, dbQuery = {}) {
|
|
160
280
|
if (!this.db) {
|
|
161
281
|
return null;
|
|
162
282
|
}
|
|
@@ -167,7 +287,7 @@ class Database {
|
|
|
167
287
|
}
|
|
168
288
|
let result;
|
|
169
289
|
try {
|
|
170
|
-
result = yield collection.deleteOne(
|
|
290
|
+
result = yield collection.deleteOne(dbQuery);
|
|
171
291
|
}
|
|
172
292
|
catch (err) {
|
|
173
293
|
error(err.message);
|
|
@@ -177,27 +297,26 @@ class Database {
|
|
|
177
297
|
}
|
|
178
298
|
/**
|
|
179
299
|
* Ensures the form collection is created an returns it.
|
|
180
|
-
* @param scope
|
|
181
300
|
* @returns
|
|
182
301
|
*/
|
|
183
|
-
formCollection(
|
|
302
|
+
formCollection(form) {
|
|
184
303
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
-
if (this.db &&
|
|
186
|
-
if (this.currentCollection && this.currentCollectionName ===
|
|
304
|
+
if (this.db && form && form.settings && form.settings.collection) {
|
|
305
|
+
if (this.currentCollection && this.currentCollectionName === form.settings.collection) {
|
|
187
306
|
return this.currentCollection;
|
|
188
307
|
}
|
|
189
308
|
let collection = null;
|
|
190
309
|
try {
|
|
191
|
-
debug('db.createCollection()',
|
|
192
|
-
yield this.db.createCollection(this.collectionName(
|
|
193
|
-
collection = this.collection(
|
|
194
|
-
this.setupIndexes(collection,
|
|
310
|
+
debug('db.createCollection()', form.settings.collection);
|
|
311
|
+
yield this.db.createCollection(this.collectionName(form.settings.collection));
|
|
312
|
+
collection = this.collection(form.settings.collection);
|
|
313
|
+
this.setupIndexes(collection, form);
|
|
195
314
|
}
|
|
196
315
|
catch (err) {
|
|
197
316
|
error(err.message);
|
|
198
317
|
}
|
|
199
|
-
this.currentCollection = collection || this.collection(
|
|
200
|
-
this.currentCollectionName =
|
|
318
|
+
this.currentCollection = collection || this.collection(form.settings.collection);
|
|
319
|
+
this.currentCollectionName = form.settings.collection;
|
|
201
320
|
return this.currentCollection;
|
|
202
321
|
}
|
|
203
322
|
return this.defaultCollection;
|
|
@@ -206,7 +325,7 @@ class Database {
|
|
|
206
325
|
/**
|
|
207
326
|
* Setup indexes.
|
|
208
327
|
*/
|
|
209
|
-
setupIndexes(collection,
|
|
328
|
+
setupIndexes(collection, form) {
|
|
210
329
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
330
|
if (!collection) {
|
|
212
331
|
return;
|
|
@@ -232,8 +351,8 @@ class Database {
|
|
|
232
351
|
else {
|
|
233
352
|
this.addIndex(collection, 'created');
|
|
234
353
|
}
|
|
235
|
-
if (
|
|
236
|
-
|
|
354
|
+
if (form && form.components) {
|
|
355
|
+
core_1.Utils.eachComponent(form.components, (component, path) => {
|
|
237
356
|
if (component.dbIndex) {
|
|
238
357
|
this.addIndex(collection, `data.${path}`);
|
|
239
358
|
}
|
|
@@ -262,13 +381,12 @@ class Database {
|
|
|
262
381
|
}
|
|
263
382
|
/**
|
|
264
383
|
* Adds new indexes to the forms submission collection.
|
|
265
|
-
* @param scope
|
|
266
384
|
* @param indexes
|
|
267
385
|
* @returns
|
|
268
386
|
*/
|
|
269
|
-
addIndexes(
|
|
387
|
+
addIndexes(form, indexes) {
|
|
270
388
|
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
-
const collection = yield this.formCollection(
|
|
389
|
+
const collection = yield this.formCollection(form);
|
|
272
390
|
if (!collection) {
|
|
273
391
|
return;
|
|
274
392
|
}
|
|
@@ -295,13 +413,12 @@ class Database {
|
|
|
295
413
|
}
|
|
296
414
|
/**
|
|
297
415
|
* Removes indexes to the forms submission collection.
|
|
298
|
-
* @param scope
|
|
299
416
|
* @param indexes
|
|
300
417
|
* @returns
|
|
301
418
|
*/
|
|
302
|
-
removeIndexes(
|
|
419
|
+
removeIndexes(form, indexes) {
|
|
303
420
|
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
-
const collection = yield this.formCollection(
|
|
421
|
+
const collection = yield this.formCollection(form);
|
|
305
422
|
if (!collection) {
|
|
306
423
|
return;
|
|
307
424
|
}
|
|
@@ -315,59 +432,60 @@ class Database {
|
|
|
315
432
|
* @param {*} table
|
|
316
433
|
* @param {*} query
|
|
317
434
|
*/
|
|
318
|
-
index(
|
|
319
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
435
|
+
index(form_1) {
|
|
436
|
+
return __awaiter(this, arguments, void 0, function* (form, urlQuery = {}, collection = null) {
|
|
320
437
|
let items = [], skip = 0, limit = 10, count = 0;
|
|
321
438
|
try {
|
|
322
|
-
limit =
|
|
323
|
-
skip =
|
|
324
|
-
const sort =
|
|
325
|
-
const includeDeleted =
|
|
439
|
+
limit = urlQuery.limit || 10;
|
|
440
|
+
skip = urlQuery.skip || 0;
|
|
441
|
+
const sort = urlQuery.sort || { created: -1 };
|
|
442
|
+
const includeDeleted = urlQuery.includeDeleted || false;
|
|
326
443
|
let project = {};
|
|
327
|
-
if (
|
|
328
|
-
if (typeof
|
|
329
|
-
const fields =
|
|
444
|
+
if (urlQuery.select) {
|
|
445
|
+
if (typeof urlQuery.select === 'string') {
|
|
446
|
+
const fields = urlQuery.select.split(',');
|
|
330
447
|
fields.forEach((field) => {
|
|
331
448
|
project[field] = true;
|
|
332
449
|
});
|
|
333
450
|
}
|
|
334
451
|
else {
|
|
335
|
-
Object.keys(
|
|
452
|
+
Object.keys(urlQuery.select).forEach((field) => {
|
|
336
453
|
project[field] = true;
|
|
337
454
|
});
|
|
338
455
|
}
|
|
339
456
|
}
|
|
340
|
-
delete
|
|
341
|
-
delete
|
|
342
|
-
delete
|
|
343
|
-
delete
|
|
344
|
-
delete
|
|
345
|
-
debug('db.index()',
|
|
346
|
-
|
|
347
|
-
|
|
457
|
+
delete urlQuery.limit;
|
|
458
|
+
delete urlQuery.skip;
|
|
459
|
+
delete urlQuery.sort;
|
|
460
|
+
delete urlQuery.select;
|
|
461
|
+
delete urlQuery.includeDeleted;
|
|
462
|
+
debug('db.index()', urlQuery);
|
|
463
|
+
const dbQuery = this.dbQuery(urlQuery);
|
|
464
|
+
items = (yield this.find(form, dbQuery, limit, skip, sort, project, collection, includeDeleted)) || [];
|
|
465
|
+
count = yield this.count(form, dbQuery, collection, includeDeleted);
|
|
348
466
|
}
|
|
349
467
|
catch (err) {
|
|
350
468
|
error(err.message);
|
|
351
469
|
}
|
|
352
|
-
return { items, limit, skip, count };
|
|
470
|
+
return { items: items, limit, skip, count };
|
|
353
471
|
});
|
|
354
472
|
}
|
|
355
473
|
/**
|
|
356
474
|
* Perform a count of the amount of documents within a collection.
|
|
357
|
-
* @param
|
|
475
|
+
* @param form
|
|
358
476
|
* @param query
|
|
359
477
|
* @returns
|
|
360
478
|
*/
|
|
361
|
-
count(
|
|
362
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
479
|
+
count(form_1) {
|
|
480
|
+
return __awaiter(this, arguments, void 0, function* (form, dbQuery = {}, inCollection = null, includeDeleted = false) {
|
|
363
481
|
let count = 0;
|
|
364
482
|
try {
|
|
365
|
-
debug('db.count()',
|
|
366
|
-
const collection = inCollection || (yield this.formCollection(
|
|
483
|
+
debug('db.count()', dbQuery);
|
|
484
|
+
const collection = inCollection || (yield this.formCollection(form));
|
|
367
485
|
if (!collection) {
|
|
368
486
|
return 0;
|
|
369
487
|
}
|
|
370
|
-
count = yield collection.countDocuments(this.query(
|
|
488
|
+
count = yield collection.countDocuments(this.query(form, dbQuery, includeDeleted));
|
|
371
489
|
}
|
|
372
490
|
catch (err) {
|
|
373
491
|
error(err.message);
|
|
@@ -378,21 +496,21 @@ class Database {
|
|
|
378
496
|
/**
|
|
379
497
|
* Create a new record.
|
|
380
498
|
*/
|
|
381
|
-
create(
|
|
382
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
499
|
+
create(form_1, record_1) {
|
|
500
|
+
return __awaiter(this, arguments, void 0, function* (form, record, allowFields = []) {
|
|
383
501
|
record.deleted = null;
|
|
384
502
|
record.created = new Date();
|
|
385
503
|
record.modified = new Date();
|
|
386
|
-
if (
|
|
387
|
-
record.form = this.ObjectId(
|
|
504
|
+
if (form && form._id) {
|
|
505
|
+
record.form = this.ObjectId(form._id);
|
|
388
506
|
}
|
|
389
|
-
if (
|
|
390
|
-
record.project = this.ObjectId(
|
|
507
|
+
if (form.project && form.project) {
|
|
508
|
+
record.project = this.ObjectId(form.project);
|
|
391
509
|
}
|
|
392
510
|
if (record.owner) {
|
|
393
511
|
record.owner = this.ObjectId(record.owner);
|
|
394
512
|
}
|
|
395
|
-
const collection = yield this.formCollection(
|
|
513
|
+
const collection = yield this.formCollection(form);
|
|
396
514
|
try {
|
|
397
515
|
debug('db.create()', record);
|
|
398
516
|
const result = yield collection.insertOne((0, lodash_1.pick)(record, [
|
|
@@ -410,10 +528,11 @@ class Database {
|
|
|
410
528
|
if (!result.insertedId) {
|
|
411
529
|
return null;
|
|
412
530
|
}
|
|
413
|
-
return yield this.read(
|
|
531
|
+
return yield this.read(form, result.insertedId);
|
|
414
532
|
}
|
|
415
533
|
catch (err) {
|
|
416
534
|
error(err.message);
|
|
535
|
+
return null;
|
|
417
536
|
}
|
|
418
537
|
});
|
|
419
538
|
}
|
|
@@ -423,7 +542,7 @@ class Database {
|
|
|
423
542
|
* @param {*} id
|
|
424
543
|
* @returns
|
|
425
544
|
*/
|
|
426
|
-
query(
|
|
545
|
+
query(form, query = {}, includeAll = false) {
|
|
427
546
|
if (query._id) {
|
|
428
547
|
query._id = this.ObjectId(query._id);
|
|
429
548
|
}
|
|
@@ -433,11 +552,11 @@ class Database {
|
|
|
433
552
|
if (query.form) {
|
|
434
553
|
query.form = this.ObjectId(query.form);
|
|
435
554
|
}
|
|
436
|
-
else if (!includeAll &&
|
|
437
|
-
query.form = this.ObjectId(
|
|
555
|
+
else if (!includeAll && form && form._id) {
|
|
556
|
+
query.form = this.ObjectId(form._id);
|
|
438
557
|
}
|
|
439
|
-
if (!includeAll &&
|
|
440
|
-
query.project = this.ObjectId(
|
|
558
|
+
if (!includeAll && form.project) {
|
|
559
|
+
query.project = this.ObjectId(form.project);
|
|
441
560
|
}
|
|
442
561
|
if (!includeAll) {
|
|
443
562
|
query.deleted = { $eq: null };
|
|
@@ -445,7 +564,7 @@ class Database {
|
|
|
445
564
|
// Handle nested queries.
|
|
446
565
|
['$or', '$and'].forEach((key) => {
|
|
447
566
|
if (query[key]) {
|
|
448
|
-
query[key].forEach((subQuery) => this.query(
|
|
567
|
+
query[key].forEach((subQuery) => this.query(form, subQuery, true));
|
|
449
568
|
}
|
|
450
569
|
});
|
|
451
570
|
return query;
|
|
@@ -453,15 +572,15 @@ class Database {
|
|
|
453
572
|
/**
|
|
454
573
|
* Find many records that match a query.
|
|
455
574
|
*/
|
|
456
|
-
find(
|
|
457
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
575
|
+
find(form_1) {
|
|
576
|
+
return __awaiter(this, arguments, void 0, function* (form, dbQuery = {}, limit = 10, skip = 0, sort = { created: -1 }, select = {}, inCollection = null, includeDeleted = false) {
|
|
458
577
|
try {
|
|
459
|
-
debug('db.find()',
|
|
460
|
-
const collection = inCollection || (yield this.formCollection(
|
|
578
|
+
debug('db.find()', dbQuery);
|
|
579
|
+
const collection = inCollection || (yield this.formCollection(form));
|
|
461
580
|
if (!collection) {
|
|
462
581
|
return [];
|
|
463
582
|
}
|
|
464
|
-
return yield collection.find(this.query(
|
|
583
|
+
return yield collection.find(this.query(form, dbQuery, includeDeleted)).limit(limit).skip(skip).sort(sort).project(select).toArray();
|
|
465
584
|
}
|
|
466
585
|
catch (err) {
|
|
467
586
|
error(err.message);
|
|
@@ -472,15 +591,19 @@ class Database {
|
|
|
472
591
|
/**
|
|
473
592
|
* Find a record for a provided query.
|
|
474
593
|
*/
|
|
475
|
-
findOne(
|
|
476
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
594
|
+
findOne(form_1) {
|
|
595
|
+
return __awaiter(this, arguments, void 0, function* (form, dbQuery = {}, options = {}) {
|
|
477
596
|
try {
|
|
478
|
-
debug('db.findOne()',
|
|
479
|
-
const collection = yield this.formCollection(
|
|
597
|
+
debug('db.findOne()', dbQuery);
|
|
598
|
+
const collection = yield this.formCollection(form);
|
|
480
599
|
if (!collection) {
|
|
481
600
|
return null;
|
|
482
601
|
}
|
|
483
|
-
|
|
602
|
+
const doc = yield collection.findOne(this.query(form, dbQuery));
|
|
603
|
+
if (!doc) {
|
|
604
|
+
return null;
|
|
605
|
+
}
|
|
606
|
+
return doc;
|
|
484
607
|
}
|
|
485
608
|
catch (err) {
|
|
486
609
|
error(err.message);
|
|
@@ -494,18 +617,22 @@ class Database {
|
|
|
494
617
|
* @param {*} table
|
|
495
618
|
* @param {*} query
|
|
496
619
|
*/
|
|
497
|
-
read(
|
|
620
|
+
read(form, id) {
|
|
498
621
|
return __awaiter(this, void 0, void 0, function* () {
|
|
499
622
|
try {
|
|
500
623
|
debug('db.read()', id);
|
|
501
624
|
if (!id) {
|
|
502
625
|
return null;
|
|
503
626
|
}
|
|
504
|
-
const collection = yield this.formCollection(
|
|
627
|
+
const collection = yield this.formCollection(form);
|
|
505
628
|
if (!collection) {
|
|
506
629
|
return null;
|
|
507
630
|
}
|
|
508
|
-
|
|
631
|
+
const doc = yield collection.findOne(this.query(form, { _id: this.ObjectId(id) }));
|
|
632
|
+
if (!doc) {
|
|
633
|
+
return null;
|
|
634
|
+
}
|
|
635
|
+
return doc;
|
|
509
636
|
}
|
|
510
637
|
catch (err) {
|
|
511
638
|
error(err.message);
|
|
@@ -519,25 +646,25 @@ class Database {
|
|
|
519
646
|
* @param {*} table
|
|
520
647
|
* @param {*} query
|
|
521
648
|
*/
|
|
522
|
-
update(
|
|
523
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
649
|
+
update(form_1, id_1, update_1) {
|
|
650
|
+
return __awaiter(this, arguments, void 0, function* (form, id, update, allowFields = []) {
|
|
524
651
|
if (!id || !update) {
|
|
525
652
|
return null;
|
|
526
653
|
}
|
|
527
654
|
update.modified = new Date();
|
|
528
655
|
try {
|
|
529
656
|
debug('db.update()', id, update);
|
|
530
|
-
const collection = yield this.formCollection(
|
|
657
|
+
const collection = yield this.formCollection(form);
|
|
531
658
|
if (!collection) {
|
|
532
659
|
return null;
|
|
533
660
|
}
|
|
534
|
-
const result = yield collection.updateOne(this.query(
|
|
661
|
+
const result = yield collection.updateOne(this.query(form, { _id: this.ObjectId(id) }), {
|
|
535
662
|
$set: (0, lodash_1.pick)(update, ['data', 'metadata', 'modified', 'state'].concat(allowFields))
|
|
536
663
|
});
|
|
537
664
|
if (result.modifiedCount === 0) {
|
|
538
665
|
return null;
|
|
539
666
|
}
|
|
540
|
-
return yield this.read(
|
|
667
|
+
return yield this.read(form, id);
|
|
541
668
|
}
|
|
542
669
|
catch (err) {
|
|
543
670
|
error(err.message);
|
|
@@ -548,25 +675,25 @@ class Database {
|
|
|
548
675
|
/**
|
|
549
676
|
* Delete a record from the database.
|
|
550
677
|
*/
|
|
551
|
-
delete(
|
|
552
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
678
|
+
delete(form_1, id_1) {
|
|
679
|
+
return __awaiter(this, arguments, void 0, function* (form, id, hard = false) {
|
|
553
680
|
if (!id) {
|
|
554
681
|
return false;
|
|
555
682
|
}
|
|
556
683
|
try {
|
|
557
684
|
debug('db.delete()', id, hard ? '(hard)' : '(soft)');
|
|
558
|
-
const collection = yield this.formCollection(
|
|
685
|
+
const collection = yield this.formCollection(form);
|
|
559
686
|
if (!collection) {
|
|
560
687
|
return false;
|
|
561
688
|
}
|
|
562
689
|
if (hard) {
|
|
563
690
|
// Hard delete - completely remove the record from the database
|
|
564
|
-
const result = yield collection.deleteOne(this.query(
|
|
691
|
+
const result = yield collection.deleteOne(this.query(form, { _id: this.ObjectId(id) }, true));
|
|
565
692
|
return result.deletedCount > 0;
|
|
566
693
|
}
|
|
567
694
|
else {
|
|
568
695
|
// Soft delete - set deleted timestamp
|
|
569
|
-
const result = yield collection.updateOne(this.query(
|
|
696
|
+
const result = yield collection.updateOne(this.query(form, { _id: this.ObjectId(id) }), {
|
|
570
697
|
$set: {
|
|
571
698
|
deleted: new Date(),
|
|
572
699
|
modified: new Date()
|
|
@@ -610,15 +737,14 @@ class Database {
|
|
|
610
737
|
* - If the value is not unique, it returns the id of the non-unique submission.
|
|
611
738
|
* - If an error occurs, or uniqueness cannot be determined, it will throw an error.
|
|
612
739
|
*
|
|
613
|
-
* @param
|
|
740
|
+
* @param form
|
|
614
741
|
* @param context
|
|
615
742
|
* @param value
|
|
616
743
|
* @returns
|
|
617
744
|
*/
|
|
618
|
-
isUnique(
|
|
745
|
+
isUnique(form, component, submission, componentPath, value) {
|
|
619
746
|
return __awaiter(this, void 0, void 0, function* () {
|
|
620
|
-
const
|
|
621
|
-
const path = `data.${context.path}`;
|
|
747
|
+
const path = `data.${componentPath}`;
|
|
622
748
|
if (!form) {
|
|
623
749
|
return false;
|
|
624
750
|
}
|
|
@@ -666,8 +792,8 @@ class Database {
|
|
|
666
792
|
// Perform the query.
|
|
667
793
|
let result = null;
|
|
668
794
|
try {
|
|
669
|
-
result = yield this.findOne(
|
|
670
|
-
if (!result || ((result === null || result === void 0 ? void 0 : result._id.toString()) === (submission === null || submission === void 0 ? void 0 : submission._id))) {
|
|
795
|
+
result = yield this.findOne(form, query, options);
|
|
796
|
+
if (!(result === null || result === void 0 ? void 0 : result._id) || ((result === null || result === void 0 ? void 0 : result._id.toString()) === (submission === null || submission === void 0 ? void 0 : submission._id))) {
|
|
671
797
|
return true;
|
|
672
798
|
}
|
|
673
799
|
else {
|
|
@@ -683,8 +809,8 @@ class Database {
|
|
|
683
809
|
$options: 'i'
|
|
684
810
|
}, query, path);
|
|
685
811
|
try {
|
|
686
|
-
result = yield this.findOne(
|
|
687
|
-
if (!result || ((result === null || result === void 0 ? void 0 : result._id.toString()) === (submission === null || submission === void 0 ? void 0 : submission._id))) {
|
|
812
|
+
result = yield this.findOne(form, query);
|
|
813
|
+
if (!(result === null || result === void 0 ? void 0 : result._id) || ((result === null || result === void 0 ? void 0 : result._id.toString()) === (submission === null || submission === void 0 ? void 0 : submission._id))) {
|
|
688
814
|
return true;
|
|
689
815
|
}
|
|
690
816
|
else {
|