@opra/mongodb 1.21.0 → 1.22.0

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 (43) hide show
  1. package/package.json +12 -29
  2. package/{esm/services → services}/mongo-collection-service.js +6 -0
  3. package/{esm/services → services}/mongo-nested-service.js +24 -0
  4. package/{esm/services → services}/mongo-service.js +44 -2
  5. package/{esm/services → services}/mongo-singleton-service.js +5 -0
  6. package/cjs/adapter/mongo-adapter.js +0 -127
  7. package/cjs/adapter/mongo-patch-generator.js +0 -215
  8. package/cjs/adapter/prepare-filter.js +0 -190
  9. package/cjs/adapter/prepare-key-values.js +0 -21
  10. package/cjs/adapter/prepare-projection.js +0 -58
  11. package/cjs/adapter/prepare-sort.js +0 -17
  12. package/cjs/index.js +0 -11
  13. package/cjs/package.json +0 -3
  14. package/cjs/services/mongo-collection-service.js +0 -401
  15. package/cjs/services/mongo-entity-service.js +0 -578
  16. package/cjs/services/mongo-nested-service.js +0 -913
  17. package/cjs/services/mongo-service.js +0 -282
  18. package/cjs/services/mongo-singleton-service.js +0 -213
  19. package/cjs/types.js +0 -2
  20. package/esm/package.json +0 -3
  21. package/types/index.d.cts +0 -8
  22. /package/{types/adapter → adapter}/mongo-adapter.d.ts +0 -0
  23. /package/{esm/adapter → adapter}/mongo-adapter.js +0 -0
  24. /package/{types/adapter → adapter}/mongo-patch-generator.d.ts +0 -0
  25. /package/{esm/adapter → adapter}/mongo-patch-generator.js +0 -0
  26. /package/{types/adapter → adapter}/prepare-filter.d.ts +0 -0
  27. /package/{esm/adapter → adapter}/prepare-filter.js +0 -0
  28. /package/{types/adapter → adapter}/prepare-key-values.d.ts +0 -0
  29. /package/{esm/adapter → adapter}/prepare-key-values.js +0 -0
  30. /package/{types/adapter → adapter}/prepare-projection.d.ts +0 -0
  31. /package/{esm/adapter → adapter}/prepare-projection.js +0 -0
  32. /package/{types/adapter → adapter}/prepare-sort.d.ts +0 -0
  33. /package/{esm/adapter → adapter}/prepare-sort.js +0 -0
  34. /package/{types/index.d.ts → index.d.ts} +0 -0
  35. /package/{esm/index.js → index.js} +0 -0
  36. /package/{types/services → services}/mongo-collection-service.d.ts +0 -0
  37. /package/{types/services → services}/mongo-entity-service.d.ts +0 -0
  38. /package/{esm/services → services}/mongo-entity-service.js +0 -0
  39. /package/{types/services → services}/mongo-nested-service.d.ts +0 -0
  40. /package/{types/services → services}/mongo-service.d.ts +0 -0
  41. /package/{types/services → services}/mongo-singleton-service.d.ts +0 -0
  42. /package/{types/types.d.ts → types.d.ts} +0 -0
  43. /package/{esm/types.js → types.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -10,46 +10,29 @@
10
10
  "valgen": "^5.18.2"
11
11
  },
12
12
  "peerDependencies": {
13
- "@opra/common": "^1.21.0",
14
- "@opra/core": "^1.21.0",
15
- "@opra/http": "^1.21.0",
13
+ "@opra/common": "^1.22.0",
14
+ "@opra/core": "^1.22.0",
15
+ "@opra/http": "^1.22.0",
16
16
  "mongodb": "^7.0.0"
17
17
  },
18
- "type": "module",
19
18
  "exports": {
20
19
  ".": {
21
- "import": {
22
- "types": "./types/index.d.ts",
23
- "default": "./esm/index.js"
24
- },
25
- "require": {
26
- "types": "./types/index.d.cts",
27
- "default": "./cjs/index.js"
28
- },
29
- "default": "./esm/index.js"
20
+ "types": "./index.d.ts",
21
+ "default": "./index.js"
30
22
  },
31
23
  "./package.json": "./package.json"
32
24
  },
