@opra/mongodb 0.33.13 → 1.0.0-alpha.2

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.
@@ -8,7 +8,7 @@ const opMap = {
8
8
  '>=': '$gte',
9
9
  '<': '$lt',
10
10
  '<=': '$lte',
11
- 'in': '$in',
11
+ in: '$in',
12
12
  '!in': '$nin',
13
13
  };
14
14
  /**
@@ -63,9 +63,7 @@ function prepareFilter(filters, options) {
63
63
  }
64
64
  i++;
65
65
  }
66
- return i
67
- ? (options?.fieldPrefix ? addPrefix(out, options.fieldPrefix) : out)
68
- : undefined;
66
+ return i ? (options?.fieldPrefix ? addPrefix(out, options.fieldPrefix) : out) : undefined;
69
67
  }
70
68
  exports.default = prepareFilter;
71
69
  function addPrefix(source, prefix) {
@@ -85,10 +83,8 @@ function addPrefix(source, prefix) {
85
83
  function prepareFilterAst(ast, negative) {
86
84
  if (!ast)
87
85
  return;
88
- if (ast instanceof common_1.OpraFilter.QualifiedIdentifier) {
89
- return ast.value;
90
- }
91
- if (ast instanceof common_1.OpraFilter.NumberLiteral ||
86
+ if (ast instanceof common_1.OpraFilter.QualifiedIdentifier ||
87
+ ast instanceof common_1.OpraFilter.NumberLiteral ||
92
88
  ast instanceof common_1.OpraFilter.StringLiteral ||
93
89
  ast instanceof common_1.OpraFilter.BooleanLiteral ||
94
90
  ast instanceof common_1.OpraFilter.NullLiteral ||
@@ -103,9 +99,7 @@ function prepareFilterAst(ast, negative) {
103
99
  return prepareFilterAst(ast.expression, !negative);
104
100
  }
105
101
  if (ast instanceof common_1.OpraFilter.LogicalExpression) {
106
- const items = ast.items
107
- .map(x => prepareFilterAst(x, negative))
108
- .filter(x => x != null);
102
+ const items = ast.items.map(x => prepareFilterAst(x, negative)).filter(x => x != null);
109
103
  if (ast.op === 'or')
110
104
  return { $or: items };
111
105
  return { $and: items };
@@ -118,15 +112,13 @@ function prepareFilterAst(ast, negative) {
118
112
  if (ast.right instanceof common_1.OpraFilter.QualifiedIdentifier) {
119
113
  const op = opMap[ast.op];
120
114
  if (op)
121
- return { $expr: { [op]: ["$" + left, "$" + ast.right.value] } };
115
+ return { $expr: { [op]: ['$' + left, '$' + ast.right.value] } };
122
116
  /* istanbul ignore next */
123
117
  throw new Error(`Invalid filter query.`);
124
118
  }
125
119
  let right = prepareFilterAst(ast.right);
126
120
  if (right == null) {
127
- const op = ast.op === '='
128
- ? (negative ? '!=' : '=')
129
- : (negative ? '=' : '!=');
121
+ const op = ast.op === '=' ? (negative ? '!=' : '=') : negative ? '=' : '!=';
130
122
  if (op === '=')
131
123
  return { $or: [{ [left]: null }, { [left]: { $exists: false } }] };
132
124
  if (op === '!=')
@@ -146,38 +138,38 @@ function prepareFilterAst(ast, negative) {
146
138
  [left]: wrapNot({
147
139
  $text: {
148
140
  $search: '\\"' + right.replace(/\\"/, '"') + '\\"',
149
- $caseSensitive: true
150
- }
151
- }, negative)
141
+ $caseSensitive: true,
142
+ },
143
+ }, negative),
152
144
  };
153
145
  case 'ilike':
154
146
  return {
155
147
  [left]: wrapNot({
156
148
  $text: {
157
- $search: '\\"' + right.replace(/\\"/, '"') + '\\"'
158
- }
159
- }, negative)
149
+ $search: '\\"' + right.replace(/\\"/, '"') + '\\"',
150
+ },
151
+ }, negative),
160
152
  };
161
153
  case '!like':
162
154
  return {
163
155
  [left]: wrapNot({
164
156
  $text: {
165
157
  $search: '\\"' + right.replace(/\\"/, '"') + '\\"',
166
- $caseSensitive: true
167
- }
168
- }, !negative)
158
+ $caseSensitive: true,
159
+ },
160
+ }, !negative),
169
161
  };
