@opra/mongodb 1.4.3 → 1.4.4

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.
@@ -18,7 +18,8 @@ var MongoAdapter;
18
18
  }
19
19
  const ctx = context;
20
20
  const { operation } = ctx;
21
- if (operation?.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
21
+ if (operation?.composition?.startsWith('Entity.') &&
22
+ operation.compositionOptions?.type) {
22
23
  const controller = operation.owner;
23
24
  switch (operation.composition) {
24
25
  case 'Entity.Create': {
@@ -26,15 +27,24 @@ var MongoAdapter;
26
27
  const options = {
27
28
  projection: ctx.queryParams.projection,
28
29
  };
29
- return { method: 'create', data, options };
30
+ return {
31
+ method: 'create',
32
+ data,
33
+ options,
34
+ };
30
35
  }
31
36
  case 'Entity.Delete': {
32
- const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
37
+ const keyParam = operation.parameters.find(p => p.keyParam) ||
38
+ controller.parameters.find(p => p.keyParam);
33
39
  const key = keyParam && ctx.pathParams[String(keyParam.name)];
34
40
  const options = {
35
41
  filter: ctx.queryParams.filter,
36
42
  };
37
- return { method: 'delete', key, options };
43
+ return {
44
+ method: 'delete',
45
+ key,
46
+ options,
47
+ };
38
48
  }
39
49
  case 'Entity.DeleteMany': {
40
50
  const options = {
@@ -45,16 +55,19 @@ var MongoAdapter;
45
55
  case 'Entity.FindMany': {
46
56
  const options = {
47
57
  filter: ctx.queryParams.filter,
48
- projection: ctx.queryParams.projection || operation.compositionOptions.defaultProjection,
58
+ projection: ctx.queryParams.projection ||
59
+ operation.compositionOptions.defaultProjection,
49
60
  count: ctx.queryParams.count,
50
- limit: ctx.queryParams.limit || operation.compositionOptions.defaultLimit,
61
+ limit: ctx.queryParams.limit ||
62
+ operation.compositionOptions.defaultLimit,
51
63
  skip: ctx.queryParams.skip,
52
64
  sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
53
65
  };
54
66
  return { method: 'findMany', options };
55
67
  }
56
68
  case 'Entity.Get': {
57
- const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
69
+ const keyParam = operation.parameters.find(p => p.keyParam) ||
70
+ controller.parameters.find(p => p.keyParam);
58
71
  const key = keyParam && ctx.pathParams[String(keyParam.name)];
59
72
  const options = {
60
73
  projection: ctx.queryParams.projection,
@@ -64,30 +77,46 @@ var MongoAdapter;
64
77
  }
65
78
  case 'Entity.Replace': {
66
79
  const data = await ctx.getBody();
67
- const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
80
+ const keyParam = operation.parameters.find(p => p.keyParam) ||
81
+ controller.parameters.find(p => p.keyParam);
68
82
  const key = keyParam && ctx.pathParams[String(keyParam.name)];
69
83
  const options = {
70
84
  projection: ctx.queryParams.projection,
71
85
  filter: ctx.queryParams.filter,
72
86
  };
73
- return { method: 'replace', key, data, options };
87
+ return {
88
+ method: 'replace',
89
+ key,
90
+ data,
91
+ options,
92
+ };
74
93
  }
75
94
  case 'Entity.Update': {
76
95
  const data = await ctx.getBody();
77
- const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
96
+ const keyParam = operation.parameters.find(p => p.keyParam) ||
97
+ controller.parameters.find(p => p.keyParam);
78
98
  const key = keyParam && ctx.pathParams[String(keyParam.name)];
79
99
  const options = {
80
100
  projection: ctx.queryParams.projection,
81
101
  filter: ctx.queryParams.filter,
82
102
  };
83
- return { method: 'update', key, data, options };
103
+ return {
104
+ method: 'update',
105
+ key,
106
+ data,
107
+ options,
108
+ };
84
109
  }
85
110
  case 'Entity.UpdateMany': {
86
111
  const data = await ctx.getBody();
87
112
  const options = {
88
113
  filter: ctx.queryParams.filter,
89
114
  };
90
- return { method: 'updateMany', data, options };
115
+ return {
116
+ method: 'updateMany',
117
+ data,
118
+ options,
119
+ };
91
120
  }
92
121
  default:
93
122
  break;
@@ -88,7 +88,9 @@ class MongoPatchGenerator {
88
88
  delete v[keyField];
89
89
  /** Add array filter */
90
90
  ctx.arrayFilters = ctx.arrayFilters || [];
91
- ctx.arrayFilters.push({ [`${arrayFilterName}.${keyField}`]: keyValue });
91
+ ctx.arrayFilters.push({
92
+ [`${arrayFilterName}.${keyField}`]: keyValue,
93
+ });
92
94
  /** Process each object in array */
93
95
  this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v);
94
96
  }
@@ -138,7 +140,9 @@ class MongoPatchGenerator {
138
140
  }
139
141
  continue;
140
142
  }
141
- ctx.$push[pathDot + key] = Array.isArray(value) ? { $each: value } : value;
143
+ ctx.$push[pathDot + key] = Array.isArray(value)
144
+ ? { $each: value }
145
+ : value;
142
146
  }
143
147
  }
144
148
  _processPull(ctx, dataType, path, input) {
@@ -158,10 +162,16 @@ class MongoPatchGenerator {
158
162
  keyField = field.keyField || field.type.keyField;
159
163
  if (!keyField)
160
164
  continue;
161
- ctx.$pull[pathDot + key] = { $elemMatch: { [keyField]: Array.isArray(value) ? { $in: value } : value } };
165
+ ctx.$pull[pathDot + key] = {
166
+ $elemMatch: {
167
+ [keyField]: Array.isArray(value) ? { $in: value } : value,
168
+ },
169
+ };
162
170
  }
163
171
  else {
164
- ctx.$pull[pathDot + key] = Array.isArray(value) ? { $in: value } : value;
172
+ ctx.$pull[pathDot + key] = Array.isArray(value)
173
+ ? { $in: value }
174
+ : value;
165
175
  }
166
176
  }
167
177
  }
@@ -64,7 +64,11 @@ function prepareFilter(filters, options) {
64
64
  }
65
65
  i++;
66
66
  }
