@restorecommerce/chassis-srv 0.3.3 → 0.3.7

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/lib/cache/index.js +22 -3
  3. package/lib/cache/index.js.map +1 -1
  4. package/lib/command-interface/index.d.ts +3 -3
  5. package/lib/command-interface/index.js +432 -498
  6. package/lib/command-interface/index.js.map +1 -1
  7. package/lib/config/index.js +6 -15
  8. package/lib/config/index.js.map +1 -1
  9. package/lib/database/index.d.ts +2 -4
  10. package/lib/database/index.js +4 -13
  11. package/lib/database/index.js.map +1 -1
  12. package/lib/database/provider/arango/base.js +314 -322
  13. package/lib/database/provider/arango/base.js.map +1 -1
  14. package/lib/database/provider/arango/common.js +70 -57
  15. package/lib/database/provider/arango/common.js.map +1 -1
  16. package/lib/database/provider/arango/graph.d.ts +4 -8
  17. package/lib/database/provider/arango/graph.js +335 -470
  18. package/lib/database/provider/arango/graph.js.map +1 -1
  19. package/lib/database/provider/arango/index.js +34 -21
  20. package/lib/database/provider/arango/index.js.map +1 -1
  21. package/lib/database/provider/arango/interface.d.ts +70 -0
  22. package/lib/database/provider/arango/interface.js +46 -0
  23. package/lib/database/provider/arango/interface.js.map +1 -0
  24. package/lib/database/provider/arango/utils.d.ts +77 -0
  25. package/lib/database/provider/arango/utils.js +587 -0
  26. package/lib/database/provider/arango/utils.js.map +1 -0
  27. package/lib/database/provider/nedb/index.js +203 -206
  28. package/lib/database/provider/nedb/index.js.map +1 -1
  29. package/lib/health/index.js +36 -42
  30. package/lib/health/index.js.map +1 -1
  31. package/lib/index.d.ts +4 -0
  32. package/lib/index.js +27 -6
  33. package/lib/index.js.map +1 -1
  34. package/lib/microservice/endpoint.js +33 -23
  35. package/lib/microservice/endpoint.js.map +1 -1
  36. package/lib/microservice/server.js +115 -106
  37. package/lib/microservice/server.js.map +1 -1
  38. package/lib/microservice/transport/provider/grpc/index.js +58 -52
  39. package/lib/microservice/transport/provider/grpc/index.js.map +1 -1
  40. package/lib/microservice/transport/provider/grpc/reflection.js +101 -93
  41. package/lib/microservice/transport/provider/grpc/reflection.js.map +1 -1
  42. package/lib/offsets/index.d.ts +2 -2
  43. package/lib/offsets/index.js +41 -42
  44. package/lib/offsets/index.js.map +1 -1
  45. package/package.json +29 -29
  46. package/tsconfig.json +13 -6
  47. package/setupTopics.js +0 -32
@@ -1,17 +1,27 @@
1
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
- });
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
10
20
  };
11
21
  Object.defineProperty(exports, "__esModule", { value: true });
12
22
  exports.Arango = void 0;
13
23
  const arangojs_1 = require("arangojs");
14
- const _ = require("lodash");
24
+ const _ = __importStar(require("lodash"));
15
25
  const common_1 = require("./common");
16
26
  /**
17
27
  * ArangoDB database provider.
@@ -33,91 +43,89 @@ class Arango {
33
43
  * @param {Object} options options.limit, options.offset
34
44
  * @return {Promise<any>} Promise for list of found documents.
35
45
  */
