metal-orm 1.0.103 → 1.0.105
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/dist/index.cjs +130 -86
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -8
- package/dist/index.d.ts +6 -8
- package/dist/index.js +130 -86
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/entity-relations.ts +30 -7
- package/src/orm/entity.ts +162 -151
- package/src/orm/relations/belongs-to.ts +130 -126
- package/src/orm/relations/has-many.ts +190 -186
- package/src/orm/relations/has-one.ts +162 -158
- package/src/orm/relations/many-to-many.ts +228 -224
|
@@ -1,158 +1,162 @@
|
|
|
1
|
-
import { HasOneReferenceApi } from '../../schema/types.js';
|
|
2
|
-
import { EntityContext } from '../entity-context.js';
|
|
3
|
-
import { RelationKey } from '../runtime-types.js';
|
|
4
|
-
import { HasOneRelation } from '../../schema/relation.js';
|
|
5
|
-
import { TableDef } from '../../schema/table.js';
|
|
6
|
-
import { EntityMeta, getHydrationRecord, hasEntityMeta } from '../entity-meta.js';
|
|
7
|
-
|
|
8
|
-
type Row = Record<string, unknown>;
|
|
9
|
-
|
|
10
|
-
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
11
|
-
|
|
12
|
-
const hideInternal = (obj: object, keys: string[]): void => {
|
|
13
|
-
for (const key of keys) {
|
|
14
|
-
Object.defineProperty(obj, key, {
|
|
15
|
-
value: obj[key],
|
|
16
|
-
writable: false,
|
|
17
|
-
configurable:
|
|
18
|
-
enumerable: false
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Default implementation of a has-one reference.
|
|
25
|
-
* Manages a reference to a child entity where the child carries the foreign key.
|
|
26
|
-
*
|
|
27
|
-
* @template TChild The type of the child entity.
|
|
28
|
-
*/
|
|
29
|
-
export class DefaultHasOneReference<TChild extends object> implements HasOneReferenceApi<TChild> {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @param ctx The entity context for tracking changes.
|
|
35
|
-
* @param meta Metadata for the parent entity.
|
|
36
|
-
* @param root The parent entity instance.
|
|
37
|
-
* @param relationName The name of the relation.
|
|
38
|
-
* @param relation Relation definition.
|
|
39
|
-
* @param rootTable Table definition of the parent entity.
|
|
40
|
-
* @param loader Function to load the child entity.
|
|
41
|
-
* @param createEntity Function to create entity instances from rows.
|
|
42
|
-
* @param localKey The local key on the parent entity used for the relation.
|
|
43
|
-
*/
|
|
44
|
-
constructor(
|
|
45
|
-
private readonly ctx: EntityContext,
|
|
46
|
-
private readonly meta: EntityMeta<TableDef>,
|
|
47
|
-
private readonly root: unknown,
|
|
48
|
-
private readonly relationName: string,
|
|
49
|
-
private readonly relation: HasOneRelation,
|
|
50
|
-
private readonly rootTable: TableDef,
|
|
51
|
-
private readonly loader: () => Promise<Map<string, Row>>,
|
|
52
|
-
private readonly createEntity: (row: Row) => TChild,
|
|
53
|
-
private readonly localKey: string
|
|
54
|
-
) {
|
|
55
|
-
hideInternal(this, [
|
|
56
|
-
'ctx',
|
|
57
|
-
'meta',
|
|
58
|
-
'root',
|
|
59
|
-
'relationName',
|
|
60
|
-
'relation',
|
|
61
|
-
'rootTable',
|
|
62
|
-
'loader',
|
|
63
|
-
'createEntity',
|
|
64
|
-
'localKey'
|
|
65
|
-
]);
|
|
66
|
-
this.populateFromHydrationCache();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async load(): Promise<TChild | null> {
|
|
70
|
-
if (this
|
|
71
|
-
const map = await this.loader();
|
|
72
|
-
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
73
|
-
if (keyValue === undefined || keyValue === null) {
|
|
74
|
-
this
|
|
75
|
-
return this
|
|
76
|
-
}
|
|
77
|
-
const row = map.get(toKey(keyValue));
|
|
78
|
-
this
|
|
79
|
-
this
|
|
80
|
-
return this
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
get(): TChild | null {
|
|
84
|
-
return this
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
set(data: Partial<TChild> | TChild | null): TChild | null {
|
|
88
|
-
if (data === null) {
|
|
89
|
-
return this.detachCurrent();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const entity = hasEntityMeta(data) ? (data as TChild) : this.createEntity(data as Row);
|
|
93
|
-
if (this
|
|
94
|
-
this.ctx.registerRelationChange(
|
|
95
|
-
this.root,
|
|
96
|
-
this.relationKey,
|
|
97
|
-
this.rootTable,
|
|
98
|
-
this.relationName,
|
|
99
|
-
this.relation,
|
|
100
|
-
{ kind: 'remove', entity: this
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this.assignForeignKey(entity);
|
|
105
|
-
this
|
|
106
|
-
this
|
|
107
|
-
|
|
108
|
-
this.ctx.registerRelationChange(
|
|
109
|
-
this.root,
|
|
110
|
-
this.relationKey,
|
|
111
|
-
this.rootTable,
|
|
112
|
-
this.relationName,
|
|
113
|
-
this.relation,
|
|
114
|
-
{ kind: 'attach', entity }
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
return entity;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return this
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
1
|
+
import { HasOneReferenceApi } from '../../schema/types.js';
|
|
2
|
+
import { EntityContext } from '../entity-context.js';
|
|
3
|
+
import { RelationKey } from '../runtime-types.js';
|
|
4
|
+
import { HasOneRelation } from '../../schema/relation.js';
|
|
5
|
+
import { TableDef } from '../../schema/table.js';
|
|
6
|
+
import { EntityMeta, getHydrationRecord, hasEntityMeta } from '../entity-meta.js';
|
|
7
|
+
|
|
8
|
+
type Row = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
11
|
+
|
|
12
|
+
const hideInternal = (obj: object, keys: string[]): void => {
|
|
13
|
+
for (const key of keys) {
|
|
14
|
+
Object.defineProperty(obj, key, {
|
|
15
|
+
value: obj[key],
|
|
16
|
+
writable: false,
|
|
17
|
+
configurable: true, // Must be configurable for Proxy get trap to work properly
|
|
18
|
+
enumerable: false
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Default implementation of a has-one reference.
|
|
25
|
+
* Manages a reference to a child entity where the child carries the foreign key.
|
|
26
|
+
*
|
|
27
|
+
* @template TChild The type of the child entity.
|
|
28
|
+
*/
|
|
29
|
+
export class DefaultHasOneReference<TChild extends object> implements HasOneReferenceApi<TChild> {
|
|
30
|
+
#loaded = false;
|
|
31
|
+
#current: TChild | null = null;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param ctx The entity context for tracking changes.
|
|
35
|
+
* @param meta Metadata for the parent entity.
|
|
36
|
+
* @param root The parent entity instance.
|
|
37
|
+
* @param relationName The name of the relation.
|
|
38
|
+
* @param relation Relation definition.
|
|
39
|
+
* @param rootTable Table definition of the parent entity.
|
|
40
|
+
* @param loader Function to load the child entity.
|
|
41
|
+
* @param createEntity Function to create entity instances from rows.
|
|
42
|
+
* @param localKey The local key on the parent entity used for the relation.
|
|
43
|
+
*/
|
|
44
|
+
constructor(
|
|
45
|
+
private readonly ctx: EntityContext,
|
|
46
|
+
private readonly meta: EntityMeta<TableDef>,
|
|
47
|
+
private readonly root: unknown,
|
|
48
|
+
private readonly relationName: string,
|
|
49
|
+
private readonly relation: HasOneRelation,
|
|
50
|
+
private readonly rootTable: TableDef,
|
|
51
|
+
private readonly loader: () => Promise<Map<string, Row>>,
|
|
52
|
+
private readonly createEntity: (row: Row) => TChild,
|
|
53
|
+
private readonly localKey: string
|
|
54
|
+
) {
|
|
55
|
+
hideInternal(this, [
|
|
56
|
+
'ctx',
|
|
57
|
+
'meta',
|
|
58
|
+
'root',
|
|
59
|
+
'relationName',
|
|
60
|
+
'relation',
|
|
61
|
+
'rootTable',
|
|
62
|
+
'loader',
|
|
63
|
+
'createEntity',
|
|
64
|
+
'localKey'
|
|
65
|
+
]);
|
|
66
|
+
this.populateFromHydrationCache();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async load(): Promise<TChild | null> {
|
|
70
|
+
if (this.#loaded) return this.#current;
|
|
71
|
+
const map = await this.loader();
|
|
72
|
+
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
73
|
+
if (keyValue === undefined || keyValue === null) {
|
|
74
|
+
this.#loaded = true;
|
|
75
|
+
return this.#current;
|
|
76
|
+
}
|
|
77
|
+
const row = map.get(toKey(keyValue));
|
|
78
|
+
this.#current = row ? this.createEntity(row) : null;
|
|
79
|
+
this.#loaded = true;
|
|
80
|
+
return this.#current;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get(): TChild | null {
|
|
84
|
+
return this.#current;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
set(data: Partial<TChild> | TChild | null): TChild | null {
|
|
88
|
+
if (data === null) {
|
|
89
|
+
return this.detachCurrent();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const entity = hasEntityMeta(data) ? (data as TChild) : this.createEntity(data as Row);
|
|
93
|
+
if (this.#current && this.#current !== entity) {
|
|
94
|
+
this.ctx.registerRelationChange(
|
|
95
|
+
this.root,
|
|
96
|
+
this.relationKey,
|
|
97
|
+
this.rootTable,
|
|
98
|
+
this.relationName,
|
|
99
|
+
this.relation,
|
|
100
|
+
{ kind: 'remove', entity: this.#current }
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.assignForeignKey(entity);
|
|
105
|
+
this.#current = entity;
|
|
106
|
+
this.#loaded = true;
|
|
107
|
+
|
|
108
|
+
this.ctx.registerRelationChange(
|
|
109
|
+
this.root,
|
|
110
|
+
this.relationKey,
|
|
111
|
+
this.rootTable,
|
|
112
|
+
this.relationName,
|
|
113
|
+
this.relation,
|
|
114
|
+
{ kind: 'attach', entity }
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return entity;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
isLoaded(): boolean {
|
|
121
|
+
return this.#loaded;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
toJSON(): TChild | null {
|
|
125
|
+
return this.#current;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private detachCurrent(): TChild | null {
|
|
129
|
+
const previous = this.#current;
|
|
130
|
+
if (!previous) return null;
|
|
131
|
+
this.#current = null;
|
|
132
|
+
this.#loaded = true;
|
|
133
|
+
this.ctx.registerRelationChange(
|
|
134
|
+
this.root,
|
|
135
|
+
this.relationKey,
|
|
136
|
+
this.rootTable,
|
|
137
|
+
this.relationName,
|
|
138
|
+
this.relation,
|
|
139
|
+
{ kind: 'remove', entity: previous }
|
|
140
|
+
);
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private assignForeignKey(entity: TChild): void {
|
|
145
|
+
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
147
|
+
(entity as Row)[this.relation.foreignKey] = keyValue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private get relationKey(): RelationKey {
|
|
151
|
+
return `${this.rootTable.name}.${this.relationName}`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private populateFromHydrationCache(): void {
|
|
155
|
+
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
156
|
+
if (keyValue === undefined || keyValue === null) return;
|
|
157
|
+
const row = getHydrationRecord(this.meta, this.relationName, keyValue);
|
|
158
|
+
if (!row) return;
|
|
159
|
+
this.#current = this.createEntity(row);
|
|
160
|
+
this.#loaded = true;
|
|
161
|
+
}
|
|
162
|
+
}
|