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.
@@ -1,186 +1,199 @@
1
- import { HasManyCollection } from '../../schema/types.js';
2
- import { EntityContext } from '../entity-context.js';
3
- import { RelationKey } from '../runtime-types.js';
4
- import { HasManyRelation } from '../../schema/relation.js';
5
- import { TableDef } from '../../schema/table.js';
6
- import { EntityMeta, getHydrationRows } from '../entity-meta.js';
7
-
8
- type Rows = 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: false,
18
- enumerable: false
19
- });
20
- }
21
- };
22
-
23
- /**
24
- * Default implementation of HasManyCollection for managing one-to-many relationships.
25
- * @template TChild - The type of child entities in the collection
26
- */
27
- export class DefaultHasManyCollection<TChild> implements HasManyCollection<TChild> {
28
- private loaded = false;
29
- private items: TChild[] = [];
30
- private readonly added = new Set<TChild>();
31
- private readonly removed = new Set<TChild>();
32
-
33
- /**
34
- * Creates a new DefaultHasManyCollection instance.
35
- * @param ctx - The entity context
36
- * @param meta - The entity metadata
37
- * @param root - The root entity
38
- * @param relationName - The relation name
39
- * @param relation - The relation definition
40
- * @param rootTable - The root table definition
41
- * @param loader - The loader function for lazy loading
42
- * @param createEntity - Function to create entities from rows
43
- * @param localKey - The local key for the relation
44
- */
45
- constructor(
46
- private readonly ctx: EntityContext,
47
- private readonly meta: EntityMeta<TableDef>,
48
- private readonly root: unknown,
49
- private readonly relationName: string,
50
- private readonly relation: HasManyRelation,
51
- private readonly rootTable: TableDef,
52
- private readonly loader: () => Promise<Map<string, Rows>>,
53
- private readonly createEntity: (row: Record<string, unknown>) => TChild,
54
- private readonly localKey: string
55
- ) {
56
- hideInternal(this, ['ctx', 'meta', 'root', 'relationName', 'relation', 'rootTable', 'loader', 'createEntity', 'localKey']);
57
- this.hydrateFromCache();
58
- }
59
-
60
- /**
61
- * Loads the related entities if not already loaded.
62
- * @returns Promise resolving to the array of child entities
63
- */
64
- async load(): Promise<TChild[]> {
65
- if (this.loaded) return this.items;
66
- const map = await this.loader();
67
- const key = toKey((this.root as Record<string, unknown>)[this.localKey]);
68
- const rows = map.get(key) ?? [];
69
- this.items = rows.map(row => this.createEntity(row));
70
- this.loaded = true;
71
- return this.items;
72
- }
73
-
74
- /**
75
- * Gets the current items in the collection.
76
- * @returns Array of child entities
77
- */
78
- getItems(): TChild[] {
79
- return this.items;
80
- }
81
-
82
- /**
83
- * Array-compatible length for testing frameworks.
84
- */
85
- get length(): number {
86
- return this.items.length;
87
- }
88
-
89
- /**
90
- * Enables iteration over the collection like an array.
91
- */
92
- [Symbol.iterator](): Iterator<TChild> {
93
- return this.items[Symbol.iterator]();
94
- }
95
-
96
- /**
97
- * Adds a new child entity to the collection.
98
- * @param data - Partial data for the new entity
99
- * @returns The created entity
100
- */
101
- add(data: Partial<TChild>): TChild {
102
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
103
- const childRow: Record<string, unknown> = {
104
- ...data,
105
- [this.relation.foreignKey]: keyValue
106
- };
107
- const entity = this.createEntity(childRow);
108
- this.added.add(entity);
109
- this.items.push(entity);
110
- this.ctx.registerRelationChange(
111
- this.root,
112
- this.relationKey,
113
- this.rootTable,
114
- this.relationName,
115
- this.relation,
116
- { kind: 'add', entity }
117
- );
118
- return entity;
119
- }
120
-
121
- /**
122
- * Attaches an existing entity to the collection.
123
- * @param entity - The entity to attach
124
- */
125
- attach(entity: TChild): void {
126
- const keyValue = this.root[this.localKey];
127
- (entity as Record<string, unknown>)[this.relation.foreignKey] = keyValue;
128
- this.ctx.markDirty(entity as object);
129
- this.items.push(entity);
130
- this.ctx.registerRelationChange(
131
- this.root,
132
- this.relationKey,
133
- this.rootTable,
134
- this.relationName,
135
- this.relation,
136
- { kind: 'attach', entity }
137
- );
138
- }
139
-
140
- /**
141
- * Removes an entity from the collection.
142
- * @param entity - The entity to remove
143
- */
144
- remove(entity: TChild): void {
145
- this.items = this.items.filter(item => item !== entity);
146
- this.removed.add(entity);
147
- this.ctx.registerRelationChange(
148
- this.root,
149
- this.relationKey,
150
- this.rootTable,
151
- this.relationName,
152
- this.relation,
153
- { kind: 'remove', entity }
154
- );
155
- }
156
-
157
- /**
158
- * Clears all entities from the collection.
159
- */
160
- clear(): void {
161
- for (const entity of [...this.items]) {
162
- this.remove(entity);
163
- }
164
- }
165
-
166
- private get relationKey(): RelationKey {
167
- return `${this.rootTable.name}.${this.relationName}`;
168
- }
169
-
170
- private hydrateFromCache(): void {
171
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
172
- if (keyValue === undefined || keyValue === null) return;
173
- const rows = getHydrationRows(this.meta, this.relationName, keyValue);
174
- if (!rows?.length) return;
175
- this.items = rows.map(row => this.createEntity(row));
176
- this.loaded = true;
177
- }
178
-
179
- /**
180
- * Returns the items for JSON serialization.
181
- * @returns Array of child entities
182
- */
183
- toJSON(): TChild[] {
184
- return this.items;
185
- }
186
- }
1
+ import { HasManyCollection } from '../../schema/types.js';
2
+ import { EntityContext } from '../entity-context.js';
3
+ import { RelationKey } from '../runtime-types.js';
4
+ import { HasManyRelation } from '../../schema/relation.js';
5
+ import { TableDef } from '../../schema/table.js';
6
+ import { EntityMeta, getHydrationRows } from '../entity-meta.js';
7
+
8
+ type Rows = 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: false,
18
+ enumerable: false
19
+ });
20
+ }
21
+ };
22
+
23
+ const hideWritable = (obj: object, keys: string[]): void => {
24
+ for (const key of keys) {
25
+ const value = obj[key as keyof typeof obj];
26
+ Object.defineProperty(obj, key, {
27
+ value,
28
+ writable: true,
29
+ configurable: true,
30
+ enumerable: false
31
+ });
32
+ }
33
+ };
34
+
35
+ /**
36
+ * Default implementation of HasManyCollection for managing one-to-many relationships.
37
+ * @template TChild - The type of child entities in the collection
38
+ */
39
+ export class DefaultHasManyCollection<TChild> implements HasManyCollection<TChild> {
40
+ private loaded = false;
41
+ private items: TChild[] = [];
42
+ private readonly added = new Set<TChild>();
43
+ private readonly removed = new Set<TChild>();
44
+
45
+ /**
46
+ * Creates a new DefaultHasManyCollection instance.
47
+ * @param ctx - The entity context
48
+ * @param meta - The entity metadata
49
+ * @param root - The root entity
50
+ * @param relationName - The relation name
51
+ * @param relation - The relation definition
52
+ * @param rootTable - The root table definition
53
+ * @param loader - The loader function for lazy loading
54
+ * @param createEntity - Function to create entities from rows
55
+ * @param localKey - The local key for the relation
56
+ */
57
+ constructor(
58
+ private readonly ctx: EntityContext,
59
+ private readonly meta: EntityMeta<TableDef>,
60
+ private readonly root: unknown,
61
+ private readonly relationName: string,
62
+ private readonly relation: HasManyRelation,
63
+ private readonly rootTable: TableDef,
64
+ private readonly loader: () => Promise<Map<string, Rows>>,
65
+ private readonly createEntity: (row: Record<string, unknown>) => TChild,
66
+ private readonly localKey: string
67
+ ) {
68
+ hideInternal(this, ['ctx', 'meta', 'root', 'relationName', 'relation', 'rootTable', 'loader', 'createEntity', 'localKey']);
69
+ hideWritable(this, ['loaded', 'items', 'added', 'removed']);
70
+ this.hydrateFromCache();
71
+ }
72
+
73
+ /**
74
+ * Loads the related entities if not already loaded.
75
+ * @returns Promise resolving to the array of child entities
76
+ */
77
+ async load(): Promise<TChild[]> {
78
+ if (this.loaded) return this.items;
79
+ const map = await this.loader();
80
+ const key = toKey((this.root as Record<string, unknown>)[this.localKey]);
81
+ const rows = map.get(key) ?? [];
82
+ this.items = rows.map(row => this.createEntity(row));
83
+ this.loaded = true;
84
+ return this.items;
85
+ }
86
+
87
+ /**
88
+ * Gets the current items in the collection.
89
+ * @returns Array of child entities
90
+ */
91
+ getItems(): TChild[] {
92
+ return this.items;
93
+ }
94
+
95
+ /**
96
+ * Array-compatible length for testing frameworks.
97
+ */
98
+ get length(): number {
99
+ return this.items.length;
100
+ }
101
+
102
+ /**
103
+ * Enables iteration over the collection like an array.
104
+ */
105
+ [Symbol.iterator](): Iterator<TChild> {
106
+ return this.items[Symbol.iterator]();
107
+ }
108
+
109
+ /**
110
+ * Adds a new child entity to the collection.
111
+ * @param data - Partial data for the new entity
112
+ * @returns The created entity
113
+ */
114
+ add(data: Partial<TChild>): TChild {
115
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
116
+ const childRow: Record<string, unknown> = {
117
+ ...data,
118
+ [this.relation.foreignKey]: keyValue
119
+ };
120
+ const entity = this.createEntity(childRow);
121
+ this.added.add(entity);
122
+ this.items.push(entity);
123
+ this.ctx.registerRelationChange(
124
+ this.root,
125
+ this.relationKey,
126
+ this.rootTable,
127
+ this.relationName,
128
+ this.relation,
129
+ { kind: 'add', entity }
130
+ );
131
+ return entity;
132
+ }
133
+
134
+ /**
135
+ * Attaches an existing entity to the collection.
136
+ * @param entity - The entity to attach
137
+ */
138
+ attach(entity: TChild): void {
139
+ const keyValue = this.root[this.localKey];
140
+ (entity as Record<string, unknown>)[this.relation.foreignKey] = keyValue;
141
+ this.ctx.markDirty(entity as object);
142
+ this.items.push(entity);
143
+ this.ctx.registerRelationChange(
144
+ this.root,
145
+ this.relationKey,
146
+ this.rootTable,
147
+ this.relationName,
148
+ this.relation,
149
+ { kind: 'attach', entity }
150
+ );
151
+ }
152
+
153
+ /**
154
+ * Removes an entity from the collection.
155
+ * @param entity - The entity to remove
156
+ */
157
+ remove(entity: TChild): void {
158
+ this.items = this.items.filter(item => item !== entity);
159
+ this.removed.add(entity);
160
+ this.ctx.registerRelationChange(
161
+ this.root,
162
+ this.relationKey,
163
+ this.rootTable,
164
+ this.relationName,
165
+ this.relation,
166
+ { kind: 'remove', entity }
167
+ );
168
+ }
169
+
170
+ /**
171
+ * Clears all entities from the collection.
172
+ */
173
+ clear(): void {
174
+ for (const entity of [...this.items]) {
175
+ this.remove(entity);
176
+ }
177
+ }
178
+
179
+ private get relationKey(): RelationKey {
180
+ return `${this.rootTable.name}.${this.relationName}`;
181
+ }
182
+
183
+ private hydrateFromCache(): void {
184
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
185
+ if (keyValue === undefined || keyValue === null) return;
186
+ const rows = getHydrationRows(this.meta, this.relationName, keyValue);
187
+ if (!rows?.length) return;
188
+ this.items = rows.map(row => this.createEntity(row));
189
+ this.loaded = true;
190
+ }
191
+
192
+ /**
193
+ * Returns the items for JSON serialization.
194
+ * @returns Array of child entities
195
+ */
196
+ toJSON(): TChild[] {
197
+ return this.items;
198
+ }
199
+ }