unconventional 1.9.1 → 1.10.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.
@@ -37,6 +37,8 @@ export declare class BaseModel extends Document {
37
37
  private static addDefaults;
38
38
  /** Gets the field name to use for the provided identifier */
39
39
  private static getFieldName;
40
+ /** Masks private fields and in the provided data and its relations */
41
+ static maskPrivate<T extends typeof BaseModel>(this: T, data: InstanceType<T> | InstanceType<T>[], relationsOnly?: boolean): void;
40
42
  $id(): any;
41
43
  $key(): any;
42
44
  $owner(): any;
@@ -204,6 +204,25 @@ class BaseModel extends Document {
204
204
  ? this.idField
205
205
  : this.keyField || undefined;
206
206
  }
207
+ /** Masks private fields and in the provided data and its relations */
208
+ static maskPrivate(data, relationsOnly = false) {
209
+ if (!data || typeof data !== 'object')
210
+ return;
211
+ const dataArray = Array.isArray(data) ? data : [data];
212
+ const propSummary = Object.entries(this.schema.props);
213
+ for (const item of dataArray) {
214
+ for (const [key, options] of propSummary) {
215
+ // Mask private fields. Skip 1st level if relationsOnly.
216
+ if (!relationsOnly && options.private && key in item) {
217
+ item[key] = undefined;
218
+ }
219
+ // Mask private fields in relations
220
+ if (options.relation && item[key]) {
221
+ (0, utils_1.getRelationModel)(options.relation).maskPrivate(item[key]);
222
+ }
223
+ }
224
+ }
225
+ }
207
226
  $id() { return this[this.constructor['idField']]; }
208
227
  $key() { return this[this.constructor['keyField']]; }
209
228
  $owner() { return this[this.constructor['ownerField']]; }
@@ -1,4 +1,6 @@
1
1
  import { AndOr } from "unconventional-pg-queries";
2
+ import { Relation } from "../types";
3
+ import { BaseModel } from "../core/base.model";
2
4
  /**
3
5
  * Decompose an expand-formatted string into an array of its parts.
4
6
  * @example
@@ -23,3 +25,5 @@ export declare function replaceFilter(filter: string | string[] | undefined, tar
23
25
  export declare function hasFilter(filter: string | string[] | undefined, target: string): boolean;
24
26
  /** Check whether a string is UUID */
25
27
  export declare function isUUID(str: string): boolean;
28
+ /** Get the model class of a relation definition */
29
+ export declare function getRelationModel<T extends typeof BaseModel>(relation: Relation<T>): T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isUUID = exports.hasFilter = exports.replaceFilter = exports.splitExpandUnit = exports.parseExpandString = void 0;
3
+ exports.getRelationModel = exports.isUUID = exports.hasFilter = exports.replaceFilter = exports.splitExpandUnit = exports.parseExpandString = void 0;
4
4
  const unconventional_pg_queries_1 = require("unconventional-pg-queries");
5
5
  const db_types_1 = require("../types/db.types");
6
6
  const defaultTarget = ',';
@@ -91,3 +91,8 @@ function isUUID(str) {
91
91
  return true;
92
92
  }
93
93
  exports.isUUID = isUUID;
94
+ /** Get the model class of a relation definition */
95
+ function getRelationModel(relation) {
96
+ return (relation.model.toString().startsWith('class')) ? relation.model : relation.model();
97
+ }
98
+ exports.getRelationModel = getRelationModel;
@@ -19,7 +19,7 @@ function getExpand(model, expand) {
19
19
  const relation = propSummary[parent]?.relation;
20
20
  if (!relation)
21
21
  throw api_error_1.APIError.errInvalidQueryParameter(`Invalid expansion: ${parent}`);
22
- const relationModel = (relation.model.toString().startsWith('class')) ? relation.model : relation.model();
22
+ const relationModel = (0, api_utils_1.getRelationModel)(relation);
23
23
  expansions[parent] = {
24
24
  type: [decorator_types_1.RelationType.HasOne, decorator_types_1.RelationType.BelongsTo].includes(relation.type) ? unconventional_pg_queries_1.OneOrMany.One : unconventional_pg_queries_1.OneOrMany.Many,
25
25
  fromTable: model.collection,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",