@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.
- package/CHANGELOG.md +19 -0
- package/lib/cache/index.js +22 -3
- package/lib/cache/index.js.map +1 -1
- package/lib/command-interface/index.d.ts +3 -3
- package/lib/command-interface/index.js +432 -498
- package/lib/command-interface/index.js.map +1 -1
- package/lib/config/index.js +6 -15
- package/lib/config/index.js.map +1 -1
- package/lib/database/index.d.ts +2 -4
- package/lib/database/index.js +4 -13
- package/lib/database/index.js.map +1 -1
- package/lib/database/provider/arango/base.js +314 -322
- package/lib/database/provider/arango/base.js.map +1 -1
- package/lib/database/provider/arango/common.js +70 -57
- package/lib/database/provider/arango/common.js.map +1 -1
- package/lib/database/provider/arango/graph.d.ts +4 -8
- package/lib/database/provider/arango/graph.js +335 -470
- package/lib/database/provider/arango/graph.js.map +1 -1
- package/lib/database/provider/arango/index.js +34 -21
- package/lib/database/provider/arango/index.js.map +1 -1
- package/lib/database/provider/arango/interface.d.ts +70 -0
- package/lib/database/provider/arango/interface.js +46 -0
- package/lib/database/provider/arango/interface.js.map +1 -0
- package/lib/database/provider/arango/utils.d.ts +77 -0
- package/lib/database/provider/arango/utils.js +587 -0
- package/lib/database/provider/arango/utils.js.map +1 -0
- package/lib/database/provider/nedb/index.js +203 -206
- package/lib/database/provider/nedb/index.js.map +1 -1
- package/lib/health/index.js +36 -42
- package/lib/health/index.js.map +1 -1
- package/lib/index.d.ts +4 -0
- package/lib/index.js +27 -6
- package/lib/index.js.map +1 -1
- package/lib/microservice/endpoint.js +33 -23
- package/lib/microservice/endpoint.js.map +1 -1
- package/lib/microservice/server.js +115 -106
- package/lib/microservice/server.js.map +1 -1
- package/lib/microservice/transport/provider/grpc/index.js +58 -52
- package/lib/microservice/transport/provider/grpc/index.js.map +1 -1
- package/lib/microservice/transport/provider/grpc/reflection.js +101 -93
- package/lib/microservice/transport/provider/grpc/reflection.js.map +1 -1
- package/lib/offsets/index.d.ts +2 -2
- package/lib/offsets/index.js +41 -42
- package/lib/offsets/index.js.map +1 -1
- package/package.json +29 -29
- package/tsconfig.json +13 -6
- package/setupTopics.js +0 -32
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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 (!
|
|
212
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
232
|
-
|
|
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
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
277
|
-
|
|
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
|
-
|
|
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
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
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
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
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
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
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
|
-
|
|
424
|
-
|
|
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
|
-
|
|
432
|
-
|
|
423
|
+
}
|
|
424
|
+
return insertResponse;
|
|
433
425
|
}
|
|
434
426
|
/**
|
|
435
427
|
* Registers a custom AQL query.
|