unconventional 1.11.0 → 1.12.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.
@@ -210,11 +210,19 @@ class BaseModel extends Document {
210
210
  return;
211
211
  const dataArray = Array.isArray(data) ? data : [data];
212
212
  const propSummary = Object.entries(this.schema.props);
213
- for (const item of dataArray) {
213
+ for (let item of dataArray) {
214
214
  for (const [key, options] of propSummary) {
215
215
  // Mask private fields. Skip 1st level if relationsOnly.
216
- if (!relationsOnly && options.private && key in item) {
217
- item[key] = undefined;
216
+ if (!relationsOnly && key in item && options.private != null) {
217
+ if (Array.isArray(item[key])) {
218
+ item[key] = item[key].filter(rel => !(0, utils_1.evaluate)(options.private, item, rel));
219
+ if (item[key].length === 0)
220
+ continue;
221
+ }
222
+ else if ((0, utils_1.evaluate)(options.private, item, item[key])) {
223
+ item[key] = undefined;
224
+ continue;
225
+ }
218
226
  }
219
227
  // Mask private fields in relations
220
228
  if (options.relation && item[key]) {
@@ -63,9 +63,9 @@ export interface PropOptions<T = any, S extends typeof BaseModel = any, P extend
63
63
  /** Give the Property a default Value */
64
64
  default?: T | (() => T);
65
65
  /** Protect field from edits by non-privileged users */
66
- system?: boolean;
66
+ system?: boolean | ((model: InstanceType<S>, field: T extends typeof BaseModel ? InstanceType<T> : T) => boolean);
67
67
  /** Hide field in responses to non-privileged users */
68
- private?: boolean;
68
+ private?: boolean | ((model: InstanceType<S>, field: T extends typeof BaseModel ? InstanceType<T> : T) => boolean);
69
69
  /** Relational field data */
70
70
  relation?: T extends typeof BaseModel ? Relation<T, S, P> : never;
71
71
  }
@@ -2,3 +2,5 @@
2
2
  export declare const equalArrays: <T>(a: T[], b: T[]) => boolean;
3
3
  /** Checks whether an object has no keys and/or the value for every key is undefined */
4
4
  export declare function isEmpty(obj: any): boolean;
5
+ /** Evaluate a direct or functional value */
6
+ export declare function evaluate<T, F extends (...args: any[]) => T>(value: T | F | undefined, ...args: Parameters<F>): T | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isEmpty = exports.equalArrays = void 0;
3
+ exports.evaluate = exports.isEmpty = exports.equalArrays = void 0;
4
4
  /** Compares two arrays and returns true if they contain the same elements, regardless of order */
5
5
  const equalArrays = (a, b) => {
6
6
  if (a.length !== b.length)
@@ -26,3 +26,8 @@ function isEmpty(obj) {
26
26
  return true;
27
27
  }
28
28
  exports.isEmpty = isEmpty;
29
+ /** Evaluate a direct or functional value */
30
+ function evaluate(value, ...args) {
31
+ return value instanceof Function ? value(...args) : value;
32
+ }
33
+ exports.evaluate = evaluate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -39,9 +39,9 @@
39
39
  "docs:build": "vitepress build docs",
40
40
  "docs:preview": "vitepress preview docs",
41
41
  "docs:serve": "vitepress serve docs",
42
- "release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags",
43
- "release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags",
44
- "release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags"
42
+ "release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major",
43
+ "release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor",
44
+ "release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "hono": "^4",