33
- "main": "./cjs/index.js",
34
- "module": "./esm/index.js",
35
- "types": "./types/index.d.ts",
25
+ "type": "module",
26
+ "module": "./index.js",
27
+ "types": "./index.d.ts",
28
+ "engines": {
29
+ "node": ">=20.0"
30
+ },
36
31
  "repository": {
37
32
  "type": "git",
38
33
  "url": "git+https://github.com/panates/opra.git",
39
34
  "directory": "packages/mongodb"
40
35
  },
41
- "engines": {
42
- "node": ">=16.0",
43
- "npm": ">=7.0.0"
44
- },
45
- "files": [
46
- "bin/",
47
- "cjs/",
48
- "esm/",
49
- "types/",
50
- "LICENSE",
51
- "README.md"
52
- ],
53
36
  "keywords": [
54
37
  "opra",
55
38
  "mongodb",
@@ -6,6 +6,12 @@ import { MongoEntityService } from './mongo-entity-service.js';
6
6
  * @template T - The type of the documents in the collection.
7
7
  */
8
8
  export class MongoCollectionService extends MongoEntityService {
9
+ /**
10
+ * Represents the default limit value for a certain operation.
11
+ *
12
+ * @type {number}
13
+ */
14
+ defaultLimit;
9
15
  /**
10
16
  * Constructs a new instance
11
17
  *
@@ -10,6 +10,30 @@ import { MongoService } from './mongo-service.js';
10
10
  * @template T The type of the array item.
11
11
  */
12
12
  export class MongoNestedService extends MongoService {
13
+ /**
14
+ * Represents the name of the array field in parent document
15
+ *
16
+ * @type {string}
17
+ */
18
+ fieldName;
19
+ /**
20
+ * Represents the value of a nested array key field
21
+ *
22
+ * @type {string}
23
+ */
24
+ nestedKey;
25
+ /**
26
+ * Represents the default limit value for a certain operation.
27
+ *
28
+ * @type {number}
29
+ */
30
+ defaultLimit;
31
+ /**
32
+ * Represents a common array filter function
33
+ *
34
+ * @type {FilterInput | Function}
35
+ */
36
+ nestedFilter;
13
37
  /**
14
38
  * Constructs a new instance
15
39
  *
@@ -9,6 +9,50 @@ const transactionKey = Symbol.for('transaction');
9
9
  * @template T - The type of the documents in the collection.
10
10
  */
11
11
  export class MongoService extends ServiceBase {
12
+ _dataTypeScope;
13
+ _dataType_;
14
+ _dataType;
15
+ _inputCodecs = {};
16
+ _outputCodecs = {};
17
+ /**
18
+ * Defines comma delimited scopes for api document
19
+ */
20
+ scope;
21
+ /**
22
+ * Represents the name of a collection in MongoDB
23
+ */
24
+ collectionName;
25
+ /**
26
+ * Represents the name of a resource.
27
+ * @type {string}
28
+ */
29
+ resourceName;
30
+ /**
31
+ * Represents a MongoDB database object.
32
+ */
33
+ db;
34
+ /**
35
+ * Represents a MongoDB ClientSession.
36
+ */
37
+ session;
38
+ /**
39
+ * Generates a new id for new inserting Document.
40
+ *
41
+ */
42
+ idGenerator;
43
+ /**
44
+ * Callback function for handling errors.
45
+ *
46
+ * @param {unknown} error - The error object.
47
+ * @param _this - The context object.
48
+ */
49
+ onError;
50
+ /**
51
+ * Represents a common filter function for a MongoService.
52
+ *
53
+ * @type {FilterInput | Function}
54
+ */
55
+ documentFilter;
12
56
  /**
13
57
  * Constructs a new instance
14
58
  *
@@ -18,8 +62,6 @@ export class MongoService extends ServiceBase {
18
62
  */
19
63
  constructor(dataType, options) {
20
64
  super();
21
- this._inputCodecs = {};
22
- this._outputCodecs = {};
23
65
  this._dataType_ = dataType;
24
66
  this.db = options?.db;
25
67
  this.documentFilter = options?.documentFilter;
@@ -10,6 +10,11 @@ import { MongoEntityService } from './mongo-entity-service.js';
10
10
  * @template T - The type of document stored in the collection
11
11
  */
12
12
  export class MongoSingletonService extends MongoEntityService {
13
+ /**
14
+ * Represents a unique identifier for singleton record
15
+ * @type {MongoAdapter.AnyId} _id
16
+ */
17
+ _id;
13
18
  /**
14
19
  * Constructs a new instance
15
20
  *
@@ -1,127 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- const prepare_filter_js_1 = tslib_1.__importDefault(require("./prepare-filter.js"));
6
- const prepare_key_values_js_1 = tslib_1.__importDefault(require("./prepare-key-values.js"));
7
- const prepare_projection_js_1 = tslib_1.__importDefault(require("./prepare-projection.js"));
8
- const prepare_sort_js_1 = tslib_1.__importDefault(require("./prepare-sort.js"));
9
- var MongoAdapter;
10
- (function (MongoAdapter) {
11
- MongoAdapter.prepareFilter = prepare_filter_js_1.default;
12
- MongoAdapter.prepareKeyValues = prepare_key_values_js_1.default;
13
- MongoAdapter.prepareProjection = prepare_projection_js_1.default;
14
- MongoAdapter.prepareSort = prepare_sort_js_1.default;
15
- async function parseRequest(context) {
16
- if (context.transport !== 'http') {
17
- throw new TypeError('MongoAdapter can parse only HttpContext');
18
- }
19
- const ctx = context;
20
- const { __oprDef } = ctx;
21
- if (__oprDef?.composition?.startsWith('Entity.') &&
22
- __oprDef.compositionOptions?.type) {
23
- const controller = __oprDef.owner;
24
- switch (__oprDef.composition) {
25
- case 'Entity.Create': {
26
- const data = await ctx.getBody();
27
- const options = {
28
- projection: ctx.queryParams.projection,
29
- };
30
- return {
31
- method: 'create',
32
- data,
33
- options,
34
- };
35
- }
36
- case 'Entity.Delete': {
37
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
38
- controller.parameters.find(p => p.keyParam);
39
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
40
- const options = {
41
- filter: ctx.queryParams.filter,
42
- };
43
- return {
44
- method: 'delete',
45
- key,
46
- options,
47
- };
48
- }
49
- case 'Entity.DeleteMany': {
50
- const options = {
51
- filter: ctx.queryParams.filter,
52
- };
53
- return { method: 'deleteMany', options };
54
- }
55
- case 'Entity.FindMany': {
56
- const options = {
57
- filter: ctx.queryParams.filter,
58
- projection: ctx.queryParams.projection ||
59
- __oprDef.compositionOptions.defaultProjection,
60
- count: ctx.queryParams.count,
61
- limit: ctx.queryParams.limit || __oprDef.compositionOptions.defaultLimit,
62
- skip: ctx.queryParams.skip,
63
- sort: ctx.queryParams.sort || __oprDef.compositionOptions.defaultSort,
64
- };
65
- return { method: 'findMany', options };
66
- }
67
- case 'Entity.Get': {
68
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
69
- controller.parameters.find(p => p.keyParam);
70
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
71
- const options = {
72
- projection: ctx.queryParams.projection,
73
- filter: ctx.queryParams.filter,
74
- };
75
- return { method: 'get', key, options };
76
- }
77
- case 'Entity.Replace': {
78
- const data = await ctx.getBody();
79
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
80
- controller.parameters.find(p => p.keyParam);
81
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
82
- const options = {
83
- projection: ctx.queryParams.projection,
84
- filter: ctx.queryParams.filter,
85
- };
86
- return {
87
- method: 'replace',
88
- key,
89
- data,
90
- options,
91
- };
92
- }
93
- case 'Entity.Update': {
94
- const data = await ctx.getBody();
95
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
96
- controller.parameters.find(p => p.keyParam);
97
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
98
- const options = {
99
- projection: ctx.queryParams.projection,
100
- filter: ctx.queryParams.filter,
101
- };
102
- return {
103
- method: 'update',
104
- key,
105
- data,
106
- options,
107
- };
108
- }
109
- case 'Entity.UpdateMany': {
110
- const data = await ctx.getBody();
111
- const options = {
112
- filter: ctx.queryParams.filter,
113
- };
114
- return {
115
- method: 'updateMany',
116
- data,
117
- options,
118
- };
119
- }
120
- default:
121
- break;
122
- }
123
- }
124
- throw new Error(`This operation is not compatible to MongoDB adapter`);
125
- }
126
- MongoAdapter.parseRequest = parseRequest;
127
- })(MongoAdapter || (exports.MongoAdapter = MongoAdapter = {}));
@@ -1,215 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoPatchGenerator = void 0;
4
- const common_1 = require("@opra/common");
5
- const FIELD_NAME_PATTERN = /^([-><*:])?(.+)$/;
6
- class MongoPatchGenerator {
7
- generatePatch(dataType, doc, options) {
8
- const ctx = {};
9
- this._processComplexType(ctx, dataType, options?.currentPath || '', doc, options?.scope);
10
- const update = {};
11
- if (ctx.$pull)
12
- update.$pull = ctx.$pull;
13
- if (ctx.$unset)
14
- update.$unset = ctx.$unset;
15
- if (ctx.$set)
16
- update.$set = ctx.$set;
17
- if (ctx.$push)
18
- update.$push = ctx.$push;
19
- return {
20
- update,
21
- arrayFilters: ctx.arrayFilters,
22
- initArrayFields: ctx.initArrayFields,
23
- };
24
- }
25
- _processComplexType(ctx, dataType, path, input, scope) {
26
- let result = false;
27
- if (input._$push) {
28
- result =
29
- result || this._processPush(ctx, dataType, path, input._$push, scope);
30
- }
31
- if (input._$pull) {
32
- result =
33
- result || this._processPull(ctx, dataType, path, input._$pull, scope);
34
- }
35
- const keys = Object.keys(input);
36
- const pathDot = path + (path ? '.' : '');
37
- let field;
38
- let key;
39
- let value;
40
- let keyField;
41
- let keyValue;
42
- let arrayIndex = 0;
43
- let arrayFilterName = '';
44
- for (key of keys) {
45
- const m = FIELD_NAME_PATTERN.exec(key);
46
- if (!m)
47
- continue;
48
- key = m[2];
49
- if (key === '_$push' || key === '_$pull')
50
- continue;
51
- value = input[key];
52
- field = dataType.findField(key, scope);
53
- if (field && !field.inScope(scope))
54
- continue;
55
- /** Field not found */
56
- if (!field) {
57
- /** Additional fields will be updated */
58
- if (dataType.additionalFields) {
59
- if (value === null) {
60
- ctx.$unset = ctx.$unset || {};
61
- ctx.$unset[pathDot + key] = 1;
62
- result = true;
63
- }
64
- else {
65
- ctx.$set = ctx.$set || {};
66
- if (dataType.additionalFields instanceof common_1.ComplexType) {
67
- /** Process nested object */
68
- if (this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope)) {
69
- result = true;
70
- }
71
- continue;
72
- }
73
- ctx.$set[pathDot + key] = value;
74
- result = true;
75
- }
76
- }
77
- continue;
78
- }
79
- /** Unset field value if null */
80
- if (value === null) {
81
- ctx.$unset = ctx.$unset || {};
82
- ctx.$unset[pathDot + field.name] = 1;
83
- result = true;
84
- continue;
85
- }
86
- if (field.type instanceof common_1.ComplexType) {
87
- if (!value)
88
- continue;
89
- if (field.isArray) {
90
- if (field.isNestedEntity) {
91
- ctx.initArrayFields = ctx.initArrayFields || [];
92
- ctx.initArrayFields.push(pathDot + field.name);
93
- if (!value.length)
94
- continue;
95
- keyField = field.keyField || field.type.keyField;
96
- if (keyField) {
97
- for (let v of value) {
98
- /** Increase arrayIndex and determine a new name for array filter */
99
- arrayFilterName = 'f' + String(++arrayIndex);
100
- /** Extract key value from object */
101
- keyValue = v[keyField];
102
- if (keyValue == null)
103
- continue;
104
- v = { ...v };
105
- /** Remove key field from object */
106
- delete v[keyField];
107
- /** Process each object in array */
108
- if (this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope)) {
109
- result = true;
110
- /** Add array filter */
111
- ctx.arrayFilters = ctx.arrayFilters || [];
112
- ctx.arrayFilters.unshift({
113
- [`${arrayFilterName}.${keyField}`]: keyValue,
114
- });
115
- }
116
- }
117
- continue;
118
- }
119
- }
120
- }
121
- else {
122
- if (!(typeof value === 'object'))
123
- continue;
124
- /** Process nested object */
125
- if (this._processComplexType(ctx, field.type, pathDot + field.name, value, scope)) {
126
- result = true;
127
- }
128
- continue;
129
- }
130
- }
131
- ctx.$set = ctx.$set || {};
132
- ctx.$set[pathDot + field.name] = value;
133
- result = true;
134
- }
135
- return result;
136
- }
137
- _processPush(ctx, dataType, path, input, scope) {
138
- let result = false;
139
- let field;
140
- let key;
141
- let value;
142
- const pathDot = path + (path ? '.' : '');
143
- const keys = Object.keys(input);
144
- let keyField;
145
- for (key of keys) {
146
- value = input[key];
147
- field = dataType.getField(key, scope);
148
- if (!(field && field.isArray))
149
- continue;
150
- ctx.$push = ctx.$push || {};
151
- if (field.type instanceof common_1.ComplexType) {
152
- keyField = field.keyField || field.type.keyField;
153
- if (keyField) {
154
- if (Array.isArray(value)) {
155
- value.forEach(v => {
156
- if (!v[keyField]) {
157
- throw new TypeError(`You must provide a key value of ${field.type.name} for $push operation.`);
158
- }
159
- });
160
- ctx.$push[pathDot + key] = { $each: value };
161
- result = true;
162
- }
163
- else {
164
- if (!value[keyField]) {
165
- throw new TypeError(`You must provide a key value of ${field.type.name} for $push operation.`);
166
- }
167
- ctx.$push[pathDot + key] = value;
168
- result = true;
169
- }
170
- }
171
- continue;
172
- }
173
- ctx.$push[pathDot + key] = Array.isArray(value)
174
- ? { $each: value }
175
- : value;
176
- result = true;
177
- }
178
- return result;
179
- }
180
- _processPull(ctx, dataType, path, input, scope) {
181
- let result = false;
182
- let field;
183
- let key;
184
- let value;
185
- const pathDot = path + (path ? '.' : '');
186
- const keys = Object.keys(input);
187
- let keyField;
188
- for (key of keys) {
189
- value = input[key];
190
- field = dataType.getField(key, scope);
191
- if (!(field && field.isArray))
192
- continue;
193
- ctx.$pull = ctx.$pull || {};
194
- if (field.type instanceof common_1.ComplexType) {
195
- keyField = field.keyField || field.type.keyField;
196
- if (!keyField)
197
- continue;
198
- ctx.$pull[pathDot + key] = {
199
- $elemMatch: {
200
- [keyField]: Array.isArray(value) ? { $in: value } : value,
201
- },
202
- };
203
- result = true;
204
- }
205
- else {
206
- ctx.$pull[pathDot + key] = Array.isArray(value)
207
- ? { $in: value }
208
- : value;
209
- result = true;
210
- }
211
- }
212
- return result;
213
- }
214
- }
215
- exports.MongoPatchGenerator = MongoPatchGenerator;