@cheetah.js/orm 0.1.74 → 0.1.76
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.
|
@@ -73,3 +73,14 @@ export declare class Reference<T> {
|
|
|
73
73
|
constructor(entity: T);
|
|
74
74
|
get(): T;
|
|
75
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates a lightweight entity reference by class and id without hitting the DB.
|
|
78
|
+
* Useful to assign many-to-one relations when only the id is known.
|
|
79
|
+
*
|
|
80
|
+
* Example:
|
|
81
|
+
* const library = await UserLibrary.create({
|
|
82
|
+
* user: refById(User, userId),
|
|
83
|
+
* course: refById(Course, courseId),
|
|
84
|
+
* });
|
|
85
|
+
*/
|
|
86
|
+
export declare function refById<C extends new () => any, T = InstanceType<C>, PK = any>(Cls: C, id: PK): T;
|
package/dist/domain/reference.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.Reference = void 0;
|
|
|
4
4
|
exports.ref = ref;
|
|
5
5
|
exports.unwrap = unwrap;
|
|
6
6
|
exports.isLoaded = isLoaded;
|
|
7
|
+
exports.refById = refById;
|
|
7
8
|
/**
|
|
8
9
|
* Creates a reference to an entity.
|
|
9
10
|
* This is an identity function - it returns the input unchanged.
|
|
@@ -68,3 +69,18 @@ class Reference {
|
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
exports.Reference = Reference;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a lightweight entity reference by class and id without hitting the DB.
|
|
74
|
+
* Useful to assign many-to-one relations when only the id is known.
|
|
75
|
+
*
|
|
76
|
+
* Example:
|
|
77
|
+
* const library = await UserLibrary.create({
|
|
78
|
+
* user: refById(User, userId),
|
|
79
|
+
* course: refById(Course, courseId),
|
|
80
|
+
* });
|
|
81
|
+
*/
|
|
82
|
+
function refById(Cls, id) {
|
|
83
|
+
const entity = new Cls();
|
|
84
|
+
entity.id = id;
|
|
85
|
+
return entity;
|
|
86
|
+
}
|
|
@@ -76,11 +76,17 @@ class ModelTransformer {
|
|
|
76
76
|
entity[key] = value;
|
|
77
77
|
}
|
|
78
78
|
findPropertyByColumnName(columnName, options) {
|
|
79
|
+
// First, try to find in regular properties
|
|
79
80
|
const entry = Object.entries(options.properties).find(([_, prop]) => prop.options.columnName === columnName);
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
81
|
+
if (entry) {
|
|
82
|
+
return { key: entry[0], property: entry[1] };
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
+
// If not found, try to find in relations (many-to-one)
|
|
85
|
+
const relation = options.relations?.find((rel) => rel.columnName === columnName && rel.relation === 'many-to-one');
|
|
86
|
+
if (relation) {
|
|
87
|
+
return { key: relation.propertyKey, property: relation };
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
84
90
|
}
|
|
85
91
|
isValueObjectType(type) {
|
|
86
92
|
return (0, utils_1.extendsFrom)(value_object_1.ValueObject, type?.prototype);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheetah.js/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.76",
|
|
4
4
|
"description": "A simple ORM for Cheetah.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"bun",
|
|
56
56
|
"value-object"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "253d62e2b6a0d63016b730b3d72bb982c6e64825"
|
|
59
59
|
}
|