67
- return i ? (options?.fieldPrefix ? addPrefix(out, options.fieldPrefix) : out) : undefined;
67
+ return i
68
+ ? options?.fieldPrefix
69
+ ? addPrefix(out, options.fieldPrefix)
70
+ : out
71
+ : undefined;
68
72
  }
69
73
  function addPrefix(source, prefix) {
70
74
  if (typeof source !== 'object')
@@ -99,7 +103,9 @@ function prepareFilterAst(ast, negative) {
99
103
  return prepareFilterAst(ast.expression, !negative);
100
104
  }
101
105
  if (ast instanceof common_1.OpraFilter.LogicalExpression) {
102
- const items = ast.items.map(x => prepareFilterAst(x, negative)).filter(x => x != null);
106
+ const items = ast.items
107
+ .map(x => prepareFilterAst(x, negative))
108
+ .filter(x => x != null);
103
109
  if (ast.op === 'or')
104
110
  return { $or: items };
105
111
  return { $and: items };
@@ -122,7 +128,9 @@ function prepareFilterAst(ast, negative) {
122
128
  if (op === '=')
123
129
  return { $or: [{ [left]: null }, { [left]: { $exists: false } }] };
124
130
  if (op === '!=')
125
- return { $and: [{ $ne: { [left]: null } }, { [left]: { $exists: true } }] };
131
+ return {
132
+ $and: [{ $ne: { [left]: null } }, { [left]: { $exists: true } }],
133
+ };
126
134
  }
127
135
  const mngOp = opMap[ast.op];
128
136
  if (mngOp) {
@@ -6,10 +6,14 @@ const common_1 = require("@opra/common");
6
6
  function prepareProjection(dataType, projection) {
7
7
  if (projection === '*')
8
8
  return undefined;
9
- if (projection && typeof projection === 'object' && !Array.isArray(projection))
9
+ if (projection &&
10
+ typeof projection === 'object' &&
11
+ !Array.isArray(projection))
10
12
  return projection;
11
13
  const out = {};
12
- const projection_ = typeof projection === 'string' || Array.isArray(projection) ? (0, common_1.parseFieldsProjection)(projection) : projection;
14
+ const projection_ = typeof projection === 'string' || Array.isArray(projection)
15
+ ? (0, common_1.parseFieldsProjection)(projection)
16
+ : projection;
13
17
  // const exclusionProjection = !pick && !!omit;
14
18
  prepare(dataType, out, projection_);
15
19
  return Object.keys(out).length ? out : undefined;
@@ -36,7 +40,8 @@ function prepare(dataType, target, projection) {
36
40
  (defaultFields && field.exclusive && !p)) {
37
41
  continue;
38
42
  }
39
- if (field.type instanceof common_1.ComplexType && typeof p?.projection === 'object') {
43
+ if (field.type instanceof common_1.ComplexType &&
44
+ typeof p?.projection === 'object') {
40
45
  target[fieldName] = {};
41
46
  prepare(field.type, target[fieldName], p.projection);
42
47
  continue;
@@ -41,7 +41,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
41
41
  input,
42
42
  options,
43
43
  };
44
- input._id = input._id == null || input._id === '' ? this._generateId(command) : input._id;
44
+ input._id =
45
+ input._id == null || input._id === ''
46
+ ? this._generateId(command)
47
+ : input._id;
45
48
  return this._executeCommand(command, async () => {
46
49
  const r = await this._create(command);
47
50
  const findCommand = {
@@ -72,7 +75,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
72
75
  return this._executeCommand(command, async () => {
73
76
  const documentFilter = await this._getDocumentFilter(command);
74
77
  if (documentFilter) {
75
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
78
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
79
+ documentFilter,
80
+ command.options?.filter,
81
+ ]);
76
82
  command.options = { ...command.options, filter };
77
83
  }
78
84
  return this._count(command);
@@ -96,7 +102,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
96
102
  return this._executeCommand(command, async () => {
97
103
  const documentFilter = await this._getDocumentFilter(command);
98
104
  if (documentFilter) {
99
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
105
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
106
+ documentFilter,
107
+ command.options?.filter,
108
+ ]);
100
109
  command.options = { ...command.options, filter };
101
110
  }
102
111
  return this._delete(command);
@@ -118,7 +127,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
118
127
  return this._executeCommand(command, async () => {
119
128
  const documentFilter = await this._getDocumentFilter(command);
120
129
  if (documentFilter) {
121
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
130
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
131
+ documentFilter,
132
+ command.options?.filter,
133
+ ]);
122
134
  command.options = { ...command.options, filter };
123
135
  }
124
136
  return this._deleteMany(command);
@@ -141,7 +153,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
141
153
  return this._executeCommand(command, async () => {
142
154
  const documentFilter = await this._getDocumentFilter(command);
143
155
  if (documentFilter) {
144
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
156
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
157
+ documentFilter,
158
+ command.options?.filter,
159
+ ]);
145
160
  command.options = { ...command.options, filter };
146
161
  }
147
162
  return this._distinct(command);
@@ -166,8 +181,15 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
166
181
  const findCommand = command;
167
182
  const documentFilter = await this._getDocumentFilter(command);
168
183
  if (documentFilter) {
169
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
170
- findCommand.options = { ...command.options, filter, projection: ['_id'] };
184
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
185
+ documentFilter,
186
+ command.options?.filter,
187
+ ]);
188
+ findCommand.options = {
189
+ ...command.options,
190
+ filter,
191
+ projection: ['_id'],
192
+ };
171
193
  }
172
194
  return !!(await this._findById(findCommand));
173
195
  });
@@ -192,7 +214,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
192
214
  return this._executeCommand(command, async () => {
193
215
  const documentFilter = await this._getDocumentFilter(command);
194
216
  if (documentFilter) {
195
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
217
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
218
+ documentFilter,
219
+ command.options?.filter,
220
+ ]);
196
221
  command.options = { ...command.options, filter };
197
222
  }
198
223
  return this._findById(command);
@@ -208,7 +233,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
208
233
  return this._executeCommand(command, async () => {
209
234
  const documentFilter = await this._getDocumentFilter(command);
210
235
  if (documentFilter) {
211
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
236
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
237
+ documentFilter,
238
+ command.options?.filter,
239
+ ]);
212
240
  command.options = { ...command.options, filter };
213
241
  }
214
242
  return this._findOne(command);
@@ -225,7 +253,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
225
253
  const documentFilter = await this._getDocumentFilter(command);
226
254
  command.options = command.options || {};
227
255
  if (documentFilter) {
228
- command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
256
+ command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
257
+ documentFilter,
258
+ command.options?.filter,
259
+ ]);
229
260
  }
230
261
  const limit = command.options?.limit || this.defaultLimit;
231
262
  if (limit)
@@ -244,7 +275,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
244
275
  const documentFilter = await this._getDocumentFilter(command);
245
276
  command.options = command.options || {};
246
277
  if (documentFilter) {
247
- command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
278
+ command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
279
+ documentFilter,
280
+ command.options?.filter,
281
+ ]);
248
282
  }
