framework-do-dede 1.0.30 → 1.0.31
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/domain/Entity.d.ts +2 -2
- package/dist/domain/Entity.js +38 -3
- package/package.json +1 -1
package/dist/domain/Entity.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare abstract class Entity {
|
|
2
2
|
get(): Promise<Record<string, any>>;
|
|
3
|
-
|
|
3
|
+
toEntity(): Promise<Record<string, any>>;
|
|
4
|
+
toMap(properties: string[]): Promise<Record<string, any>>;
|
|
4
5
|
protected beforeSave(): Promise<void>;
|
|
5
|
-
protected beforeGet(): Promise<void>;
|
|
6
6
|
}
|
package/dist/domain/Entity.js
CHANGED
|
@@ -5,7 +5,6 @@ export class Entity {
|
|
|
5
5
|
const virtualProperties = ctor._virtualProperties || new Map();
|
|
6
6
|
const exposeConfigs = ctor._exposeConfigs || new Map();
|
|
7
7
|
const attributes = {};
|
|
8
|
-
await this.beforeGet();
|
|
9
8
|
for (const [key, value] of Object.entries(this)) {
|
|
10
9
|
if (!restrictedProps.has(key)) {
|
|
11
10
|
attributes[key] = value;
|
|
@@ -52,7 +51,7 @@ export class Entity {
|
|
|
52
51
|
}
|
|
53
52
|
return attributes;
|
|
54
53
|
}
|
|
55
|
-
async
|
|
54
|
+
async toEntity() {
|
|
56
55
|
await this.beforeSave();
|
|
57
56
|
const result = {};
|
|
58
57
|
const processedKeys = new Set();
|
|
@@ -83,6 +82,42 @@ export class Entity {
|
|
|
83
82
|
}
|
|
84
83
|
return result;
|
|
85
84
|
}
|
|
85
|
+
async toMap(properties) {
|
|
86
|
+
const ctor = this.constructor;
|
|
87
|
+
const exposeConfigs = ctor._exposeConfigs || new Map();
|
|
88
|
+
const attributes = {};
|
|
89
|
+
for (const property of properties) {
|
|
90
|
+
if (exposeConfigs.has(property)) {
|
|
91
|
+
const rawValue = this[property];
|
|
92
|
+
const config = exposeConfigs.get(property);
|
|
93
|
+
try {
|
|
94
|
+
let value = config.deserialize ? await config.deserialize(rawValue) : rawValue;
|
|
95
|
+
if (config.mapping) {
|
|
96
|
+
if (typeof config.mapping === "string") {
|
|
97
|
+
attributes[config.mapping] = value;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
Object.entries(config.mapping).forEach(([srcKey, destKey]) => {
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
attributes[destKey] = value[srcKey];
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (typeof value === "object" && value !== null) {
|
|
108
|
+
Object.assign(attributes, value);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
attributes[property] = value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error(`Error in @Expose for ${String(property)}:`, error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return attributes;
|
|
121
|
+
}
|
|
86
122
|
async beforeSave() { }
|
|
87
|
-
async beforeGet() { }
|
|
88
123
|
}
|