@opra/mongodb 1.0.0-alpha.7 → 1.0.0-alpha.8

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.
@@ -167,6 +167,8 @@ function prepareFilterAst(ast, negative) {
167
167
  },
168
168
  }, !negative),
169
169
  };
170
+ default:
171
+ break;
170
172
  }
171
173
  throw new Error(`Unimplemented ComparisonExpression operation (right side is ${ast.right.kind})`);
172
174
  }
@@ -6,13 +6,14 @@ const defaultPrimaryKey = ['_id'];
6
6
  function prepareKeyValues(keyValue, primaryKey) {
7
7
  primaryKey = primaryKey || defaultPrimaryKey;
8
8
  const b = (0, putil_isplainobject_1.default)(keyValue);
9
- if (primaryKey.length > 1 && !b)
10
- new TypeError(`Argument "keyValue" must be an object that contains all key values`);
9
+ if (primaryKey.length > 1 && !b) {
10
+ throw new TypeError(`Argument "keyValue" must be an object that contains all key values`);
11
+ }
11
12
  if (primaryKey.length > 1 || b) {
12
13
  return primaryKey.reduce((o, k) => {
13
14
  o[k] = keyValue[k];
14
15
  if (o[k] == null)
15
- new Error(`Value of key "${k}" is required`);
16
+ throw new Error(`Value of key "${k}" is required`);
16
17
  return o;
17
18
  }, {});
18
19
  }
@@ -7,10 +7,11 @@ function preparePatch(doc, options) {
7
7
  return trg;
8
8
  }
9
9
  exports.default = preparePatch;
10
- function _preparePatch(src, trg = {}, path, options) {
10
+ function _preparePatch(src, trg, path, options) {
11
11
  let f;
12
12
  let key;
13
13
  let field;
14
+ trg = trg || {};
14
15
  const fieldPrefix = options?.fieldPrefix;
15
16
  for (const [k, v] of Object.entries(src)) {
16
17
  f = k.startsWith('*') ? k.substring(1) : k;
package/cjs/index.js CHANGED
@@ -2,7 +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-nested-service.js"), exports);
6
5
  tslib_1.__exportStar(require("./mongo-collection-service.js"), exports);
6
+ tslib_1.__exportStar(require("./mongo-nested-service.js"), exports);
7
7
  tslib_1.__exportStar(require("./mongo-service.js"), exports);
8
8
  tslib_1.__exportStar(require("./mongo-singleton-service.js"), exports);
@@ -77,6 +77,8 @@ var MongoAdapter;
77
77
  };
78
78
  return { method: 'updateMany', data, options };
79
79
  }
80
+ default:
81
+ break;
80
82
  }
81
83
  }
82
84
  throw new Error(`This operation is not compatible to MongoDB adapter`);
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoCollectionService = void 0;
4
- const mongodb_1 = require("mongodb");
5
4
  const common_1 = require("@opra/common");
5
+ const mongodb_1 = require("mongodb");
6
6
  const mongo_adapter_js_1 = require("./mongo-adapter.js");
7
7
  const mongo_entity_service_js_1 = require("./mongo-entity-service.js");
8
8
  /**
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoEntityService = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
5
  const assert = tslib_1.__importStar(require("node:assert"));
7
6
  const common_1 = require("@opra/common");
7
+ const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
8
8
  const mongo_adapter_js_1 = require("./mongo-adapter.js");
9
9
  const mongo_service_js_1 = require("./mongo-service.js");
10
10
  /**
@@ -239,8 +239,9 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
239
239
  async _update(id, input, options) {
240
240
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
241
241
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
242
- if (isUpdateFilter && isDocument)
242
+ if (isUpdateFilter && isDocument) {
243
243
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
244
+ }
244
245
  let update;
245
246
  if (isDocument) {
246
247
  const inputCodec = this.getInputCodec('update');
@@ -274,8 +275,9 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
274
275
  async _updateOnly(id, input, options) {
275
276
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
276
277
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
277
- if (isUpdateFilter && isDocument)
278
+ if (isUpdateFilter && isDocument) {
278
279
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
280
+ }
279
281
  let update;
280
282
  if (isDocument) {
281
283
  const inputCodec = this.getInputCodec('update');
@@ -307,8 +309,9 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
307
309
  async _updateMany(input, options) {
308
310
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
309
311
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
310
- if (isUpdateFilter && isDocument)
312
+ if (isUpdateFilter && isDocument) {
311
313
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
314
+ }
312
315
  let update;
313
316
  if (isDocument) {
314
317
  const inputCodec = this.getInputCodec('update');
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoNestedService = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
6
5
  const common_1 = require("@opra/common");
6
+ const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
7
7
  const mongo_adapter_js_1 = require("./mongo-adapter.js");
8
8
  const mongo_service_js_1 = require("./mongo-service.js");
9
9
  /**
@@ -50,8 +50,9 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
50
50
  * @throws {ResourceNotAvailableError} - If the resource does not exist.
51
51
  */
