@rtpaulino/entity 0.10.13
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/entity-utils.d.ts +35 -0
- package/dist/lib/entity-utils.d.ts.map +1 -0
- package/dist/lib/entity-utils.js +140 -0
- package/dist/lib/entity-utils.js.map +1 -0
- package/dist/lib/entity.d.ts +14 -0
- package/dist/lib/entity.d.ts.map +1 -0
- package/dist/lib/entity.js +21 -0
- package/dist/lib/entity.js.map +1 -0
- package/dist/lib/property.d.ts +21 -0
- package/dist/lib/property.d.ts.map +1 -0
- package/dist/lib/property.js +42 -0
- package/dist/lib/property.js.map +1 -0
- package/dist/lib/types.d.ts +25 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +11 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 rtpaulino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @rtpaulino/entity
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import 'reflect-metadata';\n\nexport * from './lib/entity.js';\nexport * from './lib/entity-utils.js';\nexport * from './lib/types.js';\nexport * from './lib/property.js';\n"],"names":[],"mappings":"AAAA,OAAO,mBAAmB;AAE1B,cAAc,kBAAkB;AAChC,cAAc,wBAAwB;AACtC,cAAc,iBAAiB;AAC/B,cAAc,oBAAoB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { PropertyOptions } from './types.js';
|
|
2
|
+
export declare class EntityUtils {
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a given object is an instance of a class decorated with @Entity()
|
|
5
|
+
* or if the provided value is an entity class itself
|
|
6
|
+
*
|
|
7
|
+
* @param obj - The object or class to check
|
|
8
|
+
* @returns true if the object is an entity instance or entity class, false otherwise
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* @Entity()
|
|
13
|
+
* class User {
|
|
14
|
+
* name: string;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* const user = new User();
|
|
18
|
+
* console.log(EntityUtils.isEntity(user)); // true
|
|
19
|
+
* console.log(EntityUtils.isEntity(User)); // true
|
|
20
|
+
* console.log(EntityUtils.isEntity({})); // false
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
static isEntity(obj: unknown): obj is object;
|
|
24
|
+
static sameEntity(a: object, b: object): boolean;
|
|
25
|
+
static getPropertyKeys(target: object): string[];
|
|
26
|
+
static getPropertyOptions(target: object, propertyKey: string): PropertyOptions | undefined;
|
|
27
|
+
static equals(a: unknown, b: unknown): boolean;
|
|
28
|
+
static diff<T extends object>(oldEntity: T, newEntity: T): {
|
|
29
|
+
property: string;
|
|
30
|
+
oldValue: unknown;
|
|
31
|
+
newValue: unknown;
|
|
32
|
+
}[];
|
|
33
|
+
static changes<T extends object>(oldEntity: T, newEntity: T): Partial<T>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=entity-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-utils.d.ts","sourceRoot":"","sources":["../../src/lib/entity-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,eAAe,EAChB,MAAM,YAAY,CAAC;AAGpB,qBAAa,WAAW;IACtB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM;IAmB5C,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO;IAQhD,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAoChD,MAAM,CAAC,kBAAkB,CACvB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAClB,eAAe,GAAG,SAAS;IA8B9B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO;IAe9C,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,MAAM,EAC1B,SAAS,EAAE,CAAC,EACZ,SAAS,EAAE,CAAC,GACX;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,EAAE;IA4B/D,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAYzE"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { ENTITY_METADATA_KEY, PROPERTY_METADATA_KEY, PROPERTY_OPTIONS_METADATA_KEY } from './types.js';
|
|
2
|
+
import { isEqualWith } from 'lodash-es';
|
|
3
|
+
export class EntityUtils {
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a given object is an instance of a class decorated with @Entity()
|
|
6
|
+
* or if the provided value is an entity class itself
|
|
7
|
+
*
|
|
8
|
+
* @param obj - The object or class to check
|
|
9
|
+
* @returns true if the object is an entity instance or entity class, false otherwise
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Entity()
|
|
14
|
+
* class User {
|
|
15
|
+
* name: string;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const user = new User();
|
|
19
|
+
* console.log(EntityUtils.isEntity(user)); // true
|
|
20
|
+
* console.log(EntityUtils.isEntity(User)); // true
|
|
21
|
+
* console.log(EntityUtils.isEntity({})); // false
|
|
22
|
+
* ```
|
|
23
|
+
*/ static isEntity(obj) {
|
|
24
|
+
if (obj == null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// Check if obj is a constructor function (class)
|
|
28
|
+
if (typeof obj === 'function') {
|
|
29
|
+
return Reflect.hasMetadata(ENTITY_METADATA_KEY, obj);
|
|
30
|
+
}
|
|
31
|
+
// Check if obj is an object instance
|
|
32
|
+
if (typeof obj !== 'object' || Array.isArray(obj)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const constructor = Object.getPrototypeOf(obj).constructor;
|
|
36
|
+
return Reflect.hasMetadata(ENTITY_METADATA_KEY, constructor);
|
|
37
|
+
}
|
|
38
|
+
static sameEntity(a, b) {
|
|
39
|
+
if (!this.isEntity(a) || !this.isEntity(b)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
|
|
43
|
+
}
|
|
44
|
+
static getPropertyKeys(target) {
|
|
45
|
+
// Determine if we're dealing with a prototype or an instance
|
|
46
|
+
let currentProto;
|
|
47
|
+
// Check if target is a prototype by checking if it has a constructor property
|
|
48
|
+
// and if target === target.constructor.prototype
|
|
49
|
+
if (target.constructor && target === target.constructor.prototype) {
|
|
50
|
+
// target is already a prototype
|
|
51
|
+
currentProto = target;
|
|
52
|
+
} else {
|
|
53
|
+
// target is an instance, get its prototype
|
|
54
|
+
currentProto = Object.getPrototypeOf(target);
|
|
55
|
+
}
|
|
56
|
+
const keys = [];
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
// Walk the prototype chain to collect all inherited properties
|
|
59
|
+
while(currentProto && currentProto !== Object.prototype){
|
|
60
|
+
// Use getOwnMetadata to only get metadata directly on this prototype
|
|
61
|
+
const protoKeys = Reflect.getOwnMetadata(PROPERTY_METADATA_KEY, currentProto) || [];
|
|
62
|
+
for (const key of protoKeys){
|
|
63
|
+
if (!seen.has(key)) {
|
|
64
|
+
seen.add(key);
|
|
65
|
+
keys.push(key);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
currentProto = Object.getPrototypeOf(currentProto);
|
|
69
|
+
}
|
|
70
|
+
return keys;
|
|
71
|
+
}
|
|
72
|
+
static getPropertyOptions(target, propertyKey) {
|
|
73
|
+
// Determine if we're dealing with a prototype or an instance
|
|
74
|
+
let currentProto;
|
|
75
|
+
// Check if target is a prototype by checking if it has a constructor property
|
|
76
|
+
// and if target === target.constructor.prototype
|
|
77
|
+
if (target.constructor && target === target.constructor.prototype) {
|
|
78
|
+
// target is already a prototype
|
|
79
|
+
currentProto = target;
|
|
80
|
+
} else {
|
|
81
|
+
// target is an instance, get its prototype
|
|
82
|
+
currentProto = Object.getPrototypeOf(target);
|
|
83
|
+
}
|
|
84
|
+
// Walk the prototype chain to find the property options
|
|
85
|
+
while(currentProto && currentProto !== Object.prototype){
|
|
86
|
+
const protoOptions = Reflect.getOwnMetadata(PROPERTY_OPTIONS_METADATA_KEY, currentProto) || {};
|
|
87
|
+
if (protoOptions[propertyKey]) {
|
|
88
|
+
return protoOptions[propertyKey];
|
|
89
|
+
}
|
|
90
|
+
currentProto = Object.getPrototypeOf(currentProto);
|
|
91
|
+
}
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
static equals(a, b) {
|
|
95
|
+
return isEqualWith(a, b, (val1, val2)=>{
|
|
96
|
+
if (this.isEntity(val1)) {
|
|
97
|
+
if (!this.sameEntity(val1, val2)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const diff = this.diff(val1, val2);
|
|
101
|
+
return diff.length === 0;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
static diff(oldEntity, newEntity) {
|
|
107
|
+
if (!this.sameEntity(oldEntity, newEntity)) {
|
|
108
|
+
throw new Error('Entities must be of the same type to compute diff');
|
|
109
|
+
}
|
|
110
|
+
const diffs = [];
|
|
111
|
+
const keys = this.getPropertyKeys(oldEntity);
|
|
112
|
+
for (const key of keys){
|
|
113
|
+
const oldValue = oldEntity[key];
|
|
114
|
+
const newValue = newEntity[key];
|
|
115
|
+
// Check if there's a custom equals function for this property
|
|
116
|
+
const propertyOptions = this.getPropertyOptions(oldEntity, key);
|
|
117
|
+
const areEqual = propertyOptions?.equals ? propertyOptions.equals(oldValue, newValue) : this.equals(oldValue, newValue);
|
|
118
|
+
if (!areEqual) {
|
|
119
|
+
diffs.push({
|
|
120
|
+
property: key,
|
|
121
|
+
oldValue,
|
|
122
|
+
newValue
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return diffs;
|
|
127
|
+
}
|
|
128
|
+
static changes(oldEntity, newEntity) {
|
|
129
|
+
if (!this.sameEntity(oldEntity, newEntity)) {
|
|
130
|
+
throw new Error('Entities must be of the same type to compute changes');
|
|
131
|
+
}
|
|
132
|
+
const diff = this.diff(oldEntity, newEntity);
|
|
133
|
+
return diff.reduce((acc, { property, newValue })=>{
|
|
134
|
+
acc[property] = newValue;
|
|
135
|
+
return acc;
|
|
136
|
+
}, {});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=entity-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/entity-utils.ts"],"sourcesContent":["import {\n ENTITY_METADATA_KEY,\n PROPERTY_METADATA_KEY,\n PROPERTY_OPTIONS_METADATA_KEY,\n PropertyOptions,\n} from './types.js';\nimport { isEqualWith } from 'lodash-es';\n\nexport class EntityUtils {\n /**\n * Checks if a given object is an instance of a class decorated with @Entity()\n * or if the provided value is an entity class itself\n *\n * @param obj - The object or class to check\n * @returns true if the object is an entity instance or entity class, false otherwise\n *\n * @example\n * ```typescript\n * @Entity()\n * class User {\n * name: string;\n * }\n *\n * const user = new User();\n * console.log(EntityUtils.isEntity(user)); // true\n * console.log(EntityUtils.isEntity(User)); // true\n * console.log(EntityUtils.isEntity({})); // false\n * ```\n */\n static isEntity(obj: unknown): obj is object {\n if (obj == null) {\n return false;\n }\n\n // Check if obj is a constructor function (class)\n if (typeof obj === 'function') {\n return Reflect.hasMetadata(ENTITY_METADATA_KEY, obj);\n }\n\n // Check if obj is an object instance\n if (typeof obj !== 'object' || Array.isArray(obj)) {\n return false;\n }\n\n const constructor = Object.getPrototypeOf(obj).constructor;\n return Reflect.hasMetadata(ENTITY_METADATA_KEY, constructor);\n }\n\n static sameEntity(a: object, b: object): boolean {\n if (!this.isEntity(a) || !this.isEntity(b)) {\n return false;\n }\n\n return Object.getPrototypeOf(a) === Object.getPrototypeOf(b);\n }\n\n static getPropertyKeys(target: object): string[] {\n // Determine if we're dealing with a prototype or an instance\n let currentProto: any;\n\n // Check if target is a prototype by checking if it has a constructor property\n // and if target === target.constructor.prototype\n if (target.constructor && target === target.constructor.prototype) {\n // target is already a prototype\n currentProto = target;\n } else {\n // target is an instance, get its prototype\n currentProto = Object.getPrototypeOf(target);\n }\n\n const keys: string[] = [];\n const seen = new Set<string>();\n\n // Walk the prototype chain to collect all inherited properties\n while (currentProto && currentProto !== Object.prototype) {\n // Use getOwnMetadata to only get metadata directly on this prototype\n const protoKeys: string[] =\n Reflect.getOwnMetadata(PROPERTY_METADATA_KEY, currentProto) || [];\n\n for (const key of protoKeys) {\n if (!seen.has(key)) {\n seen.add(key);\n keys.push(key);\n }\n }\n\n currentProto = Object.getPrototypeOf(currentProto);\n }\n\n return keys;\n }\n\n static getPropertyOptions(\n target: object,\n propertyKey: string,\n ): PropertyOptions | undefined {\n // Determine if we're dealing with a prototype or an instance\n let currentProto: any;\n\n // Check if target is a prototype by checking if it has a constructor property\n // and if target === target.constructor.prototype\n if (target.constructor && target === target.constructor.prototype) {\n // target is already a prototype\n currentProto = target;\n } else {\n // target is an instance, get its prototype\n currentProto = Object.getPrototypeOf(target);\n }\n\n // Walk the prototype chain to find the property options\n while (currentProto && currentProto !== Object.prototype) {\n const protoOptions: Record<string, PropertyOptions> =\n Reflect.getOwnMetadata(PROPERTY_OPTIONS_METADATA_KEY, currentProto) ||\n {};\n\n if (protoOptions[propertyKey]) {\n return protoOptions[propertyKey];\n }\n\n currentProto = Object.getPrototypeOf(currentProto);\n }\n\n return undefined;\n }\n\n static equals(a: unknown, b: unknown): boolean {\n return isEqualWith(a, b, (val1, val2) => {\n if (this.isEntity(val1)) {\n if (!this.sameEntity(val1, val2)) {\n return false;\n }\n\n const diff = this.diff(val1, val2);\n\n return diff.length === 0;\n }\n return undefined;\n });\n }\n\n static diff<T extends object>(\n oldEntity: T,\n newEntity: T,\n ): { property: string; oldValue: unknown; newValue: unknown }[] {\n if (!this.sameEntity(oldEntity, newEntity)) {\n throw new Error('Entities must be of the same type to compute diff');\n }\n\n const diffs: { property: string; oldValue: unknown; newValue: unknown }[] =\n [];\n\n const keys = this.getPropertyKeys(oldEntity);\n\n for (const key of keys) {\n const oldValue = (oldEntity as any)[key];\n const newValue = (newEntity as any)[key];\n\n // Check if there's a custom equals function for this property\n const propertyOptions = this.getPropertyOptions(oldEntity, key);\n const areEqual = propertyOptions?.equals\n ? propertyOptions.equals(oldValue, newValue)\n : this.equals(oldValue, newValue);\n\n if (!areEqual) {\n diffs.push({ property: key, oldValue, newValue });\n }\n }\n\n return diffs;\n }\n\n static changes<T extends object>(oldEntity: T, newEntity: T): Partial<T> {\n if (!this.sameEntity(oldEntity, newEntity)) {\n throw new Error('Entities must be of the same type to compute changes');\n }\n\n const diff = this.diff(oldEntity, newEntity);\n\n return diff.reduce((acc, { property, newValue }) => {\n (acc as any)[property] = newValue;\n return acc;\n }, {} as Partial<T>);\n }\n}\n"],"names":["ENTITY_METADATA_KEY","PROPERTY_METADATA_KEY","PROPERTY_OPTIONS_METADATA_KEY","isEqualWith","EntityUtils","isEntity","obj","Reflect","hasMetadata","Array","isArray","constructor","Object","getPrototypeOf","sameEntity","a","b","getPropertyKeys","target","currentProto","prototype","keys","seen","Set","protoKeys","getOwnMetadata","key","has","add","push","getPropertyOptions","propertyKey","protoOptions","undefined","equals","val1","val2","diff","length","oldEntity","newEntity","Error","diffs","oldValue","newValue","propertyOptions","areEqual","property","changes","reduce","acc"],"mappings":"AAAA,SACEA,mBAAmB,EACnBC,qBAAqB,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MAAMb,OAAOL,KAAM;YACtB,MAAMsB,WAAW,AAACJ,SAAiB,CAACb,IAAI;YACxC,MAAMkB,WAAW,AAACJ,SAAiB,CAACd,IAAI;YAExC,8DAA8D;YAC9D,MAAMmB,kBAAkB,IAAI,CAACf,kBAAkB,CAACS,WAAWb;YAC3D,MAAMoB,WAAWD,iBAAiBX,SAC9BW,gBAAgBX,MAAM,CAACS,UAAUC,YACjC,IAAI,CAACV,MAAM,CAACS,UAAUC;YAE1B,IAAI,CAACE,UAAU;gBACbJ,MAAMb,IAAI,CAAC;oBAAEkB,UAAUrB;oBAAKiB;oBAAUC;gBAAS;YACjD;QACF;QAEA,OAAOF;IACT;IAEA,OAAOM,QAA0BT,SAAY,EAAEC,SAAY,EAAc;QACvE,IAAI,CAAC,IAAI,CAAC1B,UAAU,CAACyB,WAAWC,YAAY;YAC1C,MAAM,IAAIC,MAAM;QAClB;QAEA,MAAMJ,OAAO,IAAI,CAACA,IAAI,CAACE,WAAWC;QAElC,OAAOH,KAAKY,MAAM,CAAC,CAACC,KAAK,EAAEH,QAAQ,EAAEH,QAAQ,EAAE;YAC5CM,GAAW,CAACH,SAAS,GAAGH;YACzB,OAAOM;QACT,GAAG,CAAC;IACN;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator that marks a class as an Entity.
|
|
3
|
+
* This allows us to identify entity instances later.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* @Entity()
|
|
8
|
+
* class User {
|
|
9
|
+
* name: string;
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function Entity(): ClassDecorator;
|
|
14
|
+
//# sourceMappingURL=entity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity.d.ts","sourceRoot":"","sources":["../../src/lib/entity.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,IAAI,cAAc,CAMvC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ENTITY_METADATA_KEY } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Decorator that marks a class as an Entity.
|
|
4
|
+
* This allows us to identify entity instances later.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @Entity()
|
|
9
|
+
* class User {
|
|
10
|
+
* name: string;
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/ export function Entity() {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
15
|
+
return function(target) {
|
|
16
|
+
// Store metadata on the class constructor
|
|
17
|
+
Reflect.defineMetadata(ENTITY_METADATA_KEY, true, target);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=entity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/entity.ts"],"sourcesContent":["import { ENTITY_METADATA_KEY } from './types.js';\n\n/**\n * Decorator that marks a class as an Entity.\n * This allows us to identify entity instances later.\n *\n * @example\n * ```typescript\n * @Entity()\n * class User {\n * name: string;\n * }\n * ```\n */\nexport function Entity(): ClassDecorator {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n return function (target: Function) {\n // Store metadata on the class constructor\n Reflect.defineMetadata(ENTITY_METADATA_KEY, true, target);\n };\n}\n"],"names":["ENTITY_METADATA_KEY","Entity","target","Reflect","defineMetadata"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,aAAa;AAEjD;;;;;;;;;;;CAWC,GACD,OAAO,SAASC;IACd,sEAAsE;IACtE,OAAO,SAAUC,MAAgB;QAC/B,0CAA0C;QAC1CC,QAAQC,cAAc,CAACJ,qBAAqB,MAAME;IACpD;AACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PropertyOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Property decorator that marks class properties with metadata.
|
|
4
|
+
* This decorator can be used to identify and track properties within classes.
|
|
5
|
+
*
|
|
6
|
+
* @param options - Optional configuration for the property
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* class User {
|
|
10
|
+
* @Property()
|
|
11
|
+
* name: string;
|
|
12
|
+
*
|
|
13
|
+
* @Property({ equals: (a, b) => a.toLowerCase() === b.toLowerCase() })
|
|
14
|
+
* email: string;
|
|
15
|
+
*
|
|
16
|
+
* @Property()
|
|
17
|
+
* age: number;
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
export declare function Property<T = any>(options?: PropertyOptions<T>): PropertyDecorator;
|
|
21
|
+
//# sourceMappingURL=property.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"property.d.ts","sourceRoot":"","sources":["../../src/lib/property.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,GAAG,GAAG,EAC9B,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC3B,iBAAiB,CAiCnB"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { PROPERTY_METADATA_KEY, PROPERTY_OPTIONS_METADATA_KEY } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Property decorator that marks class properties with metadata.
|
|
4
|
+
* This decorator can be used to identify and track properties within classes.
|
|
5
|
+
*
|
|
6
|
+
* @param options - Optional configuration for the property
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* class User {
|
|
10
|
+
* @Property()
|
|
11
|
+
* name: string;
|
|
12
|
+
*
|
|
13
|
+
* @Property({ equals: (a, b) => a.toLowerCase() === b.toLowerCase() })
|
|
14
|
+
* email: string;
|
|
15
|
+
*
|
|
16
|
+
* @Property()
|
|
17
|
+
* age: number;
|
|
18
|
+
* }
|
|
19
|
+
*/ export function Property(options) {
|
|
20
|
+
return (target, propertyKey)=>{
|
|
21
|
+
// Only support string property keys
|
|
22
|
+
if (typeof propertyKey !== 'string') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// Get existing metadata from own property only (not from prototype chain)
|
|
26
|
+
const existingProperties = Reflect.getOwnMetadata(PROPERTY_METADATA_KEY, target) || [];
|
|
27
|
+
// Add this property if not already tracked
|
|
28
|
+
if (!existingProperties.includes(propertyKey)) {
|
|
29
|
+
existingProperties.push(propertyKey);
|
|
30
|
+
}
|
|
31
|
+
// Store updated metadata on the target itself
|
|
32
|
+
Reflect.defineMetadata(PROPERTY_METADATA_KEY, existingProperties, target);
|
|
33
|
+
// Store property options if provided
|
|
34
|
+
if (options) {
|
|
35
|
+
const existingOptions = Reflect.getOwnMetadata(PROPERTY_OPTIONS_METADATA_KEY, target) || {};
|
|
36
|
+
existingOptions[propertyKey] = options;
|
|
37
|
+
Reflect.defineMetadata(PROPERTY_OPTIONS_METADATA_KEY, existingOptions, target);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=property.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/property.ts"],"sourcesContent":["import {\n PROPERTY_METADATA_KEY,\n PROPERTY_OPTIONS_METADATA_KEY,\n PropertyOptions,\n} from './types.js';\n\n/**\n * Property decorator that marks class properties with metadata.\n * This decorator can be used to identify and track properties within classes.\n *\n * @param options - Optional configuration for the property\n *\n * @example\n * class User {\n * @Property()\n * name: string;\n *\n * @Property({ equals: (a, b) => a.toLowerCase() === b.toLowerCase() })\n * email: string;\n *\n * @Property()\n * age: number;\n * }\n */\nexport function Property<T = any>(\n options?: PropertyOptions<T>,\n): PropertyDecorator {\n return (target: object, propertyKey: string | symbol): void => {\n // Only support string property keys\n if (typeof propertyKey !== 'string') {\n return;\n }\n\n // Get existing metadata from own property only (not from prototype chain)\n const existingProperties: string[] =\n Reflect.getOwnMetadata(PROPERTY_METADATA_KEY, target) || [];\n\n // Add this property if not already tracked\n if (!existingProperties.includes(propertyKey)) {\n existingProperties.push(propertyKey);\n }\n\n // Store updated metadata on the target itself\n Reflect.defineMetadata(PROPERTY_METADATA_KEY, existingProperties, target);\n\n // Store property options if provided\n if (options) {\n const existingOptions: Record<string, PropertyOptions> =\n Reflect.getOwnMetadata(PROPERTY_OPTIONS_METADATA_KEY, target) || {};\n\n existingOptions[propertyKey] = options;\n\n Reflect.defineMetadata(\n PROPERTY_OPTIONS_METADATA_KEY,\n existingOptions,\n target,\n );\n }\n };\n}\n"],"names":["PROPERTY_METADATA_KEY","PROPERTY_OPTIONS_METADATA_KEY","Property","options","target","propertyKey","existingProperties","Reflect","getOwnMetadata","includes","push","defineMetadata","existingOptions"],"mappings":"AAAA,SACEA,qBAAqB,EACrBC,6BAA6B,QAExB,aAAa;AAEpB;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,SAASC,SACdC,OAA4B;IAE5B,OAAO,CAACC,QAAgBC;QACtB,oCAAoC;QACpC,IAAI,OAAOA,gBAAgB,UAAU;YACnC;QACF;QAEA,0EAA0E;QAC1E,MAAMC,qBACJC,QAAQC,cAAc,CAACR,uBAAuBI,WAAW,EAAE;QAE7D,2CAA2C;QAC3C,IAAI,CAACE,mBAAmBG,QAAQ,CAACJ,cAAc;YAC7CC,mBAAmBI,IAAI,CAACL;QAC1B;QAEA,8CAA8C;QAC9CE,QAAQI,cAAc,CAACX,uBAAuBM,oBAAoBF;QAElE,qCAAqC;QACrC,IAAID,SAAS;YACX,MAAMS,kBACJL,QAAQC,cAAc,CAACP,+BAA+BG,WAAW,CAAC;YAEpEQ,eAAe,CAACP,YAAY,GAAGF;YAE/BI,QAAQI,cAAc,CACpBV,+BACAW,iBACAR;QAEJ;IACF;AACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata key used to store property information
|
|
3
|
+
*/
|
|
4
|
+
export declare const PROPERTY_METADATA_KEY: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* Metadata key used to store property options
|
|
7
|
+
*/
|
|
8
|
+
export declare const PROPERTY_OPTIONS_METADATA_KEY: unique symbol;
|
|
9
|
+
/**
|
|
10
|
+
* Metadata key used to store entity information
|
|
11
|
+
*/
|
|
12
|
+
export declare const ENTITY_METADATA_KEY: unique symbol;
|
|
13
|
+
/**
|
|
14
|
+
* Options for the Property decorator
|
|
15
|
+
*/
|
|
16
|
+
export interface PropertyOptions<T = any> {
|
|
17
|
+
/**
|
|
18
|
+
* Custom equality comparison function for this property
|
|
19
|
+
* @param a - First value to compare
|
|
20
|
+
* @param b - Second value to compare
|
|
21
|
+
* @returns true if values are equal, false otherwise
|
|
22
|
+
*/
|
|
23
|
+
equals?: (a: T, b: T) => boolean;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,qBAAqB,eAA8B,CAAC;AAEjE;;GAEG;AACH,eAAO,MAAM,6BAA6B,eAEzC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,eAA4B,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,GAAG;IACtC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;CAClC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata key used to store property information
|
|
3
|
+
*/ export const PROPERTY_METADATA_KEY = Symbol('property:metadata');
|
|
4
|
+
/**
|
|
5
|
+
* Metadata key used to store property options
|
|
6
|
+
*/ export const PROPERTY_OPTIONS_METADATA_KEY = Symbol('property:options:metadata');
|
|
7
|
+
/**
|
|
8
|
+
* Metadata key used to store entity information
|
|
9
|
+
*/ export const ENTITY_METADATA_KEY = Symbol('entity:metadata');
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/types.ts"],"sourcesContent":["/**\n * Metadata key used to store property information\n */\nexport const PROPERTY_METADATA_KEY = Symbol('property:metadata');\n\n/**\n * Metadata key used to store property options\n */\nexport const PROPERTY_OPTIONS_METADATA_KEY = Symbol(\n 'property:options:metadata',\n);\n\n/**\n * Metadata key used to store entity information\n */\nexport const ENTITY_METADATA_KEY = Symbol('entity:metadata');\n\n/**\n * Options for the Property decorator\n */\nexport interface PropertyOptions<T = any> {\n /**\n * Custom equality comparison function for this property\n * @param a - First value to compare\n * @param b - Second value to compare\n * @returns true if values are equal, false otherwise\n */\n equals?: (a: T, b: T) => boolean;\n}\n"],"names":["PROPERTY_METADATA_KEY","Symbol","PROPERTY_OPTIONS_METADATA_KEY","ENTITY_METADATA_KEY"],"mappings":"AAAA;;CAEC,GACD,OAAO,MAAMA,wBAAwBC,OAAO,qBAAqB;AAEjE;;CAEC,GACD,OAAO,MAAMC,gCAAgCD,OAC3C,6BACA;AAEF;;CAEC,GACD,OAAO,MAAME,sBAAsBF,OAAO,mBAAmB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rtpaulino/entity",
|
|
3
|
+
"version": "0.10.13",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"@rtpaulino/libs": "./src/index.ts",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"!**/*.tsbuildinfo"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"nx": {
|
|
26
|
+
"tags": [
|
|
27
|
+
"scope:entity"
|
|
28
|
+
],
|
|
29
|
+
"sourceRoot": "packages/entity/src",
|
|
30
|
+
"targets": {
|
|
31
|
+
"build": {
|
|
32
|
+
"executor": "@nx/js:swc",
|
|
33
|
+
"outputs": [
|
|
34
|
+
"{options.outputPath}"
|
|
35
|
+
],
|
|
36
|
+
"dependsOn": [
|
|
37
|
+
"typecheck"
|
|
38
|
+
],
|
|
39
|
+
"options": {
|
|
40
|
+
"outputPath": "packages/entity/dist",
|
|
41
|
+
"main": "packages/entity/src/index.ts",
|
|
42
|
+
"tsConfig": "packages/entity/tsconfig.lib.json",
|
|
43
|
+
"skipTypeCheck": true,
|
|
44
|
+
"stripLeadingPaths": true
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@rtpaulino/core": "^0.10.4",
|
|
51
|
+
"@swc/helpers": "~0.5.18",
|
|
52
|
+
"lodash-es": "^4.17.22"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/lodash-es": "^4.17.12"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"reflect-metadata": "^0.2.2"
|
|
59
|
+
}
|
|
60
|
+
}
|