@formio/appserver 2.0.0-dev.3 → 2.0.0-dev.30

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/db.js DELETED
@@ -1,445 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Database = void 0;
16
- const mongodb_1 = require("mongodb");
17
- const pick_1 = __importDefault(require("lodash/pick"));
18
- const omit_1 = __importDefault(require("lodash/omit"));
19
- const debug = require('debug')('formio:db');
20
- const error = require('debug')('formio:error');
21
- class Database {
22
- constructor(config) {
23
- this.config = config;
24
- this.prefix = '';
25
- this.db = null;
26
- this.currentCollectionName = 'submissions';
27
- this.currentCollection = null;
28
- this.defaultCollection = null;
29
- }
30
- ObjectId(id) {
31
- if (id === null || id === void 0 ? void 0 : id.hasOwnProperty('$in')) {
32
- return { $in: id['$in'].map((_id) => this.ObjectId(_id)) };
33
- }
34
- try {
35
- return id ? (new mongodb_1.ObjectId(id)) : null;
36
- }
37
- catch (e) {
38
- return id;
39
- }
40
- }
41
- collectionName(name) {
42
- return `${this.prefix}${name}`;
43
- }
44
- /**
45
- * Connect to the database.
46
- * @param {*} scope
47
- * @returns
48
- */
49
- connect(prefix = '') {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- this.prefix = prefix ? `${prefix}.` : '';
52
- try {
53
- debug('db.connect()');
54
- const config = this.config.config ? JSON.parse(this.config.config) : {};
55
- const client = new mongodb_1.MongoClient(this.config.url, config);
56
- yield client.connect();
57
- this.db = yield client.db();
58
- this.addIndex(this.db.collection(this.collectionName('project')), 'name');
59
- this.defaultCollection = this.db.collection(this.collectionName('submissions'));
60
- yield this.setupIndexes(this.defaultCollection);
61
- debug('Connected to database');
62
- }
63
- catch (err) {
64
- error(err.message);
65
- process.exit();
66
- }
67
- });
68
- }
69
- // Generic save record method.
70
- save(collectionName, item) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- if (!this.db) {
73
- return null;
74
- }
75
- const collection = this.db.collection(`${this.prefix}${collectionName}`);
76
- let result;
77
- try {
78
- debug('db.save()', collectionName);
79
- result = yield collection.findOneAndUpdate({ _id: this.ObjectId(item._id) }, { $set: (0, omit_1.default)(item, '_id') }, { upsert: true, returnOriginal: false });
80
- }
81
- catch (err) {
82
- error(err.message);
83
- }
84
- return result ? result.value : null;
85
- });
86
- }
87
- // Generic load record method.
88
- load(collectionName) {
89
- return __awaiter(this, void 0, void 0, function* () {
90
- if (!this.db) {
91
- return null;
92
- }
93
- debug('db.load()', collectionName);
94
- const collection = this.db.collection(this.collectionName(collectionName));
95
- let item;
96
- try {
97
- item = yield collection.findOne({});
98
- }
99
- catch (err) {
100
- error(err.message);
101
- }
102
- return item;
103
- });
104
- }
105
- // Generic delete record method.
106
- remove(collectionName) {
107
- return __awaiter(this, void 0, void 0, function* () {
108
- if (!this.db) {
109
- return null;
110
- }
111
- debug('db.remove()', collectionName);
112
- const collection = this.db.collection(this.collectionName(collectionName));
113
- let result;
114
- try {
115
- result = yield collection.deleteOne({});
116
- }
117
- catch (err) {
118
- error(err.message);
119
- }
120
- return result ? result.deletedCount : null;
121
- });
122
- }
123
- /**
124
- * Ensures the form collection is created an returns it.
125
- * @param scope
126
- * @returns
127
- */
128
- formCollection(scope) {
129
- return __awaiter(this, void 0, void 0, function* () {
130
- if (this.db && scope && scope.form && scope.form.settings && scope.form.settings.collection) {
131
- if (this.currentCollection && this.currentCollectionName === scope.form.settings.collection) {
132
- return this.currentCollection;
133
- }
134
- let collection = null;
135
- try {
136
- debug('db.createCollection()', scope.form.settings.collection);
137
- yield this.db.createCollection(this.collectionName(scope.form.settings.collection));
138
- collection = this.db.collection(this.collectionName(scope.form.settings.collection));
139
- this.setupIndexes(collection, scope);
140
- }
141
- catch (err) {
142
- error(err.message);
143
- }
144
- this.currentCollection = collection || this.db.collection(this.collectionName(scope.form.settings.collection));
145
- this.currentCollectionName = scope.form.settings.collection;
146
- return this.currentCollection;
147
- }
148
- return this.defaultCollection;
149
- });
150
- }
151
- /**
152
- * Setup indexes.
153
- */
154
- setupIndexes(collection, scope) {
155
- return __awaiter(this, void 0, void 0, function* () {
156
- if (!collection) {
157
- return;
158
- }
159
- this.addIndex(collection, 'project');
160
- this.addIndex(collection, 'form');
161
- this.addIndex(collection, 'deleted');
162
- this.addIndex(collection, 'modified');
163
- this.addIndex(collection, 'created');
164
- if (scope && scope.form && scope.form.components) {
165
- scope.utils.eachComponent(scope.form.components, (component, components, path) => {
166
- if (component.dbIndex) {
167
- this.addIndex(collection, `data.${path}`);
168
- }
169
- });
170
- }
171
- });
172
- }
173
- /**
174
- * Add a field index
175
- */
176
- addIndex(collection, path) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- const index = {};
179
- index[path] = 1;
180
- try {
181
- debug('db.addIndex()', path);
182
- collection.createIndex(index, { background: true });
183
- }
184
- catch (err) {
185
- error(`Cannot add index ${path}`, err.message);
186
- }
187
- });
188
- }
189
- /**
190
- * Adds new indexes to the forms submission collection.
191
- * @param scope
192
- * @param indexes
193
- * @returns
194
- */
195
- addIndexes(scope, indexes) {
196
- return __awaiter(this, void 0, void 0, function* () {
197
- const collection = yield this.formCollection(scope);
198
- if (!collection) {
199
- return;
200
- }
201
- for (const path of indexes) {
202
- yield this.addIndex(collection, `data.${path}`);
203
- }
204
- });
205
- }
206
- /**
207
- * Remove a field index.
208
- */
209
- removeIndex(collection, path) {
210
- return __awaiter(this, void 0, void 0, function* () {
211
- const index = {};
212
- index[path] = 1;
213
- try {
214
- debug('db.removeIndex()', path);
215
- collection.dropIndex(index);
216
- }
217
- catch (err) {
218
- error(`Cannot remove index ${path}`, err.message);
219
- }
220
- });
221
- }
222
- /**
223
- * Removes indexes to the forms submission collection.
224
- * @param scope
225
- * @param indexes
226
- * @returns
227
- */
228
- removeIndexes(scope, indexes) {
229
- return __awaiter(this, void 0, void 0, function* () {
230
- const collection = yield this.formCollection(scope);
231
- if (!collection) {
232
- return;
233
- }
234
- for (const path of indexes) {
235
- yield this.removeIndex(collection, `data.${path}`);
236
- }
237
- });
238
- }
239
- /**
240
- * Fetch a list of submissions from a table/collection.
241
- * @param {*} table
242
- * @param {*} query
243
- */
244
- index(scope, query = {}) {
245
- return __awaiter(this, void 0, void 0, function* () {
246
- let items;
247
- try {
248
- debug('db.index()', query);
249
- items = yield this.find(scope, query);
250
- }
251
- catch (err) {
252
- error(err.message);
253
- }
254
- return items;
255
- });
256
- }
257
- /**
258
- * Create a new record.
259
- */
260
- create(scope, record, allowFields = []) {
261
- return __awaiter(this, void 0, void 0, function* () {
262
- record.deleted = null;
263
- record.created = new Date();
264
- record.modified = new Date();
265
- if (scope.form && scope.form._id) {
266
- record.form = this.ObjectId(scope.form._id);
267
- }
268
- if (scope.project && scope.project._id) {
269
- record.project = this.ObjectId(scope.project._id);
270
- }
271
- if (record.owner) {
272
- record.owner = this.ObjectId(record.owner);
273
- }
274
- const collection = yield this.formCollection(scope);
275
- try {
276
- debug('db.create()', record);
277
- const result = yield collection.insertOne((0, pick_1.default)(record, [
278
- 'data',
279
- 'metadata',
280
- 'modified',
281
- 'created',
282
- 'deleted',
283
- 'form',
284
- 'project',
285
- 'owner',
286
- 'access'
287
- ].concat(allowFields)));
288
- if (!result.insertedId) {
289
- return null;
290
- }
291
- return yield this.read(scope, result.insertedId);
292
- }
293
- catch (err) {
294
- error(err.message);
295
- }
296
- });
297
- }
298
- /**
299
- * Return a query for a single mongo record.
300
- * @param {*} form
301
- * @param {*} id
302
- * @returns
303
- */
304
- query(scope, query = {}) {
305
- if (query._id) {
306
- query._id = this.ObjectId(query._id);
307
- }
308
- if (query.owner) {
309
- query.owner = this.ObjectId(query.owner);
310
- }
311
- if (query.form) {
312
- query.form = this.ObjectId(query.form);
313
- }
314
- else if (scope.form && scope.form._id) {
315
- query.form = this.ObjectId(scope.form._id);
316
- }
317
- if (scope.project && scope.project._id) {
318
- query.project = this.ObjectId(scope.project._id);
319
- }
320
- query.deleted = { $eq: null };
321
- // Handle nested queries.
322
- ['$or', '$and'].forEach((key) => {
323
- if (query[key]) {
324
- query[key].forEach((subQuery) => this.query(scope, subQuery));
325
- }
326
- });
327
- return query;
328
- }
329
- /**
330
- * Find many records that match a query.
331
- */
332
- find(scope, query = {}) {
333
- return __awaiter(this, void 0, void 0, function* () {
334
- try {
335
- debug('db.find()', query);
336
- const collection = yield this.formCollection(scope);
337
- if (!collection) {
338
- return [];
339
- }
340
- return yield collection.find(this.query(scope, query)).toArray();
341
- }
342
- catch (err) {
343
- error(err.message);
344
- }
345
- return [];
346
- });
347
- }
348
- /**
349
- * Find a record for a provided query.
350
- */
351
- findOne(scope, query = {}) {
352
- return __awaiter(this, void 0, void 0, function* () {
353
- try {
354
- debug('db.findOne()', query);
355
- const collection = yield this.formCollection(scope);
356
- if (!collection) {
357
- return null;
358
- }
359
- return yield collection.findOne(this.query(scope, query));
360
- }
361
- catch (err) {
362
- error(err.message);
363
- }
364
- return null;
365
- });
366
- }
367
- /**
368
- * Read a single submission from a form
369
- *
370
- * @param {*} table
371
- * @param {*} query
372
- */
373
- read(scope, id) {
374
- return __awaiter(this, void 0, void 0, function* () {
375
- try {
376
- debug('db.read()', id);
377
- const collection = yield this.formCollection(scope);
378
- if (!collection) {
379
- return null;
380
- }
381
- return yield collection.findOne(this.query(scope, { _id: id }));
382
- }
383
- catch (err) {
384
- error(err.message);
385
- }
386
- return null;
387
- });
388
- }
389
- /**
390
- * Update a single submission of a form.
391
- *
392
- * @param {*} table
393
- * @param {*} query
394
- */
395
- update(scope, id, update, allowFields = []) {
396
- return __awaiter(this, void 0, void 0, function* () {
397
- if (!id || !update) {
398
- return null;
399
- }
400
- update.modified = new Date();
401
- try {
402
- debug('db.update()', id, update);
403
- const collection = yield this.formCollection(scope);
404
- if (!collection) {
405
- return null;
406
- }
407
- const result = yield collection.updateOne(this.query(scope, { _id: id }), {
408
- $set: (0, pick_1.default)(update, ['data', 'metadata', 'modified'].concat(allowFields))
409
- });
410
- if (result.modifiedCount === 0) {
411
- return null;
412
- }
413
- return yield this.read(scope, id);
414
- }
415
- catch (err) {
416
- error(err.message);
417
- }
418
- return null;
419
- });
420
- }
421
- /**
422
- * Delete a record from the database.
423
- */
424
- delete(scope, id) {
425
- return __awaiter(this, void 0, void 0, function* () {
426
- if (!id) {
427
- return false;
428
- }
429
- try {
430
- debug('db.delete()', id);
431
- const collection = yield this.formCollection(scope);
432
- if (!collection) {
433
- return false;
434
- }
435
- const result = yield collection.updateOne(this.query(scope, { _id: id }), { $set: { deleted: Date.now() } });
436
- return result.modifiedCount > 0;
437
- }
438
- catch (err) {
439
- error(err.message);
440
- }
441
- return false;
442
- });
443
- }
444
- }
445
- exports.Database = Database;
package/lib/index.d.ts DELETED
@@ -1,180 +0,0 @@
1
- /// <reference types="express" />
2
- import { Server as CoreServer } from '@formio/appserver-core';
3
- import { Auth } from "./auth";
4
- import { Database } from "./db";
5
- import Actions from "./actions";
6
- import { Processor } from "./process";
7
- import { Prepper } from './prepare';
8
- import { AppServerScope, Processor as ProcessorType, Prepper as PrepperType } from '@formio/appserver-types';
9
- export declare const Modules: {
10
- db: typeof Database;
11
- auth: typeof Auth;
12
- processor: ProcessorType;
13
- prepper: PrepperType;
14
- actions: {
15
- save: {
16
- readonly info: {
17
- name: string;
18
- title: string;
19
- description: string;
20
- priority: number;
21
- defaults: {
22
- handler: string[];
23
- method: string[];
24
- };
25
- access: {
26
- handler: boolean;
27
- method: boolean;
28
- };
29
- };
30
- mappingComponents(scope: import("@formio/appserver-types").ActionScope): Promise<any>;
31
- settingsForm(scope: import("@formio/appserver-types").ActionScope): Promise<any>;
32
- saveToForm(scope: import("@formio/appserver-types").ActionScope, resource: string): undefined;
33
- childSubmission(scope: import("@formio/appserver-types").ActionScope, req: import("@formio/appserver-types").SubmissionRequest, res: import("@formio/appserver-types").SubmissionResponse, submission: any): any;
34
- childResponse(scope: import("@formio/appserver-types").ActionScope, req: import("@formio/appserver-types").SubmissionRequest, res: import("@formio/appserver-types").SubmissionResponse): void;
35
- executor(scope: import("@formio/appserver-types").ActionScope): Promise<(req: import("@formio/appserver-types").SubmissionRequest, res: import("@formio/appserver-types").SubmissionResponse, next: import("express").NextFunction) => Promise<void>>;
36
- };
37
- login: {
38
- readonly info: {
39
- name: string;
40
- title: string;
41
- description: string;
42
- priority: number;
43
- defaults: {
44
- handler: string[];
45
- method: string[];
46
- };
47
- access: {
48
- handler: boolean;
49
- method: boolean;
50
- };
51
- };
52
- settingsForm(scope: import("@formio/appserver-types").ActionScope): Promise<({
53
- type: string;
54
- label: string;
55
- key: string;
56
- placeholder: string;
57
- dataSrc: string;
58
- valueProperty: string;
59
- data: {
60
- json: any;
61
- };
62
- multiple: boolean;
63
- validate: {
64
- required: boolean;
65
- };
66
- input?: undefined;
67
- description?: undefined;
68
- defaultValue?: undefined;
69
- suffix?: undefined;
70
- } | {
71
- type: string;
72
- input: boolean;
73
- label: string;
74
- key: string;
75
- placeholder: string;
76
- dataSrc: string;
77
- valueProperty: string;
78
- data: {
79
- json: any;
80
- };
81
- multiple: boolean;
82
- validate: {
83
- required: boolean;
84
- };
85
- description?: undefined;
86
- defaultValue?: undefined;
87
- suffix?: undefined;
88
- } | {
89
- type: string;
90
- key: string;
91
- input: boolean;
92
- label: string;
93
- description: string;
94
- defaultValue: string;
95
- placeholder?: undefined;
96
- dataSrc?: undefined;
97
- valueProperty?: undefined;
98
- data?: undefined;
99
- multiple?: undefined;
100
- validate?: undefined;
101
- suffix?: undefined;
102
- } | {
103
- type: string;
104
- key: string;
105
- input: boolean;
106
- label: string;
107
- description: string;
108
- defaultValue: string;
109
- suffix: string;
110
- placeholder?: undefined;
111
- dataSrc?: undefined;
112
- valueProperty?: undefined;
113
- data?: undefined;
114
- multiple?: undefined;
115
- validate?: undefined;
116
- })[]>;
117
- waitText(time: number): string;
118
- checkAttempts(scope: import("@formio/appserver-types").ActionScope, error: any, user: import("@formio/appserver-types").User): Promise<any>;
119
- executor(scope: import("@formio/appserver-types").ActionScope): Promise<(req: import("@formio/appserver-types").SubmissionRequest, res: import("@formio/appserver-types").SubmissionResponse, next: import("express").NextFunction) => Promise<void>>;
120
- };
121
- role: {
122
- readonly info: {
123
- name: string;
124
- title: string;
125
- description: string;
126
- priority: number;
127
- defaults: {
128
- handler: string[];
129
- method: string[];
130
- };
131
- access: {
132
- handler: boolean;
133
- method: boolean;
134
- };
135
- };
136
- settingsForm(scope: import("@formio/appserver-types").ActionScope): Promise<({
137
- type: string;
138
- input: boolean;
139
- label: string;
140
- key: string;
141
- placeholder: string;
142
- template: string;
143
- dataSrc: string;
144
- data: {
145
- json: string;
146
- };
147
- valueProperty: string;
148
- multiple: boolean;
149
- validate: {
150
- required: boolean;
151
- };
152
- } | {
153
- type: string;
154
- input: boolean;
155
- label: string;
156
- key: string;
157
- placeholder: string;
158
- template: string;
159
- dataSrc: string;
160
- data: {
161
- json: unknown[];
162
- };
163
- valueProperty: string;
164
- multiple: boolean;
165
- validate: {
166
- required: boolean;
167
- };
168
- })[]>;
169
- executor(scope: import("@formio/appserver-types").ActionScope): Promise<(req: import("@formio/appserver-types").SubmissionRequest, res: import("@formio/appserver-types").SubmissionResponse, next: import("express").NextFunction) => Promise<void | import("@formio/appserver-types").SubmissionResponse>>;
170
- };
171
- };
172
- };
173
- export declare class Server extends CoreServer {
174
- constructor(config?: AppServerScope);
175
- }
176
- export { Prepper };
177
- export { Processor };
178
- export { Database };
179
- export { Auth };
180
- export { Actions };