framework-do-dede 1.0.32 → 1.0.33
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.
- package/dist/decorators/entity.d.ts +4 -5
- package/dist/decorators/entity.js +17 -17
- package/dist/domain/Entity.d.ts +0 -1
- package/dist/domain/Entity.js +23 -25
- package/package.json +1 -1
|
@@ -5,9 +5,8 @@ export type ExposeConfig = {
|
|
|
5
5
|
};
|
|
6
6
|
export declare function Expose(configOrMapping: ExposeConfig | string): PropertyDecorator;
|
|
7
7
|
export declare function VirtualProperty(propertyName: string): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
|
|
8
|
-
type DbColumnConfig = {
|
|
9
|
-
|
|
10
|
-
serialize?: (value:
|
|
8
|
+
export type DbColumnConfig<T = any> = {
|
|
9
|
+
column?: string | Record<string, string>;
|
|
10
|
+
serialize?: (value: T) => any | Promise<any>;
|
|
11
11
|
};
|
|
12
|
-
export declare function DbColumn(
|
|
13
|
-
export {};
|
|
12
|
+
export declare function DbColumn<T>(configOrColumn: DbColumnConfig<T> | string): PropertyDecorator;
|
|
@@ -39,27 +39,27 @@ export function VirtualProperty(propertyName) {
|
|
|
39
39
|
ctor._virtualProperties.set(propertyName, methodName);
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
export function DbColumn(
|
|
42
|
+
export function DbColumn(configOrColumn) {
|
|
43
43
|
return function (target, propertyKey) {
|
|
44
44
|
const ctor = target.constructor;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
serialize = config.serialize;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
actualMapping = config;
|
|
59
|
-
}
|
|
45
|
+
const configs = ctor._dbColumnConfigs || (ctor._dbColumnConfigs = new Map());
|
|
46
|
+
if (typeof configOrColumn === "string") {
|
|
47
|
+
configs.set(propertyKey, [
|
|
48
|
+
...(configs.get(propertyKey) || []),
|
|
49
|
+
{
|
|
50
|
+
column: configOrColumn,
|
|
51
|
+
serialize: (value) => value
|
|
52
|
+
}
|
|
53
|
+
]);
|
|
60
54
|
}
|
|
61
55
|
else {
|
|
62
|
-
|
|
56
|
+
configs.set(propertyKey, [
|
|
57
|
+
...(configs.get(propertyKey) || []),
|
|
58
|
+
{
|
|
59
|
+
column: configOrColumn.column,
|
|
60
|
+
serialize: configOrColumn.serialize || ((value) => value)
|
|
61
|
+
}
|
|
62
|
+
]);
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
65
|
}
|
package/dist/domain/Entity.d.ts
CHANGED
package/dist/domain/Entity.js
CHANGED
|
@@ -52,35 +52,34 @@ export class Entity {
|
|
|
52
52
|
return attributes;
|
|
53
53
|
}
|
|
54
54
|
async toEntity() {
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
for (const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (subValue !== undefined) {
|
|
72
|
-
result[column] = subValue;
|
|
55
|
+
const ctor = this.constructor;
|
|
56
|
+
const dbColumnConfigs = ctor._dbColumnConfigs || new Map();
|
|
57
|
+
const dbRecord = {};
|
|
58
|
+
for (const [propertyKey, configs] of dbColumnConfigs) {
|
|
59
|
+
const rawValue = this[propertyKey];
|
|
60
|
+
for (const config of configs) {
|
|
61
|
+
try {
|
|
62
|
+
let serializedValue = config.serialize ? await config.serialize(rawValue) : rawValue;
|
|
63
|
+
if (config.column) {
|
|
64
|
+
if (typeof config.column === "string") {
|
|
65
|
+
dbRecord[config.column] = serializedValue;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
for (const [srcKey, destKey] of Object.entries(config.column)) {
|
|
69
|
+
dbRecord[destKey] = serializedValue[srcKey];
|
|
70
|
+
}
|
|
73
71
|
}
|
|
74
72
|
}
|
|
73
|
+
else {
|
|
74
|
+
dbRecord[propertyKey] = serializedValue;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error(`Error in @DbColumn for ${String(propertyKey)}:`, error);
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
|
-
|
|
79
|
-
if (!processedKeys.has(key) && value !== undefined) {
|
|
80
|
-
result[key] = value;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return result;
|
|
82
|
+
return dbRecord;
|
|
84
83
|
}
|
|
85
84
|
async toMap(properties) {
|
|
86
85
|
const ctor = this.constructor;
|
|
@@ -123,5 +122,4 @@ export class Entity {
|
|
|
123
122
|
}
|
|
124
123
|
return attributes;
|
|
125
124
|
}
|
|
126
|
-
async beforeSave() { }
|
|
127
125
|
}
|