framework-do-dede 1.0.31 → 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.
@@ -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
- mapping?: string | Record<string, string>;
10
- serialize?: (value: any) => any | Promise<any>;
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(config: string | Record<string, string> | DbColumnConfig): (target: Object, propertyKey: string) => void;
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(config) {
42
+ export function DbColumn(configOrColumn) {
43
43
  return function (target, propertyKey) {
44
44
  const ctor = target.constructor;
45
- let actualMapping;
46
- let serialize;
47
- if (typeof config === 'string') {
48
- actualMapping = config;
49
- }
50
- else if (typeof config === 'object') {
51
- if ('serialize' in config || 'mapping' in config) {
52
- actualMapping = config.mapping ?? propertyKey;
53
- // @ts-ignore
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
- throw new Error('Configuração inválida para @DbColumn');
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
  }
@@ -2,5 +2,4 @@ export declare abstract class Entity {
2
2
  get(): Promise<Record<string, any>>;
3
3
  toEntity(): Promise<Record<string, any>>;
4
4
  toMap(properties: string[]): Promise<Record<string, any>>;
5
- protected beforeSave(): Promise<void>;
6
5
  }
@@ -52,41 +52,44 @@ export class Entity {
52
52
  return attributes;
53
53
  }
54
54
  async toEntity() {
55
- await this.beforeSave();
56
- const result = {};
57
- const processedKeys = new Set();
58
- const dbColumns = this.constructor._dbColumns;
59
- if (dbColumns) {
60
- for (const { property, mapping } of dbColumns) {
61
- const value = this[property];
62
- if (value === undefined)
63
- continue;
64
- processedKeys.add(property);
65
- if (typeof mapping === 'string') {
66
- result[mapping] = value;
67
- }
68
- else {
69
- for (const [subProp, column] of Object.entries(mapping)) {
70
- const subValue = value[subProp];
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
- for (const [key, value] of Object.entries(this)) {
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;
87
86
  const exposeConfigs = ctor._exposeConfigs || new Map();
88
87
  const attributes = {};
89
88
  for (const property of properties) {
89
+ const rawValue = this[property];
90
+ if (!rawValue)
91
+ continue;
92
+ attributes[property] = rawValue;
90
93
  if (exposeConfigs.has(property)) {
91
94
  const rawValue = this[property];
92
95
  const config = exposeConfigs.get(property);
@@ -119,5 +122,4 @@ export class Entity {
119
122
  }
120
123
  return attributes;
121
124
  }
122
- async beforeSave() { }
123
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",