framework-do-dede 5.2.2 → 5.3.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
- constructor(input?: Record<string, any> | Entity);
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,18 +1,12 @@
1
- import { Entity } from "../../application";
2
1
  export class Model {
3
- constructor(input) {
4
- if (!input)
5
- return;
6
- let data = {};
7
- if (input instanceof Entity) {
8
- data = input.from();
9
- }
10
- else {
11
- data = input;
12
- }
13
- for (const [property, value] of Object.entries(data)) {
14
- 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;
15
8
  }
9
+ return this;
16
10
  }
17
11
  toModel() {
18
12
  const record = {};
@@ -1,11 +1,10 @@
1
1
  import { Entity as DomainEntity } from "../../domain/entity";
2
2
  export declare abstract class Entity extends DomainEntity {
3
3
  [x: string]: any;
4
- from(): Record<string, any>;
5
- to(transform?: boolean): Record<string, any>;
4
+ data(transform?: boolean): Promise<Record<string, any>>;
6
5
  protected generateGetters(): void;
7
6
  }
8
7
  export declare function Restrict(): (target: any, propertyKey: string) => void;
9
8
  export declare function VirtualProperty(propertyName: string): (target: any, methodName: string) => void;
10
- export declare function Transform(callback: (value: any) => any): PropertyDecorator;
9
+ export declare function Transform(callback: (value: any) => any | Promise<any>): PropertyDecorator;
11
10
  export declare function GetterPrefix(prefix: string): (target: any, propertyKey: string) => void;
@@ -1,40 +1,6 @@
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
- to(transform = true) {
3
+ async data(transform = true) {
38
4
  // @ts-ignore
39
5
  const propertiesConfigs = this.constructor.propertiesConfigs;
40
6
  // @ts-ignore
@@ -48,7 +14,7 @@ export class Entity extends DomainEntity {
48
14
  // @ts-ignore
49
15
  let value = this[propName];
50
16
  if (transform && propertiesConfigs && propertiesConfigs[propName]?.transform && value) {
51
- value = propertiesConfigs[propName].transform(value);
17
+ value = await propertiesConfigs[propName].transform(value);
52
18
  }
53
19
  result[propName] = value;
54
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "5.2.2",
3
+ "version": "5.3.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",