52
52
  async assert(documentId, id, options) {
53
- if (!(await this.exists(documentId, id, options)))
53
+ if (!(await this.exists(documentId, id, options))) {
54
54
  throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + id);
55
+ }
55
56
  }
56
57
  /**
57
58
  * Adds a single item into the array field.
@@ -457,8 +458,9 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
457
458
  */
458
459
  async get(documentId, nestedId, options) {
459
460
  const out = await this.findById(documentId, nestedId, options);
460
- if (!out)
461
+ if (!out) {
461
462
  throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + nestedId);
463
+ }
462
464
  return out;
463
465
  }
464
466
  /**
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoService = void 0;
4
- const mongodb_1 = require("mongodb");
5
4
  const common_1 = require("@opra/common");
6
5
  const core_1 = require("@opra/core");
6
+ const mongodb_1 = require("mongodb");
7
7
  /**
8
8
  * Class representing a MongoDB service for interacting with a collection.
9
9
  * @extends ServiceBase
@@ -117,7 +117,7 @@ class MongoService extends core_1.ServiceBase {
117
117
  if (session)
118
118
  return callback(session);
119
119
  // Backup old session property
120
- const hasOldSession = this.hasOwnProperty('session');
120
+ const hasOldSession = Object.prototype.hasOwnProperty.call(this, 'session');
121
121
  const oldSessionGetter = hasOldSession ? this.session : undefined;
122
122
  const db = this.getDatabase();
123
123
  const client = db.client;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoSingletonService = void 0;
4
- const mongodb_1 = require("mongodb");
5
4
  const common_1 = require("@opra/common");
5
+ const mongodb_1 = require("mongodb");
6
6
  const mongo_adapter_js_1 = require("./mongo-adapter.js");
7
7
  const mongo_entity_service_js_1 = require("./mongo-entity-service.js");
8
8
  /**
@@ -164,6 +164,8 @@ function prepareFilterAst(ast, negative) {
164
164
  },
165
165
  }, !negative),
166
166
  };
167
+ default:
168
+ break;
167
169
  }
168
170
  throw new Error(`Unimplemented ComparisonExpression operation (right side is ${ast.right.kind})`);
169
171
  }
@@ -3,13 +3,14 @@ const defaultPrimaryKey = ['_id'];
3
3
  export default function prepareKeyValues(keyValue, primaryKey) {
4
4
  primaryKey = primaryKey || defaultPrimaryKey;
5
5
  const b = isPlainObject(keyValue);
6
- if (primaryKey.length > 1 && !b)
7
- new TypeError(`Argument "keyValue" must be an object that contains all key values`);
6
+ if (primaryKey.length > 1 && !b) {
7
+ throw new TypeError(`Argument "keyValue" must be an object that contains all key values`);
8
+ }
8
9
  if (primaryKey.length > 1 || b) {
9
10
  return primaryKey.reduce((o, k) => {
10
11
  o[k] = keyValue[k];
11
12
  if (o[k] == null)
12
- new Error(`Value of key "${k}" is required`);
13
+ throw new Error(`Value of key "${k}" is required`);
13
14
  return o;
14
15
  }, {});
15
16
  }
@@ -4,10 +4,11 @@ export default function preparePatch(doc, options) {
4
4
  trg.$set = trg.$set || {};
5
5
  return trg;
6
6
  }
7
- function _preparePatch(src, trg = {}, path, options) {
7
+ function _preparePatch(src, trg, path, options) {
8
8
  let f;
9
9
  let key;
10
10
  let field;
11
+ trg = trg || {};
11
12
  const fieldPrefix = options?.fieldPrefix;
12
13
  for (const [k, v] of Object.entries(src)) {
13
14
  f = k.startsWith('*') ? k.substring(1) : k;
package/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './mongo-adapter.js';
2
- export * from './mongo-nested-service.js';
3
2
  export * from './mongo-collection-service.js';
3
+ export * from './mongo-nested-service.js';
4
4
  export * from './mongo-service.js';
5
5
  export * from './mongo-singleton-service.js';
@@ -73,6 +73,8 @@ export var MongoAdapter;
73
73
  };
74
74
  return { method: 'updateMany', data, options };
75
75
  }
76
+ default:
77
+ break;
76
78
  }
77
79
  }
78
80
  throw new Error(`This operation is not compatible to MongoDB adapter`);
@@ -1,5 +1,5 @@
1
- import { ObjectId } from 'mongodb';
2
1
  import { ResourceNotAvailableError } from '@opra/common';
2
+ import { ObjectId } from 'mongodb';
3
3
  import { MongoAdapter } from './mongo-adapter.js';
4
4
  import { MongoEntityService } from './mongo-entity-service.js';
5
5
  /**
@@ -1,6 +1,6 @@
1
- import omit from 'lodash.omit';
2
1
  import * as assert from 'node:assert';
3
2
  import { InternalServerError } from '@opra/common';
3
+ import omit from 'lodash.omit';
4
4
  import { MongoAdapter } from './mongo-adapter.js';
5
5
  import { MongoService } from './mongo-service.js';
6
6
  /**
@@ -235,8 +235,9 @@ export class MongoEntityService extends MongoService {
235
235
  async _update(id, input, options) {
236
236
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
237
237
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
238
- if (isUpdateFilter && isDocument)
238
+ if (isUpdateFilter && isDocument) {
239
239
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
240
+ }
240
241
  let update;
241
242
  if (isDocument) {
242
243
  const inputCodec = this.getInputCodec('update');
@@ -270,8 +271,9 @@ export class MongoEntityService extends MongoService {
270
271
  async _updateOnly(id, input, options) {
271
272
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
272
273
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
273
- if (isUpdateFilter && isDocument)
274
+ if (isUpdateFilter && isDocument) {
274
275
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
276
+ }
275
277
  let update;
276
278
  if (isDocument) {
277
279
  const inputCodec = this.getInputCodec('update');
@@ -303,8 +305,9 @@ export class MongoEntityService extends MongoService {
303
305
  async _updateMany(input, options) {
304
306
  const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
305
307
  const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
306
- if (isUpdateFilter && isDocument)
308
+ if (isUpdateFilter && isDocument) {
307
309
  throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
310
+ }
308
311
  let update;
309
312
  if (isDocument) {
310
313
  const inputCodec = this.getInputCodec('update');
@@ -1,5 +1,5 @@
1
- import omit from 'lodash.omit';
2
1
  import { ComplexType, NotAcceptableError, ResourceNotAvailableError } from '@opra/common';
2
+ import omit from 'lodash.omit';
3
3
  import { MongoAdapter } from './mongo-adapter.js';
4
4
  import { MongoService } from './mongo-service.js';
5
5
  /**
@@ -46,8 +46,9 @@ export class MongoNestedService extends MongoService {
46
46
  * @throws {ResourceNotAvailableError} - If the resource does not exist.
47
47
  */