36
- find(collectionName, filter, options) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- if (_.isNil(collectionName) || !_.isString(collectionName) ||
39
- _.isEmpty(collectionName)) {
40
- throw new Error('invalid or missing collection argument for find operation');
41
- }
42
- let filterQuery = filter || {};
43
- const opts = options || {};
44
- let filterResult;
45
- let bindVars;
46
- let customFilter = '';
47
- // checking if a custom query should be used
48
- if (!_.isEmpty(opts.customQueries)) {
49
- for (let queryName of opts.customQueries) {
50
- if (!this.customQueries.has(queryName)) {
51
- throw new Error(`custom query ${common_1.query} not found`);
52
- }
53
- const customQuery = this.customQueries.get(queryName);
54
- if (customQuery.type == 'query') {
55
- // standalone query
56
- const result = yield common_1.query(this.db, collectionName, customQuery.code, opts.customArguments || {}); // Cursor object
57
- return result.all(); // TODO: paginate
58
- }
59
- else {
60
- // filter
61
- customFilter += ` ${customQuery.code} `;
62
- }
46
+ async find(collectionName, filter, options) {
47
+ if (_.isNil(collectionName) || !_.isString(collectionName) ||
48
+ _.isEmpty(collectionName)) {
49
+ throw new Error('invalid or missing collection argument for find operation');
50
+ }
51
+ let filterQuery = filter || {};
52
+ const opts = options || {};
53
+ let filterResult;
54
+ let bindVars;
55
+ let customFilter = '';
56
+ // checking if a custom query should be used
57
+ if (!_.isEmpty(opts.customQueries)) {
58
+ for (let queryName of opts.customQueries) {
59
+ if (!this.customQueries.has(queryName)) {
60
+ throw new Error(`custom query ${common_1.query} not found`);
61
+ }
62
+ const customQuery = this.customQueries.get(queryName);
63
+ if (customQuery.type == 'query') {
64
+ // standalone query
65
+ const result = await (0, common_1.query)(this.db, collectionName, customQuery.code, opts.customArguments || {}); // Cursor object
66
+ return result.all(); // TODO: paginate
67
+ }
68
+ else {
69
+ // filter
70
+ customFilter += ` ${customQuery.code} `;
63
71
  }
64
72
  }
65
- if (!_.isArray(filterQuery)) {
66
- filterQuery = [filterQuery];
67
- }
68
- if (_.isEmpty(filterQuery[0])) {
69
- filterQuery = true;
70
- }
71
- else {
72
- filterResult = common_1.buildFilter(filterQuery);
73
- filterQuery = filterResult.q;
74
- }
75
- let sortQuery = common_1.buildSorter(opts);
76
- let limitQuery = common_1.buildLimiter(opts);
77
- let returnResult = common_1.buildReturn(opts);
78
- let returnQuery = returnResult.q;
79
- // return complete node in case no specific fields are specified
80
- if (_.isEmpty(returnQuery)) {
81
- returnQuery = 'RETURN node';
82
- }
83
- let queryString = `FOR node in @@collection FILTER ${filterQuery}`;
84
- if (!_.isEmpty(customFilter)) {
85
- queryString += customFilter;
86
- }
87
- queryString += ` ${sortQuery}
73
+ }
74
+ if (!_.isArray(filterQuery)) {
75
+ filterQuery = [filterQuery];
76
+ }
77
+ if (_.isEmpty(filterQuery[0])) {
78
+ filterQuery = true;
79
+ }
80
+ else {
81
+ filterResult = (0, common_1.buildFilter)(filterQuery);
82
+ filterQuery = filterResult.q;
83
+ }
84
+ let sortQuery = (0, common_1.buildSorter)(opts);
85
+ let limitQuery = (0, common_1.buildLimiter)(opts);
86
+ let returnResult = (0, common_1.buildReturn)(opts);
87
+ let returnQuery = returnResult.q;
88
+ // return complete node in case no specific fields are specified
89
+ if (_.isEmpty(returnQuery)) {
90
+ returnQuery = 'RETURN node';
91
+ }
92
+ let queryString = `FOR node in @@collection FILTER ${filterQuery}`;
93
+ if (!_.isEmpty(customFilter)) {
94
+ queryString += customFilter;
95
+ }
96
+ queryString += ` ${sortQuery}
88
97
  ${limitQuery} ${returnQuery}`;
89
- let varArgs = {};
90
- if (filterResult && filterResult.bindVarsMap) {
91
- varArgs = filterResult.bindVarsMap;
92
- }
93
- let returnArgs = {};
94
- if (returnResult && returnResult.bindVarsMap) {
95
- returnArgs = returnResult.bindVarsMap;
96
- }
97
- let limitArgs;
98
- if (_.isEmpty(limitQuery)) {
99
- limitArgs = {};
100
- }
101
- else {
102
- if (!_.isNil(opts.limit)) {
103
- limitArgs = { limit: opts.limit };
104
- if (!_.isNil(opts.offset)) {
105
- limitArgs = { offset: opts.offset, limit: opts.limit };
106
- }
98
+ let varArgs = {};
99
+ if (filterResult && filterResult.bindVarsMap) {
100
+ varArgs = filterResult.bindVarsMap;
101
+ }
102
+ let returnArgs = {};
103
+ if (returnResult && returnResult.bindVarsMap) {
104
+ returnArgs = returnResult.bindVarsMap;
105
+ }
106
+ let limitArgs;
107
+ if (_.isEmpty(limitQuery)) {
108
+ limitArgs = {};
109
+ }
110
+ else {
111
+ if (!_.isNil(opts.limit)) {
112
+ limitArgs = { limit: opts.limit };
113
+ if (!_.isNil(opts.offset)) {
114
+ limitArgs = { offset: opts.offset, limit: opts.limit };
107
115
  }
108
116
  }
109
- varArgs = _.assign(varArgs, limitArgs);
110
- varArgs = _.assign(varArgs, returnArgs);
111
- bindVars = _.assign({
112
- '@collection': collectionName
113
- }, varArgs);
114
- if (!_.isEmpty(customFilter) && opts.customArguments) {
115
- bindVars = _.assign(bindVars, opts.customArguments);
116
- }
117
- const res = yield common_1.query(this.db, collectionName, queryString, bindVars);
118
- const docs = yield res.all(); // TODO: paginate
119
- return _.map(docs, common_1.sanitizeOutputFields);
120
- });
117
+ }
118
+ varArgs = _.assign(varArgs, limitArgs);
119
+ varArgs = _.assign(varArgs, returnArgs);
120
+ bindVars = _.assign({
121
+ '@collection': collectionName
122
+ }, varArgs);
123
+ if (!_.isEmpty(customFilter) && opts.customArguments) {
124
+ bindVars = _.assign(bindVars, opts.customArguments);
125
+ }
126
+ const res = await (0, common_1.query)(this.db, collectionName, queryString, bindVars);
127
+ const docs = await res.all(); // TODO: paginate
128
+ return _.map(docs, common_1.sanitizeOutputFields);
121
129
  }
122
130
  /**
123
131
  * Find documents by id (_key).
@@ -126,35 +134,33 @@ class Arango {
126
134
  * @param {String|array.String} ids A single ID or multiple IDs.
127
135
  * @return {Promise<any>} A list of found documents.
128
136
  */
129
- findByID(collectionName, ids) {
130
- return __awaiter(this, void 0, void 0, function* () {
131
- if (_.isNil(collectionName) || !_.isString(collectionName) ||
132
- _.isEmpty(collectionName)) {
133
- throw new Error('invalid or missing collection argument for findByID operation');
134
- }
135
- if (_.isNil(ids)) {
136
- throw new Error('invalid or missing ids argument for findByID operation');
137
- }
138
- if (!_.isArray(ids)) {
139
- ids = [ids];
140
- }
141
- const filter = ids.map((id) => {
142
- return { id };
143
- });
144
- let filterResult = common_1.buildFilter(filter);
145
- let filterQuery = filterResult.q;
146
- let varArgs = {};
147
- if (filterResult && filterResult.bindVarsMap) {
148
- varArgs = filterResult.bindVarsMap;
149
- }
150
- const queryString = `FOR node in @@collection FILTER ${filterQuery} RETURN node`;
151
- const bindVars = Object.assign({
152
- '@collection': collectionName
153
- }, varArgs);
154
- const res = yield common_1.query(this.db, collectionName, queryString, bindVars);
155
- const docs = yield res.all();
156
- return _.map(docs, common_1.sanitizeOutputFields);
137
+ async findByID(collectionName, ids) {
138
+ if (_.isNil(collectionName) || !_.isString(collectionName) ||
139
+ _.isEmpty(collectionName)) {
140
+ throw new Error('invalid or missing collection argument for findByID operation');
141
+ }
142
+ if (_.isNil(ids)) {
143
+ throw new Error('invalid or missing ids argument for findByID operation');
144
+ }
145
+ if (!_.isArray(ids)) {
146
+ ids = [ids];
147
+ }
148
+ const filter = ids.map((id) => {
149
+ return { id };
157
150
  });
151
+ let filterResult = (0, common_1.buildFilter)(filter);
152
+ let filterQuery = filterResult.q;
153
+ let varArgs = {};
154
+ if (filterResult && filterResult.bindVarsMap) {
155
+ varArgs = filterResult.bindVarsMap;
156
+ }
157
+ const queryString = `FOR node in @@collection FILTER ${filterQuery} RETURN node`;
158
+ const bindVars = Object.assign({
159
+ '@collection': collectionName
160
+ }, varArgs);
161
+ const res = await (0, common_1.query)(this.db, collectionName, queryString, bindVars);
162
+ const docs = await res.all();
163
+ return _.map(docs, common_1.sanitizeOutputFields);
158
164
  }
159
165
  /**
160
166
  * retreive the documents including the document handlers (_key, _id and _rev).
@@ -165,26 +171,24 @@ class Arango {
165
171
  * @param {string[]} idsArray list of document ids
166
172
  * @returns {Promise<any>} A list of documents including the document handlers
167
173
  */
168
- getDocumentHandlers(collectionName, collection, documents, idsArray) {
169
- return __awaiter(this, void 0, void 0, function* () {
170
- let ids = [];
171
- if (documents && !_.isArray(documents)) {
172
- documents = [documents];
173
- }
174
- if (documents && documents.length > 0) {
175
- for (let doc of documents) {
176
- ids.push(doc.id);
177
- }
178
- }
179
- if (!_.isEmpty(idsArray) && _.isArray(idsArray)) {
180
- ids = idsArray;
174
+ async getDocumentHandlers(collectionName, collection, documents, idsArray) {
175
+ let ids = [];
176
+ if (documents && !_.isArray(documents)) {
177
+ documents = [documents];
178
+ }
179
+ if (documents && documents.length > 0) {
180
+ for (let doc of documents) {
181
+ ids.push(doc.id);
181
182
  }
182
- let queryString = arangojs_1.aql `FOR node in ${collection}
183
+ }
184
+ if (!_.isEmpty(idsArray) && _.isArray(idsArray)) {
185
+ ids = idsArray;
186
+ }
187
+ let queryString = (0, arangojs_1.aql) `FOR node in ${collection}
183
188
  FILTER node.id IN ${ids} return node`;
184
- const res = yield common_1.query(this.db, collectionName, queryString);
185
- const docsWithSelector = yield res.all();
186
- return docsWithSelector;
187
- });
189
+ const res = await (0, common_1.query)(this.db, collectionName, queryString);
190
+ const docsWithSelector = await res.all();
191
+ return docsWithSelector;
188
192
  }
