unconventional 1.17.0 → 1.18.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.
|
@@ -34,7 +34,7 @@ export declare class BaseModel extends Document {
|
|
|
34
34
|
}): Promise<InstanceType<T> | null>;
|
|
35
35
|
static truncate<T extends typeof BaseModel>(this: T): Promise<void>;
|
|
36
36
|
static fromDatabaseJson<T extends typeof BaseModel>(this: T, json: object): InstanceType<T>;
|
|
37
|
-
private static
|
|
37
|
+
private static prepareData;
|
|
38
38
|
/** Gets the field name to use for the provided identifier */
|
|
39
39
|
private static getFieldName;
|
|
40
40
|
/** Masks private fields and in the provided data and its relations */
|
package/dist/core/base.model.js
CHANGED
|
@@ -37,7 +37,7 @@ class BaseModel extends Document {
|
|
|
37
37
|
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
38
38
|
table: this.collection,
|
|
39
39
|
conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
40
|
-
data: this.
|
|
40
|
+
data: this.prepareData({ ...data, ...timestamp })
|
|
41
41
|
});
|
|
42
42
|
if (!response)
|
|
43
43
|
return null;
|
|
@@ -54,7 +54,7 @@ class BaseModel extends Document {
|
|
|
54
54
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
55
55
|
table: this.collection,
|
|
56
56
|
conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
57
|
-
data: data.map(item => this.
|
|
57
|
+
data: data.map(item => this.prepareData({ ...item, ...timestamp }))
|
|
58
58
|
});
|
|
59
59
|
if (!response)
|
|
60
60
|
return null;
|
|
@@ -108,7 +108,7 @@ class BaseModel extends Document {
|
|
|
108
108
|
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
109
109
|
table: this.collection,
|
|
110
110
|
where: [{ field: targetField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }],
|
|
111
|
-
data: { ...data, ...timestamp }
|
|
111
|
+
data: this.prepareData({ ...data, ...timestamp }, false)
|
|
112
112
|
});
|
|
113
113
|
if (!response)
|
|
114
114
|
return null;
|
|
@@ -121,7 +121,7 @@ class BaseModel extends Document {
|
|
|
121
121
|
action: db_types_1.SqlAction.Update,
|
|
122
122
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
123
123
|
table: this.collection,
|
|
124
|
-
data: data.map(item => ({ ...item, ...timestamp }))
|
|
124
|
+
data: data.map(item => this.prepareData({ ...item, ...timestamp }, false))
|
|
125
125
|
});
|
|
126
126
|
if (!response)
|
|
127
127
|
return null;
|
|
@@ -196,12 +196,17 @@ class BaseModel extends Document {
|
|
|
196
196
|
Object.assign(model, json);
|
|
197
197
|
return model;
|
|
198
198
|
}
|
|
199
|
-
static
|
|
199
|
+
static prepareData(data, applyDefaults = true) {
|
|
200
200
|
Object.entries(this.schema.props).forEach(([key, options]) => {
|
|
201
|
-
|
|
201
|
+
// Apply default
|
|
202
|
+
if (applyDefaults && data[key] === undefined && options.default != null) {
|
|
202
203
|
const val = options.default;
|
|
203
204
|
data[key] = typeof val === "function" ? val() : val;
|
|
204
205
|
}
|
|
206
|
+
// Apply preFormat
|
|
207
|
+
if (data[key] !== undefined && options.preFormat != null) {
|
|
208
|
+
data[key] = options.preFormat(data[key]);
|
|
209
|
+
}
|
|
205
210
|
});
|
|
206
211
|
return data;
|
|
207
212
|
}
|
|
@@ -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>>;
|