48
48
  async assert(documentId, id, options) {
49
- if (!(await this.exists(documentId, id, options)))
49
+ if (!(await this.exists(documentId, id, options))) {
50
50
  throw new ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + id);
51
+ }
51
52
  }
52
53
  /**
53
54
  * Adds a single item into the array field.
@@ -453,8 +454,9 @@ export class MongoNestedService extends MongoService {
453
454
  */
454
455
  async get(documentId, nestedId, options) {
455
456
  const out = await this.findById(documentId, nestedId, options);
456
- if (!out)
457
+ if (!out) {
457
458
  throw new ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + nestedId);
459
+ }
458
460
  return out;
459
461
  }
460
462
  /**
@@ -1,6 +1,6 @@
1
- import { ObjectId } from 'mongodb';
2
1
  import { DATATYPE_METADATA } from '@opra/common';
3
2
  import { ServiceBase } from '@opra/core';
3
+ import { ObjectId } from 'mongodb';
4
4
  /**
5
5
  * Class representing a MongoDB service for interacting with a collection.
6
6
  * @extends ServiceBase
@@ -114,7 +114,7 @@ export class MongoService extends ServiceBase {
114
114
  if (session)
115
115
  return callback(session);
116
116
  // Backup old session property
117
- const hasOldSession = this.hasOwnProperty('session');
117
+ const hasOldSession = Object.prototype.hasOwnProperty.call(this, 'session');
118
118
  const oldSessionGetter = hasOldSession ? this.session : undefined;
119
119
  const db = this.getDatabase();
120
120
  const client = db.client;
@@ -1,5 +1,5 @@
1
- import { ObjectId } from 'mongodb';
2
1
  import { ResourceNotAvailableError } from '@opra/common';
2
+ import { ObjectId } from 'mongodb';
3
3
  import { MongoAdapter } from './mongo-adapter.js';
4
4
  import { MongoEntityService } from './mongo-entity-service.js';
5
5
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -17,6 +17,7 @@
17
17
  "build:esm": "tsc -b tsconfig-build-esm.json",
18
18
  "postbuild": "cp README.md package.json ../../LICENSE ../../build/mongodb && cp ../../package.cjs.json ../../build/mongodb/cjs/package.json",
19
19
  "lint": "eslint . --max-warnings=0",
20
+ "lint:fix": "eslint . --max-warnings=0 --fix",
20
21
  "format": "prettier . --write --log-level=warn",
21
22
  "test": "jest --passWithNoTests",
22
23
  "cover": "jest --passWithNoTests --collect-coverage",
@@ -25,14 +26,18 @@
25
26
  "clean:dist": "rimraf ../../build/mongodb",
26
27
  "clean:cover": "rimraf ../../coverage/mongodb"
27
28
  },
29
+ "dependencies": {
30
+ "lodash.omit": "^4.5.0",
31
+ "putil-isplainobject": "^1.1.5"
32
+ },
28
33
  "devDependencies": {
29
34
  "@faker-js/faker": "^8.4.1",
30
- "mongodb": "^6.7.0",
35
+ "mongodb": "^6.8.0",
31
36
  "ts-gems": "^3.4.0"
32
37
  },
33
38
  "peerDependencies": {
34
- "@opra/common": "^1.0.0-alpha.7",
35
- "@opra/core": "^1.0.0-alpha.7",
39
+ "@opra/common": "^1.0.0-alpha.8",
40
+ "@opra/core": "^1.0.0-alpha.8",
36
41
  "mongodb": ">= 6.0.0"
37
42
  },
38
43
  "type": "module",
@@ -1,4 +1,4 @@
1
- import mongodb, { Document } from 'mongodb';
2
1
  import { ComplexType, FieldsProjection } from '@opra/common';
2
+ import mongodb, { Document } from 'mongodb';
3
3
  export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document): mongodb.Document | undefined;
4
4
  export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection): void;
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './mongo-adapter.js';
2
- export * from './mongo-nested-service.js';
3
2
  export * from './mongo-collection-service.js';
3
+ export * from './mongo-nested-service.js';
4
4
  export * from './mongo-service.js';
5
5
  export * from './mongo-singleton-service.js';
@@ -1,6 +1,6 @@
1
- import mongodb, { ClientSession, ObjectId } from 'mongodb';
2
1
  import { OpraFilter } from '@opra/common';
3
2
  import { HttpContext } from '@opra/core';
3
+ import mongodb, { ClientSession, ObjectId } from 'mongodb';
4
4
  import _prepareFilter from './adapter-utils/prepare-filter.js';
5
5
  import _prepareKeyValues from './adapter-utils/prepare-key-values.js';
6
6
  import _preparePatch from './adapter-utils/prepare-patch.js';
@@ -1,6 +1,6 @@
1
+ import { ComplexType } from '@opra/common';
1
2
  import mongodb from 'mongodb';
2
3
  import { PartialDTO, PatchDTO, Type } from 'ts-gems';
3
- import { ComplexType } from '@opra/common';
4
4
  import { MongoAdapter } from './mongo-adapter.js';
5
5
  import { MongoCollectionService } from './mongo-collection-service.js';
6
6
  import { MongoService } from './mongo-service.js';
@@ -1,9 +1,9 @@
1
- import mongodb, { Document, TransactionOptions } from 'mongodb';
2
- import { PartialDTO, StrictOmit, Type } from 'ts-gems';
3
- import { IsObject } from 'valgen';
4
1
  import * as OpraCommon from '@opra/common';
5
2
  import { ComplexType } from '@opra/common';
6
3
  import { ServiceBase } from '@opra/core';
4
+ import mongodb, { Document, TransactionOptions } from 'mongodb';
5
+ import { PartialDTO, StrictOmit, Type } from 'ts-gems';
6
+ import { IsObject } from 'valgen';
7
7
  import { MongoAdapter } from './mongo-adapter.js';
8
8
  /**
9
9
  * The namespace for the MongoService.