framework-do-dede 1.0.29 → 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.
@@ -2,28 +2,27 @@ import { Registry } from "../di/registry";
2
2
  import { StorageGateway } from "../protocols/StorageGateway";
3
3
  export function Storage(gatewayName) {
4
4
  return function (target, propertyKey) {
5
- Registry.whenLoaded(() => {
6
- if (!Registry.has(gatewayName)) {
7
- throw new Error(`StorageGateway ${gatewayName} not registered`);
8
- }
9
- const GatewayClass = Registry.resolve(gatewayName);
10
- if (!(GatewayClass instanceof StorageGateway)) {
11
- throw new Error(`${gatewayName} is not a valid StorageGateway`);
12
- }
13
- const instanceSymbol = Symbol();
14
- Object.defineProperty(target, propertyKey, {
15
- get: function () {
16
- if (!this[instanceSymbol]) {
17
- this[instanceSymbol] = GatewayClass;
5
+ const instanceSymbol = Symbol();
6
+ Object.defineProperty(target, propertyKey, {
7
+ get: function () {
8
+ if (!this[instanceSymbol]) {
9
+ // Lazy load the gateway when the property is first accessed
10
+ if (!Registry.has(gatewayName)) {
11
+ throw new Error(`StorageGateway ${gatewayName} not registered`);
18
12
  }
19
- return this[instanceSymbol];
20
- },
21
- set: () => {
22
- throw new Error('Cannot assign new value to @Storage() property');
23
- },
24
- enumerable: true,
25
- configurable: true
26
- });
13
+ const GatewayClass = Registry.resolve(gatewayName);
14
+ if (!(GatewayClass instanceof StorageGateway)) {
15
+ throw new Error(`${gatewayName} is not a valid StorageGateway`);
16
+ }
17
+ this[instanceSymbol] = GatewayClass;
18
+ }
19
+ return this[instanceSymbol];
20
+ },
21
+ set: () => {
22
+ throw new Error('Cannot assign new value to @Storage() property');
23
+ },
24
+ enumerable: true,
25
+ configurable: true
27
26
  });
28
27
  };
29
28
  }
@@ -12,7 +12,6 @@ declare class ComponentRegistry {
12
12
  classLoader<T>(target: new (...args: any[]) => T): T;
13
13
  inject(token: string): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
14
14
  loaded(): void;
15
- whenLoaded(callback: () => void): void;
16
15
  }
17
16
  export declare const Registry: ComponentRegistry;
18
17
  export {};
@@ -47,11 +47,5 @@ class ComponentRegistry {
47
47
  loaded() {
48
48
  this.isLoading = false;
49
49
  }
50
- whenLoaded(callback) {
51
- while (this.isLoading) {
52
- setTimeout(callback, 100);
53
- }
54
- callback();
55
- }
56
50
  }
57
51
  export const Registry = ComponentRegistry.getInstance();
@@ -1,6 +1,6 @@
1
1
  export declare abstract class Entity {
2
2
  get(): Promise<Record<string, any>>;
3
- toSave(): Promise<Record<string, any>>;
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
  }
@@ -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 toSave() {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",