metal-orm 1.0.106 → 1.0.107
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 +54 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +54 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/entity.ts +155 -151
- package/src/orm/relations/belongs-to.ts +139 -126
- package/src/orm/relations/has-many.ts +199 -186
- package/src/orm/relations/has-one.ts +171 -158
- package/src/orm/relations/many-to-many.ts +237 -224
|
@@ -1,224 +1,237 @@
|
|
|
1
|
-
import { ManyToManyCollection } from '../../schema/types.js';
|
|
2
|
-
import { EntityContext } from '../entity-context.js';
|
|
3
|
-
import { RelationKey } from '../runtime-types.js';
|
|
4
|
-
import { BelongsToManyRelation } from '../../schema/relation.js';
|
|
5
|
-
import { TableDef } from '../../schema/table.js';
|
|
6
|
-
import { findPrimaryKey } from '../../query-builder/hydration-planner.js';
|
|
7
|
-
import { EntityMeta, getHydrationRows } from '../entity-meta.js';
|
|
8
|
-
|
|
9
|
-
type Rows = Record<string, unknown>[];
|
|
10
|
-
|
|
11
|
-
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
12
|
-
|
|
13
|
-
const hideInternal = (obj: object, keys: string[]): void => {
|
|
14
|
-
for (const key of keys) {
|
|
15
|
-
Object.defineProperty(obj, key, {
|
|
16
|
-
value: obj[key],
|
|
17
|
-
writable: false,
|
|
18
|
-
configurable: false,
|
|
19
|
-
enumerable: false
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
this.
|
|
151
|
-
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return
|
|
223
|
-
|
|
224
|
-
|
|
1
|
+
import { ManyToManyCollection } from '../../schema/types.js';
|
|
2
|
+
import { EntityContext } from '../entity-context.js';
|
|
3
|
+
import { RelationKey } from '../runtime-types.js';
|
|
4
|
+
import { BelongsToManyRelation } from '../../schema/relation.js';
|
|
5
|
+
import { TableDef } from '../../schema/table.js';
|
|
6
|
+
import { findPrimaryKey } from '../../query-builder/hydration-planner.js';
|
|
7
|
+
import { EntityMeta, getHydrationRows } from '../entity-meta.js';
|
|
8
|
+
|
|
9
|
+
type Rows = Record<string, unknown>[];
|
|
10
|
+
|
|
11
|
+
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
|
12
|
+
|
|
13
|
+
const hideInternal = (obj: object, keys: string[]): void => {
|
|
14
|
+
for (const key of keys) {
|
|
15
|
+
Object.defineProperty(obj, key, {
|
|
16
|
+
value: obj[key],
|
|
17
|
+
writable: false,
|
|
18
|
+
configurable: false,
|
|
19
|
+
enumerable: false
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const hideWritable = (obj: object, keys: string[]): void => {
|
|
25
|
+
for (const key of keys) {
|
|
26
|
+
const value = obj[key as keyof typeof obj];
|
|
27
|
+
Object.defineProperty(obj, key, {
|
|
28
|
+
value,
|
|
29
|
+
writable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
enumerable: false
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Default implementation of a many-to-many collection.
|
|
38
|
+
* Manages the relationship between two entities through a pivot table.
|
|
39
|
+
* Supports lazy loading, attaching/detaching entities, and syncing by IDs.
|
|
40
|
+
*
|
|
41
|
+
* @template TTarget The type of the target entities in the collection.
|
|
42
|
+
*/
|
|
43
|
+
export class DefaultManyToManyCollection<TTarget, TPivot extends object | undefined = undefined>
|
|
44
|
+
implements ManyToManyCollection<TTarget, TPivot> {
|
|
45
|
+
private loaded = false;
|
|
46
|
+
private items: TTarget[] = [];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param ctx The entity context for tracking changes.
|
|
50
|
+
* @param meta Metadata for the root entity.
|
|
51
|
+
* @param root The root entity instance.
|
|
52
|
+
* @param relationName The name of the relation.
|
|
53
|
+
* @param relation Relation definition.
|
|
54
|
+
* @param rootTable Table definition of the root entity.
|
|
55
|
+
* @param loader Function to load the collection items.
|
|
56
|
+
* @param createEntity Function to create entity instances from rows.
|
|
57
|
+
* @param localKey The local key used for joining.
|
|
58
|
+
*/
|
|
59
|
+
constructor(
|
|
60
|
+
private readonly ctx: EntityContext,
|
|
61
|
+
private readonly meta: EntityMeta<TableDef>,
|
|
62
|
+
private readonly root: unknown,
|
|
63
|
+
private readonly relationName: string,
|
|
64
|
+
private readonly relation: BelongsToManyRelation,
|
|
65
|
+
private readonly rootTable: TableDef,
|
|
66
|
+
private readonly loader: () => Promise<Map<string, Rows>>,
|
|
67
|
+
private readonly createEntity: (row: Record<string, unknown>) => TTarget,
|
|
68
|
+
private readonly localKey: string
|
|
69
|
+
) {
|
|
70
|
+
hideInternal(this, ['ctx', 'meta', 'root', 'relationName', 'relation', 'rootTable', 'loader', 'createEntity', 'localKey']);
|
|
71
|
+
hideWritable(this, ['loaded', 'items']);
|
|
72
|
+
this.hydrateFromCache();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Loads the collection items if not already loaded.
|
|
77
|
+
* @returns A promise that resolves to the array of target entities.
|
|
78
|
+
*/
|
|
79
|
+
async load(): Promise<TTarget[]> {
|
|
80
|
+
if (this.loaded) return this.items;
|
|
81
|
+
const map = await this.loader();
|
|
82
|
+
const key = toKey(this.root[this.localKey]);
|
|
83
|
+
const rows = map.get(key) ?? [];
|
|
84
|
+
this.items = rows.map(row => {
|
|
85
|
+
const entity = this.createEntity(row);
|
|
86
|
+
if ((row as { _pivot?: unknown })._pivot) {
|
|
87
|
+
(entity as { _pivot?: unknown })._pivot = (row as { _pivot?: unknown })._pivot;
|
|
88
|
+
}
|
|
89
|
+
return entity;
|
|
90
|
+
});
|
|
91
|
+
this.loaded = true;
|
|
92
|
+
return this.items;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns the currently loaded items.
|
|
97
|
+
* @returns Array of target entities.
|
|
98
|
+
*/
|
|
99
|
+
getItems(): TTarget[] {
|
|
100
|
+
return this.items;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Array-compatible length for testing frameworks.
|
|
105
|
+
*/
|
|
106
|
+
get length(): number {
|
|
107
|
+
return this.items.length;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Enables iteration over the collection like an array.
|
|
112
|
+
*/
|
|
113
|
+
[Symbol.iterator](): Iterator<TTarget> {
|
|
114
|
+
return this.items[Symbol.iterator]();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Attaches an entity to the collection.
|
|
119
|
+
* Registers an 'attach' change in the entity context.
|
|
120
|
+
* @param target Entity instance or its primary key value.
|
|
121
|
+
*/
|
|
122
|
+
attach(target: TTarget | number | string): void {
|
|
123
|
+
const entity = this.ensureEntity(target);
|
|
124
|
+
const id = this.extractId(entity);
|
|
125
|
+
if (id != null && this.items.some(item => this.extractId(item) === id)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (id == null && this.items.includes(entity)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.items.push(entity);
|
|
132
|
+
this.ctx.registerRelationChange(
|
|
133
|
+
this.root,
|
|
134
|
+
this.relationKey,
|
|
135
|
+
this.rootTable,
|
|
136
|
+
this.relationName,
|
|
137
|
+
this.relation,
|
|
138
|
+
{ kind: 'attach', entity }
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Detaches an entity from the collection.
|
|
144
|
+
* Registers a 'detach' change in the entity context.
|
|
145
|
+
* @param target Entity instance or its primary key value.
|
|
146
|
+
*/
|
|
147
|
+
detach(target: TTarget | number | string): void {
|
|
148
|
+
const id = typeof target === 'number' || typeof target === 'string'
|
|
149
|
+
? target
|
|
150
|
+
: this.extractId(target);
|
|
151
|
+
|
|
152
|
+
if (id == null) return;
|
|
153
|
+
|
|
154
|
+
const existing = this.items.find(item => this.extractId(item) === id);
|
|
155
|
+
if (!existing) return;
|
|
156
|
+
|
|
157
|
+
this.items = this.items.filter(item => this.extractId(item) !== id);
|
|
158
|
+
this.ctx.registerRelationChange(
|
|
159
|
+
this.root,
|
|
160
|
+
this.relationKey,
|
|
161
|
+
this.rootTable,
|
|
162
|
+
this.relationName,
|
|
163
|
+
this.relation,
|
|
164
|
+
{ kind: 'detach', entity: existing }
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Syncs the collection with a list of IDs.
|
|
170
|
+
* Attaches missing IDs and detaches IDs not in the list.
|
|
171
|
+
* @param ids Array of primary key values to sync with.
|
|
172
|
+
*/
|
|
173
|
+
async syncByIds(ids: (number | string)[]): Promise<void> {
|
|
174
|
+
await this.load();
|
|
175
|
+
const normalized = new Set(ids.map(id => toKey(id)));
|
|
176
|
+
const currentIds = new Set(this.items.map(item => toKey(this.extractId(item))));
|
|
177
|
+
|
|
178
|
+
for (const id of normalized) {
|
|
179
|
+
if (!currentIds.has(id)) {
|
|
180
|
+
this.attach(id);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
for (const item of [...this.items]) {
|
|
185
|
+
const itemId = toKey(this.extractId(item));
|
|
186
|
+
if (!normalized.has(itemId)) {
|
|
187
|
+
this.detach(item);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private ensureEntity(target: TTarget | number | string): TTarget {
|
|
193
|
+
if (typeof target === 'number' || typeof target === 'string') {
|
|
194
|
+
const stub: Record<string, unknown> = {
|
|
195
|
+
[this.targetKey]: target
|
|
196
|
+
};
|
|
197
|
+
return this.createEntity(stub);
|
|
198
|
+
}
|
|
199
|
+
return target;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private extractId(entity: TTarget | number | string | null | undefined): number | string | null {
|
|
203
|
+
if (entity === null || entity === undefined) return null;
|
|
204
|
+
if (typeof entity === 'number' || typeof entity === 'string') {
|
|
205
|
+
return entity;
|
|
206
|
+
}
|
|
207
|
+
return (entity as Record<string, unknown>)[this.targetKey] as string | number | null ?? null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private get relationKey(): RelationKey {
|
|
211
|
+
return `${this.rootTable.name}.${this.relationName}`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private get targetKey(): string {
|
|
215
|
+
return this.relation.targetKey || findPrimaryKey(this.relation.target);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private hydrateFromCache(): void {
|
|
219
|
+
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
220
|
+
if (keyValue === undefined || keyValue === null) return;
|
|
221
|
+
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
222
|
+
if (!rows?.length) return;
|
|
223
|
+
this.items = rows.map(row => {
|
|
224
|
+
const entity = this.createEntity(row);
|
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
226
|
+
if ((row as { _pivot?: unknown })._pivot) {
|
|
227
|
+
(entity as { _pivot?: unknown })._pivot = (row as { _pivot?: unknown })._pivot;
|
|
228
|
+
}
|
|
229
|
+
return entity;
|
|
230
|
+
});
|
|
231
|
+
this.loaded = true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
toJSON(): TTarget[] {
|
|
235
|
+
return this.items;
|
|
236
|
+
}
|
|
237
|
+
}
|