framework-do-dede 5.2.1 → 5.2.3
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.
|
@@ -7,7 +7,8 @@ export declare abstract class Model<TTable = string> {
|
|
|
7
7
|
table: TTable;
|
|
8
8
|
columns: ColumnDefinition[];
|
|
9
9
|
[property: string]: any;
|
|
10
|
-
|
|
10
|
+
fromModel(input: Record<string, any>): Model;
|
|
11
|
+
abstract fromEntity(entity: Entity): Model;
|
|
11
12
|
toModel(): Record<string, any>;
|
|
12
13
|
abstract toEntity(): Entity;
|
|
13
14
|
}
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import { Entity } from "../../application";
|
|
2
1
|
export class Model {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
data = input;
|
|
12
|
-
for (const [property, value] of Object.entries(data)) {
|
|
13
|
-
this[property] = value;
|
|
2
|
+
fromModel(input) {
|
|
3
|
+
const columns = this.columns ?? [];
|
|
4
|
+
const columnByName = new Map(columns.map((column) => [column.column, column.property]));
|
|
5
|
+
for (const [property, value] of Object.entries(input)) {
|
|
6
|
+
const mappedProperty = columnByName.get(property);
|
|
7
|
+
this[mappedProperty ?? property] = value;
|
|
14
8
|
}
|
|
9
|
+
return this;
|
|
15
10
|
}
|
|
16
11
|
toModel() {
|
|
17
12
|
const record = {};
|
|
@@ -1,39 +1,5 @@
|
|
|
1
1
|
import { Entity as DomainEntity } from "../../domain/entity";
|
|
2
2
|
export class Entity extends DomainEntity {
|
|
3
|
-
from() {
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
const propertiesConfigs = this.constructor.propertiesConfigs;
|
|
6
|
-
const result = {};
|
|
7
|
-
for (const [propName] of Object.entries(this)) {
|
|
8
|
-
let propertyName = propName;
|
|
9
|
-
let value = this[propName];
|
|
10
|
-
if (typeof value === 'function')
|
|
11
|
-
continue;
|
|
12
|
-
if (value === undefined)
|
|
13
|
-
continue;
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
if (propertiesConfigs && propertiesConfigs[propName]?.transform && value) {
|
|
16
|
-
const transformedValue = propertiesConfigs[propName].transform(value);
|
|
17
|
-
if (transformedValue && typeof transformedValue === 'object' && !Array.isArray(transformedValue)) {
|
|
18
|
-
const entries = Object.entries(transformedValue);
|
|
19
|
-
for (const [transformedKey, transformedPropValue] of entries) {
|
|
20
|
-
let currentValue = transformedPropValue;
|
|
21
|
-
if (!currentValue)
|
|
22
|
-
currentValue = null;
|
|
23
|
-
result[transformedKey] = currentValue;
|
|
24
|
-
}
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
value = transformedValue;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (value === undefined || value === null)
|
|
32
|
-
value = null;
|
|
33
|
-
result[propertyName] = value;
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
3
|
to(transform = true) {
|
|
38
4
|
// @ts-ignore
|
|
39
5
|
const propertiesConfigs = this.constructor.propertiesConfigs;
|