170
162
  case '!ilike':
171
163
  return {
172
164
  [left]: wrapNot({
173
165
  $text: {
174
- $search: '\\"' + right.replace(/\\"/, '"') + '\\"'
175
- }
176
- }, !negative)
166
+ $search: '\\"' + right.replace(/\\"/, '"') + '\\"',
167
+ },
168
+ }, !negative),
177
169
  };
178
170
  }
179
171
  throw new Error(`Unimplemented ComparisonExpression operation (right side is ${ast.right.kind})`);
180
172
  }
181
173
  throw new Error(`${ast.kind} is not implemented yet`);
182
174
  }
183
- const wrapNot = (o, negative) => negative ? { $not: o } : o;
175
+ const wrapNot = (o, negative) => (negative ? { $not: o } : o);
@@ -1,52 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._prepareProjection = void 0;
3
+ exports.prepare = void 0;
4
4
  const common_1 = require("@opra/common");
5
- function prepareProjection(dataType, options) {
5
+ function prepareProjection(dataType, projection) {
6
+ if (projection && typeof projection === 'object' && !Array.isArray(projection))
7
+ return projection;
6
8
  const out = {};
7
- const pick = options?.pick && (0, common_1.pathToObjectTree)(options.pick);
8
- const include = options?.include && (0, common_1.pathToObjectTree)(options.include);
9
- const omit = options?.omit && (0, common_1.pathToObjectTree)(options.omit);
9
+ const projection_ = typeof projection === 'string' || Array.isArray(projection) ? (0, common_1.parseFieldsProjection)(projection) : projection;
10
10
  // const exclusionProjection = !pick && !!omit;
11
- _prepareProjection(dataType, out, {
12
- pickActivated: !!pick,
13
- pick,
14
- include,
15
- omit
16
- });
11
+ prepare(dataType, out, projection_);
17
12
  return Object.keys(out).length ? out : undefined;
18
13
  }
19
14
  exports.default = prepareProjection;
20
- function _prepareProjection(dataType, target,
21
- // exclusionProjection: boolean,
22
- options) {
23
- // const defaultFields = options?.defaultFields ?? !options?.pick;
24
- const optionsOmit = options?.omit;
25
- const optionsPick = options?.pick;
26
- const optionsInclude = options?.include;
27
- const pickActivated = options?.pickActivated;
28
- for (const [k, f] of dataType.fields.entries()) {
29
- const fieldOmit = optionsOmit?.[k];
30
- const fieldInclude = optionsInclude?.[k];
31
- const fieldPick = optionsPick?.[k];
32
- if (fieldOmit === true ||
33
- !((pickActivated && fieldPick) ||
34
- (!pickActivated && (!f.exclusive || fieldInclude))))
15
+ function prepare(dataType, target, projection) {
16
+ const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
17
+ const projectionKeys = projection && Object.keys(projection).map(x => x.toLowerCase());
18
+ const projectionKeysSet = new Set(projectionKeys);
19
+ let fieldName;
20
+ let field;
21
+ let k;
22
+ /** Add fields from data type */
23
+ for (field of dataType.fields.values()) {
24
+ fieldName = field.name;
25
+ k = fieldName.toLowerCase();
26
+ projectionKeysSet.delete(k);
27
+ const p = projection?.[k];
28
+ if (
29
+ /** Ignore if field is omitted */
30
+ p?.sign === '-' ||
31
+ /** Ignore if default fields and field is not in projection */
32
+ (!defaultFields && !p) ||
33
+ /** Ignore if default fields enabled and fields is exclusive */
34
+ (defaultFields && field.exclusive && !p)) {
35
35
  continue;
36
- if (f.type instanceof common_1.ComplexType &&
37
- (typeof fieldInclude === 'object' ||
38
- typeof fieldPick === 'object' ||
39
- typeof fieldOmit === 'object')) {
40
- target[k] = {};
41
- _prepareProjection(f.type, target[k], {
42
- pickActivated: fieldPick != null && fieldPick !== true,
43
- include: typeof fieldInclude === 'object' ? fieldInclude : undefined,
44
- pick: typeof fieldPick === 'object' ? fieldPick : undefined,
45
- omit: typeof fieldOmit === 'object' ? fieldOmit : undefined
46
- });
36
+ }
37
+ if (field.type instanceof common_1.ComplexType && typeof p?.projection === 'object') {
38
+ target[fieldName] = {};
39
+ prepare(field.type, target[fieldName], p.projection);
47
40
  continue;
48
41
  }
49
- target[k] = 1;
42
+ target[fieldName] = 1;
43
+ }
44
+ /** Add additional fields */
45
+ if (dataType.additionalFields) {
46
+ for (k of projectionKeysSet.values()) {
47
+ const n = projectionKeysSet[k];
48
+ if (n?.sign !== '-')
49
+ target[k] = 1;
50
+ }
50
51
  }
51
52
  }
52
- exports._prepareProjection = _prepareProjection;
53
+ exports.prepare = prepare;
package/cjs/index.js CHANGED
@@ -2,8 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./mongo-adapter.js"), exports);
5
- tslib_1.__exportStar(require("./mongo-array-service.js"), exports);
5
+ tslib_1.__exportStar(require("./mongo-nested-service.js"), exports);
6
6
  tslib_1.__exportStar(require("./mongo-collection-service.js"), exports);
7
7
  tslib_1.__exportStar(require("./mongo-service.js"), exports);
8
8
  tslib_1.__exportStar(require("./mongo-singleton-service.js"), exports);
9
- tslib_1.__exportStar(require("./types.js"), exports);
@@ -9,9 +9,74 @@ const prepare_projection_js_1 = tslib_1.__importDefault(require("./adapter-utils
9
9
  const prepare_sort_js_1 = tslib_1.__importDefault(require("./adapter-utils/prepare-sort.js"));
10
10
  var MongoAdapter;
11
11
  (function (MongoAdapter) {
12
- MongoAdapter.prepareKeyValues = prepare_key_values_js_1.default;
13
12
  MongoAdapter.prepareFilter = prepare_filter_js_1.default;
13
+ MongoAdapter.prepareKeyValues = prepare_key_values_js_1.default;
14
14
  MongoAdapter.preparePatch = prepare_patch_js_1.default;
15
15
  MongoAdapter.prepareProjection = prepare_projection_js_1.default;
16
16
  MongoAdapter.prepareSort = prepare_sort_js_1.default;
17
+ async function parseRequest(context) {
18
+ const { operation } = context;
19
+ if (operation.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
20
+ const { compositionOptions } = operation;
21
+ switch (operation.composition) {
22
+ case 'Entity.Create': {
23
+ const data = await context.getBody();
24
+ const options = {
25
+ projection: context.queryParams.projection,
26
+ };
27
+ return { method: 'create', data, options };
28
+ }
29
+ case 'Entity.Delete': {
30
+ const key = context.pathParams[compositionOptions.keyParameter];
31
+ const options = {
32
+ filter: context.queryParams.filter,
33
+ };
34
+ return { method: 'delete', key, options };
35
+ }
36
+ case 'Entity.DeleteMany': {
37
+ const options = {
38
+ filter: context.queryParams.filter,
39
+ };
40
+ return { method: 'deleteMany', options };
41
+ }
42
+ case 'Entity.FindMany': {
43
+ const options = {
44
+ filter: context.queryParams.filter,
45
+ projection: context.queryParams.projection,
46
+ count: context.queryParams.count,
47
+ limit: context.queryParams.limit,
48
+ skip: context.queryParams.skip,
49
+ sort: context.queryParams.sort,
50
+ };
51
+ return { method: 'findMany', options };
52
+ }
53
+ case 'Entity.Get': {
54
+ const key = context.pathParams[compositionOptions.keyParameter];
55
+ const options = {
56
+ projection: context.queryParams.projection,
57
+ filter: context.queryParams.filter,
58
+ };
59
+ return { method: 'get', key, options };
60
+ }
61
+ case 'Entity.Update': {
62
+ const data = await context.getBody();
63
+ const key = context.pathParams[compositionOptions.keyParameter];
64
+ const options = {
65
+ projection: context.queryParams.projection,
66
+ filter: context.queryParams.filter,
67
+ };
68
+ return { method: 'update', key, data, options };
69
+ }
70
+ case 'Entity.UpdateMany': {
71
+ const data = await context.getBody();
72
+ const options = {
73
+ filter: context.queryParams.filter,
74
+ };
75
+ return { method: 'updateMany', data, options };
76
+ }
77
+ }
78
+ }
79
+ throw new Error(`This operation is not compatible to MongoDB adapter`);
80
+ }
81
+ MongoAdapter.parseRequest = parseRequest;
17
82
  })(MongoAdapter || (exports.MongoAdapter = MongoAdapter = {}));