249
283
  const limit = command.options?.limit || this.defaultLimit;
250
284
  if (limit)
@@ -271,7 +305,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
271
305
  return this._executeCommand(command, async () => {
272
306
  const documentFilter = await this._getDocumentFilter(command);
273
307
  if (documentFilter) {
274
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
308
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
309
+ documentFilter,
310
+ command.options?.filter,
311
+ ]);
275
312
  command.options = { ...command.options, filter };
276
313
  }
277
314
  return await this._replace(command);
@@ -291,7 +328,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
291
328
  return this._executeCommand(command, async () => {
292
329
  const documentFilter = await this._getDocumentFilter(command);
293
330
  if (documentFilter) {
294
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
331
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
332
+ documentFilter,
333
+ command.options?.filter,
334
+ ]);
295
335
  command.options = { ...command.options, filter };
296
336
  }
297
337
  return this._update(command);
@@ -319,7 +359,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
319
359
  return this._executeCommand(command, async () => {
320
360
  const documentFilter = await this._getDocumentFilter(command);
321
361
  if (documentFilter) {
322
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
362
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
363
+ documentFilter,
364
+ command.options?.filter,
365
+ ]);
323
366
  command.options = { ...command.options, filter };
324
367
  }
325
368
  return this._updateOnly(command);
