unconventional 1.17.0 → 2.0.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.
@@ -9,12 +9,17 @@ interface BaseModelSchema {
9
9
  updatedField: string;
10
10
  };
11
11
  }
12
+ export declare enum IdType {
13
+ Number = "number",
14
+ UUID = "uuid"
15
+ }
12
16
  export declare class Document {
13
17
  static schema: BaseModelSchema;
14
18
  }
15
19
  export declare class BaseModel extends Document {
16
20
  static collection: string;
17
21
  static idField: string;
22
+ static idType: IdType;
18
23
  static keyField?: string;
19
24
  static ownerField?: string;
20
25
  static db: DB;
@@ -34,7 +39,7 @@ export declare class BaseModel extends Document {
34
39
  }): Promise<InstanceType<T> | null>;
35
40
  static truncate<T extends typeof BaseModel>(this: T): Promise<void>;
36
41
  static fromDatabaseJson<T extends typeof BaseModel>(this: T, json: object): InstanceType<T>;
37
- private static addDefaults;
42
+ private static prepareData;
38
43
  /** Gets the field name to use for the provided identifier */
39
44
  private static getFieldName;
40
45
  /** Masks private fields and in the provided data and its relations */
@@ -9,13 +9,18 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.TimeStamped = exports.BaseModel = exports.Document = void 0;
12
+ exports.TimeStamped = exports.BaseModel = exports.Document = exports.IdType = void 0;
13
13
  const unconventional_pg_queries_1 = require("unconventional-pg-queries");
14
14
  const prop_decorator_1 = require("../decorators/prop.decorator");
15
15
  const timestamp_decorator_1 = require("../decorators/timestamp.decorator");
16
16
  const db_types_1 = require("../types/db.types");
17
17
  const db_utils_1 = require("../utils/db.utils");
18
18
  const utils_1 = require("../utils");
19
+ var IdType;
20
+ (function (IdType) {
21
+ IdType["Number"] = "number";
22
+ IdType["UUID"] = "uuid";
23
+ })(IdType || (exports.IdType = IdType = {}));
19
24
  class Document {
20
25
  static schema = { indexes: [], props: {} };
21
26
  }
@@ -23,6 +28,7 @@ exports.Document = Document;
23
28
  class BaseModel extends Document {
24
29
  static collection;
25
30
  static idField = 'id';
31
+ static idType = IdType.Number;
26
32
  static keyField;
27
33
  static ownerField;
28
34
  static db;
@@ -37,7 +43,7 @@ class BaseModel extends Document {
37
43
  type: unconventional_pg_queries_1.OneOrMany.One,
38
44
  table: this.collection,
39
45
  conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
40
- data: this.addDefaults({ ...data, ...timestamp })
46
+ data: this.prepareData({ ...data, ...timestamp })
41
47
  });
42
48
  if (!response)
43
49
  return null;
@@ -54,7 +60,7 @@ class BaseModel extends Document {
54
60
  type: unconventional_pg_queries_1.OneOrMany.Many,
55
61
  table: this.collection,
56
62
  conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
57
- data: data.map(item => this.addDefaults({ ...item, ...timestamp }))
63
+ data: data.map(item => this.prepareData({ ...item, ...timestamp }))
58
64
  });
59
65
  if (!response)
60
66
  return null;
@@ -108,7 +114,7 @@ class BaseModel extends Document {
108
114
  type: unconventional_pg_queries_1.OneOrMany.One,
109
115
  table: this.collection,
110
116
  where: [{ field: targetField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }],
111
- data: { ...data, ...timestamp }
117
+ data: this.prepareData({ ...data, ...timestamp }, false)
112
118
  });
113
119
  if (!response)
114
120
  return null;
@@ -121,7 +127,7 @@ class BaseModel extends Document {
121
127
  action: db_types_1.SqlAction.Update,
122
128
  type: unconventional_pg_queries_1.OneOrMany.Many,
123
129
  table: this.collection,
124
- data: data.map(item => ({ ...item, ...timestamp }))
130
+ data: data.map(item => this.prepareData({ ...item, ...timestamp }, false))
125
131
  });
126
132
  if (!response)
127
133
  return null;
@@ -196,18 +202,23 @@ class BaseModel extends Document {
196
202
  Object.assign(model, json);
197
203
  return model;
198
204
  }
199
- static addDefaults(data) {
205
+ static prepareData(data, applyDefaults = true) {
200
206
  Object.entries(this.schema.props).forEach(([key, options]) => {
201
- if (data[key] === undefined && options.default != null) {
207
+ // Apply default
208
+ if (applyDefaults && data[key] === undefined && options.default != null) {
202
209
  const val = options.default;
203
210
  data[key] = typeof val === "function" ? val() : val;
204
211
  }
212
+ // Apply preFormat
213
+ if (data[key] !== undefined && options.preFormat != null) {
214
+ data[key] = options.preFormat(data[key]);
215
+ }
205
216
  });
206
217
  return data;
207
218
  }
208
219
  /** Gets the field name to use for the provided identifier */
209
220
  static getFieldName(identifier) {
210
- return !isNaN(+identifier) || (0, utils_1.isUUID)(`${identifier}`)
221
+ return (this.idType === IdType.Number && !isNaN(+identifier)) || (this.idType === IdType.UUID && (0, utils_1.isUUID)(`${identifier}`))
211
222
  ? this.idField
212
223
  : this.keyField || undefined;
213
224
  }
@@ -4,6 +4,7 @@ exports.BaseService = void 0;
4
4
  const unconventional_pg_queries_1 = require("unconventional-pg-queries");
5
5
  const api_error_1 = require("./api-error");
6
6
  const base_cache_1 = require("./base.cache");
7
+ const base_model_1 = require("./base.model");
7
8
  const db_utils_1 = require("../utils/db.utils");
8
9
  const utils_1 = require("../utils");
9
10
  class BaseService {
@@ -61,7 +62,7 @@ class BaseService {
61
62
  entity = await base_cache_1.BaseCache.getModel(cache, this.model, identifier, modifiers);
62
63
  }
63
64
  if (!entity) {
64
- if (!isNaN(+identifier) || (0, utils_1.isUUID)(`${identifier}`)) {
65
+ if ((this.model.idType === base_model_1.IdType.Number && !isNaN(+identifier)) || (this.model.idType === base_model_1.IdType.UUID && (0, utils_1.isUUID)(`${identifier}`))) {
65
66
  entity = await this.model.findById(identifier, modifiers);
66
67
  }
67
68
  else {
@@ -68,6 +68,8 @@ export interface PropOptions<T = any, S extends typeof BaseModel = any, P extend
68
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
+ /** Format the field value before it is stored in the database */
72
+ preFormat?: (fieldValue: T extends typeof BaseModel ? InstanceType<T> : T) => any;
71
73
  }
72
74
  /** Type for the Values stored in the Reflection for Properties */
73
75
  export type PropSummary<Target = any, Source extends typeof BaseModel = any> = Record<keyof Source, PropOptions<Target, Source>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "1.17.0",
3
+ "version": "2.0.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",