@opra/mongodb 1.4.4 → 1.5.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.
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = prepareProjection;
4
4
  exports.prepare = prepare;
5
5
  const common_1 = require("@opra/common");
6
- function prepareProjection(dataType, projection) {
6
+ function prepareProjection(dataType, projection, scopes) {
7
7
  if (projection === '*')
8
8
  return undefined;
9
9
  if (projection &&
@@ -14,11 +14,10 @@ function prepareProjection(dataType, projection) {
14
14
  const projection_ = typeof projection === 'string' || Array.isArray(projection)
15
15
  ? (0, common_1.parseFieldsProjection)(projection)
16
16
  : projection;
17
- // const exclusionProjection = !pick && !!omit;
18
- prepare(dataType, out, projection_);
17
+ prepare(dataType, out, projection_, scopes);
19
18
  return Object.keys(out).length ? out : undefined;
20
19
  }
21
- function prepare(dataType, target, projection) {
20
+ function prepare(dataType, target, projection, scopes) {
22
21
  const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
23
22
  const projectionKeys = projection && Object.keys(projection);
24
23
  const projectionKeysSet = new Set(projectionKeys);
@@ -30,6 +29,9 @@ function prepare(dataType, target, projection) {
30
29
  fieldName = field.name;
31
30
  k = fieldName.toLowerCase();
32
31
  projectionKeysSet.delete(k);
32
+ /** Ignore if field is not in scope */
33
+ if (!field.inScope(scopes))
34
+ continue;
33
35
  const p = projection?.[k];
34
36
  if (
35
37
  /** Ignore if field is omitted */
@@ -91,8 +91,14 @@ class MongoService extends core_1.ServiceBase {
91
91
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
92
92
  */
93
93
  get dataType() {
94
+ if (this._dataType && this._dataTypeScope !== this.scopes)
95
+ this._dataType = undefined;
94
96
  if (!this._dataType)
95
97
  this._dataType = this.context.documentNode.getComplexType(this._dataType_);
98
+ this._dataTypeScope = this.scopes;
99
+ this._dataTypeScopes = this.scopes
100
+ ? this.scopes?.split(/\s*,\s*/)
101
+ : undefined;
96
102
  return this._dataType;
97
103
  }
98
104
  /**
@@ -243,6 +249,7 @@ class MongoService extends core_1.ServiceBase {
243
249
  if (validator)
244
250
  return validator;
245
251
  const options = { projection: '*' };
252
+ options.scope = this._dataTypeScopes;
246
253
  if (operation === 'update') {
247
254
  options.partial = 'deep';
248
255
  options.allowPatchOperators = true;
@@ -262,7 +269,7 @@ class MongoService extends core_1.ServiceBase {
262
269
  const options = {
263
270
  projection: '*',
264
271
  partial: 'deep',
265
- ignoreHiddenFields: true,
272
+ scope: this._dataTypeScopes,
266
273
  };
267
274
  const dataType = this.dataType;
268
275
  validator = dataType.generateCodec('decode', options);
@@ -1,5 +1,5 @@
1
1
  import { ComplexType, parseFieldsProjection, } from '@opra/common';
2
- export default function prepareProjection(dataType, projection) {
2
+ export default function prepareProjection(dataType, projection, scopes) {
3
3
  if (projection === '*')
4
4
  return undefined;
5
5
  if (projection &&
@@ -10,11 +10,10 @@ export default function prepareProjection(dataType, projection) {
10
10
  const projection_ = typeof projection === 'string' || Array.isArray(projection)
11
11
  ? parseFieldsProjection(projection)
12
12
  : projection;
13
- // const exclusionProjection = !pick && !!omit;
14
- prepare(dataType, out, projection_);
13
+ prepare(dataType, out, projection_, scopes);
15
14
  return Object.keys(out).length ? out : undefined;
16
15
  }
17
- export function prepare(dataType, target, projection) {
16
+ export function prepare(dataType, target, projection, scopes) {
18
17
  const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
19
18
  const projectionKeys = projection && Object.keys(projection);
20
19
  const projectionKeysSet = new Set(projectionKeys);
@@ -26,6 +25,9 @@ export function prepare(dataType, target, projection) {
26
25
  fieldName = field.name;
27
26
  k = fieldName.toLowerCase();
28
27
  projectionKeysSet.delete(k);
28
+ /** Ignore if field is not in scope */
29
+ if (!field.inScope(scopes))
30
+ continue;
29
31
  const p = projection?.[k];
30
32
  if (
31
33
  /** Ignore if field is omitted */
@@ -88,8 +88,14 @@ export class MongoService extends ServiceBase {
88
88
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
89
89
  */
90
90
  get dataType() {
91
+ if (this._dataType && this._dataTypeScope !== this.scopes)
92
+ this._dataType = undefined;
91
93
  if (!this._dataType)
92
94
  this._dataType = this.context.documentNode.getComplexType(this._dataType_);
95
+ this._dataTypeScope = this.scopes;
96
+ this._dataTypeScopes = this.scopes
97
+ ? this.scopes?.split(/\s*,\s*/)
98
+ : undefined;
93
99
  return this._dataType;
94
100
  }
95
101
  /**
@@ -240,6 +246,7 @@ export class MongoService extends ServiceBase {
240
246
  if (validator)
241
247
  return validator;
242
248
  const options = { projection: '*' };
249
+ options.scope = this._dataTypeScopes;
243
250
  if (operation === 'update') {
244
251
  options.partial = 'deep';
245
252
  options.allowPatchOperators = true;
@@ -259,7 +266,7 @@ export class MongoService extends ServiceBase {
259
266
  const options = {
260
267
  projection: '*',
261
268
  partial: 'deep',
262
- ignoreHiddenFields: true,
269
+ scope: this._dataTypeScopes,
263
270
  };
264
271
  const dataType = this.dataType;
265
272
  validator = dataType.generateCodec('decode', options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.4.4",
3
+ "version": "1.5.0",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -10,9 +10,9 @@
10
10
  "valgen": "^5.12.0"
11
11
  },
12
12
  "peerDependencies": {
13
- "@opra/common": "^1.4.4",
14
- "@opra/core": "^1.4.4",
15
- "@opra/http": "^1.4.4",
13
+ "@opra/common": "^1.5.0",
14
+ "@opra/core": "^1.5.0",
15
+ "@opra/http": "^1.5.0",
16
16
  "mongodb": ">= 6.0.0"
17
17
  },
18
18
  "type": "module",
@@ -1,4 +1,4 @@
1
1
  import { ComplexType, FieldsProjection } from '@opra/common';
2
2
  import mongodb, { type Document } from 'mongodb';
3
- export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document | '*'): mongodb.Document | undefined;
4
- export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection): void;
3
+ export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document | '*', scopes?: string[]): mongodb.Document | undefined;
4
+ export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection, scopes?: string[]): void;
@@ -152,10 +152,16 @@ export interface MongoService {
152
152
  * @template T - The type of the documents in the collection.
153
153
  */
154
154
  export declare class MongoService<T extends mongodb.Document = mongodb.Document> extends ServiceBase {
155
+ protected _dataTypeScope?: string;
156
+ protected _dataTypeScopes?: string[];
155
157
  protected _dataType_: Type | string;
156
158
  protected _dataType?: ComplexType;
157
159
  protected _inputCodecs: Record<string, IsObject.Validator<T>>;
158
160
  protected _outputCodecs: Record<string, IsObject.Validator<T>>;
161
+ /**
162
+ * Defines comma delimited scopes for api document
163
+ */
164
+ scopes?: string;
159
165
  /**
160
166
  * Represents the name of a collection in MongoDB
161
167
  */