metal-orm 1.0.102 → 1.0.104
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 +103 -81
- 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 +103 -81
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/scripts/generate-entities/cli.mjs +1 -1
- package/scripts/generate-entities/generate.mjs +21 -12
- package/scripts/generate-entities/render.mjs +5 -4
- package/scripts/naming-strategy.mjs +19 -3
- package/src/orm/entity-relations.ts +209 -207
- package/src/orm/entity.ts +160 -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,186 +1,190 @@
|
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
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
|
|
70
|
-
this
|
|
71
|
-
return this
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Gets the current items in the collection.
|
|
76
|
-
* @returns Array of child entities
|
|
77
|
-
*/
|
|
78
|
-
getItems(): TChild[] {
|
|
79
|
-
return this
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Array-compatible length for testing frameworks.
|
|
84
|
-
*/
|
|
85
|
-
get length(): number {
|
|
86
|
-
return this
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Enables iteration over the collection like an array.
|
|
91
|
-
*/
|
|
92
|
-
[Symbol.iterator](): Iterator<TChild> {
|
|
93
|
-
return this
|
|
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
|
|
109
|
-
this
|
|
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
|
|
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
|
|
146
|
-
this
|
|
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
|
|
162
|
-
this.remove(entity);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
private
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
+
/**
|
|
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
|
+
#loaded = false;
|
|
29
|
+
#items: TChild[] = [];
|
|
30
|
+
readonly #added = new Set<TChild>();
|
|
31
|
+
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
|
+
isLoaded(): boolean {
|
|
167
|
+
return this.#loaded;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private get relationKey(): RelationKey {
|
|
171
|
+
return `${this.rootTable.name}.${this.relationName}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private hydrateFromCache(): void {
|
|
175
|
+
const keyValue = (this.root as Record<string, unknown>)[this.localKey];
|
|
176
|
+
if (keyValue === undefined || keyValue === null) return;
|
|
177
|
+
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
178
|
+
if (!rows?.length) return;
|
|
179
|
+
this.#items = rows.map(row => this.createEntity(row));
|
|
180
|
+
this.#loaded = true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Returns the items for JSON serialization.
|
|
185
|
+
* @returns Array of child entities
|
|
186
|
+
*/
|
|
187
|
+
toJSON(): TChild[] {
|
|
188
|
+
return this.#items;
|
|
189
|
+
}
|
|
190
|
+
}
|