@@ -345,7 +388,10 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
345
388
  return this._executeCommand(command, async () => {
346
389
  const documentFilter = await this._getDocumentFilter(command);
347
390
  if (documentFilter) {
348
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
391
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
392
+ documentFilter,
393
+ command.options?.filter,
394
+ ]);
349
395
  command.options = { ...command.options, filter };
350
396
  }
351
397
  return this._updateMany(command);
@@ -464,52 +464,76 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
464
464
  throw e;
465
465
  }
466
466
  }
467
+ async _beforeCreate(
467
468
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
468
- async _beforeCreate(command) {
469
+ command) {
469
470
  // Do nothing
470
471
  }
472
+ async _beforeDelete(
471
473
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
472
- async _beforeDelete(command) {
474
+ command) {
473
475
  // Do nothing
474
476
  }
477
+ async _beforeDeleteMany(
475
478
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
476
- async _beforeDeleteMany(command) {
479
+ command) {
477
480
  // Do nothing
478
481
  }
482
+ async _beforeReplace(
479
483
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
480
- async _beforeReplace(command) {
484
+ command) {
481
485
  // Do nothing
482
486
  }
487
+ async _beforeUpdate(
483
488
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
484
- async _beforeUpdate(command) {
489
+ command) {
485
490
  // Do nothing
486
491
  }
492
+ async _beforeUpdateMany(
487
493
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
488
- async _beforeUpdateMany(command) {
494
+ command) {
489
495
  // Do nothing
490
496
  }
497
+ async _afterCreate(
491
498
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
492
- async _afterCreate(command, result) {
499
+ command,
500
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
501
+ result) {
493
502
  // Do nothing
494
503
  }
504
+ async _afterReplace(
505
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
506
+ command,
495
507
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
496
- async _afterReplace(command, result) {
508
+ result) {
497
509
  // Do nothing
498
510
  }
511
+ async _afterDelete(
512
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
513
+ command,
499
514
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
500
- async _afterDelete(command, affected) {
515
+ affected) {
501
516
  // Do nothing
502
517
  }
518
+ async _afterDeleteMany(
503
519
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
504
- async _afterDeleteMany(command, affected) {
520
+ command,
521
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
522
+ affected) {
505
523
  // Do nothing
506
524
  }
525
+ async _afterUpdate(
526
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
527
+ command,
507
528
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
508
- async _afterUpdate(command, result) {
529
+ result) {
509
530
  // Do nothing
510
531
  }
532
+ async _afterUpdateMany(
533
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
534
+ command,
511
535
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
512
- async _afterUpdateMany(command, affected) {
536
+ affected) {
513
537
  // Do nothing
514
538
  }
515
539
  }