189
193
  /**
190
194
  * Find documents by filter and updates them with document.
@@ -192,53 +196,51 @@ class Arango {
192
196
  * @param {String} collection Collection name
193
197
  * @param {Object} updateDocuments List of documents to update
194
198
  */
195
- update(collectionName, updateDocuments) {
196
- return __awaiter(this, void 0, void 0, function* () {
197
- let documents = _.cloneDeep(updateDocuments);
198
- let updateDocsResponse = [];
199
- if (_.isNil(collectionName) ||
200
- !_.isString(collectionName) || _.isEmpty(collectionName)) {
201
- throw new Error('invalid or missing collection argument for update operation');
202
- }
203
- if (_.isNil(documents)) {
204
- throw new Error('invalid or missing document argument for update operation');
205
- }
206
- const collection = this.db.collection(collectionName);
207
- const collectionExists = yield collection.exists();
208
- if (!collectionExists) {
209
- throw new Error(`Collection ${collectionName} does not exist for update operation`);
199
+ async update(collectionName, updateDocuments) {
200
+ let documents = _.cloneDeep(updateDocuments);
201
+ let updateDocsResponse = [];
202
+ if (_.isNil(collectionName) ||
203
+ !_.isString(collectionName) || _.isEmpty(collectionName)) {
204
+ throw new Error('invalid or missing collection argument for update operation');
205
+ }
206
+ if (_.isNil(documents)) {
207
+ throw new Error('invalid or missing document argument for update operation');
208
+ }
209
+ const collection = this.db.collection(collectionName);
210
+ const collectionExists = await collection.exists();
211
+ if (!collectionExists) {
212
+ throw new Error(`Collection ${collectionName} does not exist for update operation`);
213
+ }
214
+ if (!_.isArray(documents)) {
215
+ throw new Error(`Documents should be list for update operation`);
216
+ }
217
+ const docsWithHandlers = await this.getDocumentHandlers(collectionName, collection, documents);
218
+ // update _key for the input documents
219
+ for (let document of documents) {
220
+ let foundInDB = false;
221
+ for (let docWithHandler of docsWithHandlers) {
222
+ if (docWithHandler.id === document.id) {
223
+ foundInDB = true;
224
+ document._key = docWithHandler._key;
225
+ break;
226
+ }
210
227
  }
211
- if (!_.isArray(documents)) {
212
- throw new Error(`Documents should be list for update operation`);
228
+ if (!foundInDB) {
229
+ // if document is not found in DB use the id itself as _key
230
+ // this key will return an array in response since it does not exist
231
+ document._key = document.id;
213
232
  }
214
- const docsWithHandlers = yield this.getDocumentHandlers(collectionName, collection, documents);
215
- // update _key for the input documents
216
- for (let document of documents) {
217
- let foundInDB = false;
218
- for (let docWithHandler of docsWithHandlers) {
219
- if (docWithHandler.id === document.id) {
220
- foundInDB = true;
221
- document._key = docWithHandler._key;
222
- break;
223
- }
224
- }
225
- if (!foundInDB) {
226
- // if document is not found in DB use the id itself as _key
227
- // this key will return an array in response since it does not exist
228
- document._key = document.id;
229
- }
233
+ }
234
+ let updatedDocs = await collection.updateAll(documents, { returnNew: true });
235
+ for (let doc of updatedDocs) {
236
+ if (doc && doc.new) {
237
+ updateDocsResponse.push((0, common_1.sanitizeOutputFields)(doc.new));
230
238
  }
231
- let updatedDocs = yield collection.updateAll(documents, { returnNew: true });
232
- for (let doc of updatedDocs) {
233
- if (doc && doc.new) {
234
- updateDocsResponse.push(common_1.sanitizeOutputFields(doc.new));
235
- }
236
- else {
237
- updateDocsResponse.push(doc);
238
- }
239
+ else {
240
+ updateDocsResponse.push(doc);
239
241
  }
240
- return updateDocsResponse;
241
- });
242
+ }
243
+ return updateDocsResponse;
242
244
  }
243
245
  /**
244
246
  * Find each document based on it's key and update it.
@@ -247,42 +249,40 @@ class Arango {
247
249
  * @param {String} collectionName Collection name
248
250
  * @param {Object|Array.Object} documents
249
251
  */
250
- upsert(collectionName, documents) {
251
- return __awaiter(this, void 0, void 0, function* () {
252
- if (_.isNil(collectionName) ||
253
- !_.isString(collectionName) || _.isEmpty(collectionName)) {
254
- throw new Error('invalid or missing collection argument for upsert operation');
255
- }
256
- if (_.isNil(documents)) {
257
- throw new Error('invalid or missing documents argument for upsert operation');
258
- }
259
- let docs = _.cloneDeep(documents);
260
- if (!_.isArray(documents)) {
261
- docs = [documents];
262
- }
263
- _.forEach(docs, (document, i) => {
264
- docs[i] = common_1.sanitizeInputFields(document);
265
- });
266
- let upsertResponse = [];
267
- const collection = this.db.collection(collectionName);
268
- const collectionExists = yield collection.exists();
269
- if (!collectionExists) {
270
- throw new Error(`Collection ${collectionName} does not exist for upsert operation`);
271
- }
272
- let upsertedDocs = yield collection.saveAll(docs, { returnNew: true, overwrite: true });
273
- if (!_.isArray(upsertedDocs)) {
274
- upsertedDocs = [upsertedDocs];
252
+ async upsert(collectionName, documents) {
253
+ if (_.isNil(collectionName) ||
254
+ !_.isString(collectionName) || _.isEmpty(collectionName)) {
255
+ throw new Error('invalid or missing collection argument for upsert operation');
256
+ }
257
+ if (_.isNil(documents)) {
258
+ throw new Error('invalid or missing documents argument for upsert operation');
259
+ }
260
+ let docs = _.cloneDeep(documents);
261
+ if (!_.isArray(documents)) {
262
+ docs = [documents];
263
+ }
264
+ _.forEach(docs, (document, i) => {
265
+ docs[i] = (0, common_1.sanitizeInputFields)(document);
266
+ });
267
+ let upsertResponse = [];
268
+ const collection = this.db.collection(collectionName);
269
+ const collectionExists = await collection.exists();
270
+ if (!collectionExists) {
271
+ throw new Error(`Collection ${collectionName} does not exist for upsert operation`);
272
+ }
273
+ let upsertedDocs = await collection.saveAll(docs, { returnNew: true, overwrite: true });
274
+ if (!_.isArray(upsertedDocs)) {
275
+ upsertedDocs = [upsertedDocs];
276
+ }
277
+ for (let doc of upsertedDocs) {
278
+ if (doc && doc.new) {
279
+ upsertResponse.push((0, common_1.sanitizeOutputFields)(doc.new));
275
280
  }
276
- for (let doc of upsertedDocs) {
277
- if (doc && doc.new) {
278
- upsertResponse.push(common_1.sanitizeOutputFields(doc.new));
279
- }
280
- else {
281
- upsertResponse.push(doc);
282
- }
281
+ else {
282
+ upsertResponse.push(doc);
283
283
  }
284
- return upsertResponse;
285
- });
284
+ }
285
+ return upsertResponse;
286
286
  }
287
287
  /**
288
288
  * Delete all documents with provided identifiers ids.
@@ -291,43 +291,41 @@ class Arango {
291
291
  * @param {Object} ids list of document identifiers
292
292
  * @return {Promise<any>} delete response
293
293
  */
294
- delete(collectionName, ids) {
295
- return __awaiter(this, void 0, void 0, function* () {
296
- if (_.isNil(collectionName) ||
297
- !_.isString(collectionName) || _.isEmpty(collectionName)) {
298
- throw new Error('invalid or missing collection argument');
299
- }
300
- if (_.isNil(ids) || _.isEmpty(ids)) {
301
- throw new Error('invalid or missing document IDs argument for delete operation');
302
- }
303
- const collection = this.db.collection(collectionName);
304
- const collectionExists = yield collection.exists();
305
- if (!collectionExists) {
306
- throw new Error(`Collection ${collectionName} does not exist for delete operation`);
307
- }
308
- // retreive _key for the give ids
309
- const docsWithHandlers = yield this.getDocumentHandlers(collectionName, collection, null, ids);
310
- for (let id of ids) {
311
- // check if given id is present in docsWithHandlers
312
- let foundDocInDB = false;
313
- for (let doc of docsWithHandlers) {
314
- if (doc.id === id) {
315
- foundDocInDB = true;
316
- break;
317
- }
318
- }
319
- // if document is not found in DB use the id itself as _key
320
- // this key will return an array in response since it does not exist
321
- if (!foundDocInDB) {
322
- docsWithHandlers.push({ _key: id });
294
+ async delete(collectionName, ids) {
295
+ if (_.isNil(collectionName) ||
296
+ !_.isString(collectionName) || _.isEmpty(collectionName)) {
297
+ throw new Error('invalid or missing collection argument');
298
+ }
299
+ if (_.isNil(ids) || _.isEmpty(ids)) {
300
+ throw new Error('invalid or missing document IDs argument for delete operation');
301
+ }
302
+ const collection = this.db.collection(collectionName);
303
+ const collectionExists = await collection.exists();
304
+ if (!collectionExists) {
305
+ throw new Error(`Collection ${collectionName} does not exist for delete operation`);
306
+ }
307
+ // retreive _key for the give ids
308
+ const docsWithHandlers = await this.getDocumentHandlers(collectionName, collection, null, ids);
309
+ for (let id of ids) {
310
+ // check if given id is present in docsWithHandlers
311
+ let foundDocInDB = false;
312
+ for (let doc of docsWithHandlers) {
313
+ if (doc.id === id) {
314
+ foundDocInDB = true;
315
+ break;
323
316
  }
324
317
  }
325
- let deleteHandlerIds = [];
326
- for (let doc of docsWithHandlers) {
327
- deleteHandlerIds.push(doc._key);
318
+ // if document is not found in DB use the id itself as _key
319
+ // this key will return an array in response since it does not exist
320
+ if (!foundDocInDB) {
321
+ docsWithHandlers.push({ _key: id });
328
322
  }
329
- return collection.removeAll(deleteHandlerIds);
330
- });
323
+ }
324
+ let deleteHandlerIds = [];
325
+ for (let doc of docsWithHandlers) {
326
+ deleteHandlerIds.push(doc._key);
327
+ }
328
+ return collection.removeAll(deleteHandlerIds);
331
329
  }
332
330
  /**
333
331
  * Count all documents selected by filter.
@@ -335,37 +333,35 @@ class Arango {
335
333
  * @param {String} collection Collection name
336
334
  * @param {Object} filter
337
335
  */
338
- count(collectionName, filter) {
339
- return __awaiter(this, void 0, void 0, function* () {
340
- if (_.isNil(collectionName) ||
341
- !_.isString(collectionName) || _.isEmpty(collectionName)) {
342
- throw new Error('invalid or missing collection argument for count operation');
343
- }
344
- let filterQuery = filter || {};
345
- let filterResult;
346
- if (!_.isArray(filterQuery)) {
347
- filterQuery = [filterQuery];
348
- }
349
- if (_.isEmpty(filterQuery[0])) {
350
- filterQuery = true;
351
- }
352
- else {
353
- filterResult = common_1.buildFilter(filterQuery);
354
- filterQuery = filterResult.q;
355
- }
356
- let varArgs = {};
357
- if (filterResult && filterResult.bindVarsMap) {
358
- varArgs = filterResult.bindVarsMap;
359
- }
360
- let queryString = `FOR node in @@collection FILTER ${filterQuery} COLLECT WITH COUNT
336
+ async count(collectionName, filter) {
337
+ if (_.isNil(collectionName) ||
338
+ !_.isString(collectionName) || _.isEmpty(collectionName)) {
339
+ throw new Error('invalid or missing collection argument for count operation');
340
+ }
341
+ let filterQuery = filter || {};
342
+ let filterResult;
343
+ if (!_.isArray(filterQuery)) {
344
+ filterQuery = [filterQuery];
345
+ }
346
+ if (_.isEmpty(filterQuery[0])) {
347
+ filterQuery = true;
348
+ }
349
+ else {
350
+ filterResult = (0, common_1.buildFilter)(filterQuery);
351
+ filterQuery = filterResult.q;
352
+ }
353
+ let varArgs = {};
354
+ if (filterResult && filterResult.bindVarsMap) {
355
+ varArgs = filterResult.bindVarsMap;
356
+ }
357
+ let queryString = `FOR node in @@collection FILTER ${filterQuery} COLLECT WITH COUNT
361
358
  INTO length RETURN length`;
362
- const bindVars = Object.assign({
363
- '@collection': collectionName
364
- }, varArgs);
365
- const res = yield common_1.query(this.db, collectionName, queryString, bindVars);
366
- const nn = yield res.all();
367
- return nn[0];
368
- });
359
+ const bindVars = Object.assign({
360
+ '@collection': collectionName
361
+ }, varArgs);
362
+ const res = await (0, common_1.query)(this.db, collectionName, queryString, bindVars);
363
+ const nn = await res.all();
364
+ return nn[0];
369
365
  }
370
366
  /**
371
367
  * When calling without a collection name,
@@ -374,20 +370,18 @@ class Arango {
374
370
  * delete all documents in specified collection in the database.
375
371
  * @param [string] collection Collection name.
376
372
  */
377
- truncate(collection) {
378
- return __awaiter(this, void 0, void 0, function* () {
379
- if (_.isNil(collection)) {
380
- const collections = yield this.db.collections();
381
- for (let i = 0; i < collections.length; i += 1) {
382
- const c = this.db.collection(collections[i].name);
383
- yield c.truncate();
384
- }
385
- }
386
- else {
387
- const c = this.db.collection(collection);
388
- yield c.truncate();
373
+ async truncate(collection) {
374
+ if (_.isNil(collection)) {
375
+ const collections = await this.db.collections();
376
+ for (let i = 0; i < collections.length; i += 1) {
377
+ const c = this.db.collection(collections[i].name);
378
+ await c.truncate();
389
379
  }
390
- });
380
+ }
381
+ else {
382
+ const c = this.db.collection(collection);
383
+ await c.truncate();
384
+ }
391
385
  }
392
386
  /**
393
387
  * Insert documents into database.
@@ -395,41 +389,39 @@ class Arango {
395
389
  * @param {String} collection Collection name
396
390
  * @param {Object|array.Object} documents A single or multiple documents.
397
391
  */
398
- insert(collectionName, documents) {
399
- return __awaiter(this, void 0, void 0, function* () {
400
- if (_.isNil(collectionName) || !_.isString(collectionName) || _.isEmpty(collectionName)) {
401
- throw new Error('invalid or missing collection argument for insert operation');
402
- }
403
- if (_.isNil(documents)) {
404
- throw new Error('invalid or missing documents argument for insert operation');
405
- }
406
- let docs = _.cloneDeep(documents);
407
- if (!_.isArray(documents)) {
408
- docs = [documents];
409
- }
410
- _.forEach(docs, (document, i) => {
411
- docs[i] = common_1.sanitizeInputFields(document);
412
- });
413
- const collection = this.db.collection(collectionName);
414
- const collectionExists = yield collection.exists();
415
- if (!collectionExists) {
416
- yield collection.create();
417
- }
418
- let insertResponse = [];
419
- let createdDocs = yield collection.saveAll(docs, { returnNew: true });
420
- if (!_.isArray(createdDocs)) {
421
- createdDocs = [createdDocs];
392
+ async insert(collectionName, documents) {
393
+ if (_.isNil(collectionName) || !_.isString(collectionName) || _.isEmpty(collectionName)) {
394
+ throw new Error('invalid or missing collection argument for insert operation');
395
+ }
396
+ if (_.isNil(documents)) {
397
+ throw new Error('invalid or missing documents argument for insert operation');
398
+ }
399
+ let docs = _.cloneDeep(documents);
400
+ if (!_.isArray(documents)) {
401
+ docs = [documents];
402
+ }
403
+ _.forEach(docs, (document, i) => {
404
+ docs[i] = (0, common_1.sanitizeInputFields)(document);
405
+ });
406
+ const collection = this.db.collection(collectionName);
407
+ const collectionExists = await collection.exists();
408
+ if (!collectionExists) {
409
+ await collection.create();
410
+ }
411
+ let insertResponse = [];
412
+ let createdDocs = await collection.saveAll(docs, { returnNew: true });
413
+ if (!_.isArray(createdDocs)) {
414
+ createdDocs = [createdDocs];
415
+ }
416
+ for (let doc of createdDocs) {
417
+ if (doc && doc.new) {
418
+ insertResponse.push((0, common_1.sanitizeOutputFields)(doc.new));
422
419
  }
423
- for (let doc of createdDocs) {
424
- if (doc && doc.new) {
425
- insertResponse.push(common_1.sanitizeOutputFields(doc.new));
426
- }
427
- else {
428
- insertResponse.push(doc);
429
- }
420
+ else {
421
+ insertResponse.push(doc);
430
422
  }
431
- return insertResponse;
432
- });
423
+ }
424
+ return insertResponse;
433
425
  }
434
426
  /**
435
427
  * Registers a custom AQL query.