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,158 +1,171 @@
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: false,
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
- private loaded = false;
31
- private 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
- toJSON(): TChild | null {
121
- return this.current;
122
- }
123
-
124
- private detachCurrent(): TChild | null {
125
- const previous = this.current;
126
- if (!previous) return null;
127
- this.current = null;
128
- this.loaded = true;
129
- this.ctx.registerRelationChange(
130
- this.root,
131
- this.relationKey,
132
- this.rootTable,
133
- this.relationName,
134
- this.relation,
135
- { kind: 'remove', entity: previous }
136
- );
137
- return null;
138
- }
139
-
140
- private assignForeignKey(entity: TChild): void {
141
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
142
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
- (entity as Row)[this.relation.foreignKey] = keyValue;
144
- }
145
-
146
- private get relationKey(): RelationKey {
147
- return `${this.rootTable.name}.${this.relationName}`;
148
- }
149
-
150
- private populateFromHydrationCache(): void {
151
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
152
- if (keyValue === undefined || keyValue === null) return;
153
- const row = getHydrationRecord(this.meta, this.relationName, keyValue);
154
- if (!row) return;
155
- this.current = this.createEntity(row);
156
- this.loaded = true;
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: 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 a has-one reference.
37
+ * Manages a reference to a child entity where the child carries the foreign key.
38
+ *
39
+ * @template TChild The type of the child entity.
40
+ */
41
+ export class DefaultHasOneReference<TChild extends object> implements HasOneReferenceApi<TChild> {
42
+ private loaded = false;
43
+ private current: TChild | null = null;
44
+
45
+ /**
46
+ * @param ctx The entity context for tracking changes.
47
+ * @param meta Metadata for the parent entity.
48
+ * @param root The parent entity instance.
49
+ * @param relationName The name of the relation.
50
+ * @param relation Relation definition.
51
+ * @param rootTable Table definition of the parent entity.
52
+ * @param loader Function to load the child entity.
53
+ * @param createEntity Function to create entity instances from rows.
54
+ * @param localKey The local key on the parent entity used for the relation.
55
+ */
56
+ constructor(
57
+ private readonly ctx: EntityContext,
58
+ private readonly meta: EntityMeta<TableDef>,
59
+ private readonly root: unknown,
60
+ private readonly relationName: string,
61
+ private readonly relation: HasOneRelation,
62
+ private readonly rootTable: TableDef,
63
+ private readonly loader: () => Promise<Map<string, Row>>,
64
+ private readonly createEntity: (row: Row) => TChild,
65
+ private readonly localKey: string
66
+ ) {
67
+ hideInternal(this, [
68
+ 'ctx',
69
+ 'meta',
70
+ 'root',
71
+ 'relationName',
72
+ 'relation',
73
+ 'rootTable',
74
+ 'loader',
75
+ 'createEntity',
76
+ 'localKey'
77
+ ]);
78
+ hideWritable(this, ['loaded', 'current']);
79
+ this.populateFromHydrationCache();
80
+ }
81
+
82
+ async load(): Promise<TChild | null> {
83
+ if (this.loaded) return this.current;
84
+ const map = await this.loader();
85
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
86
+ if (keyValue === undefined || keyValue === null) {
87
+ this.loaded = true;
88
+ return this.current;
89
+ }
90
+ const row = map.get(toKey(keyValue));
91
+ this.current = row ? this.createEntity(row) : null;
92
+ this.loaded = true;
93
+ return this.current;
94
+ }
95
+
96
+ get(): TChild | null {
97
+ return this.current;
98
+ }
99
+
100
+ set(data: Partial<TChild> | TChild | null): TChild | null {
101
+ if (data === null) {
102
+ return this.detachCurrent();
103
+ }
104
+
105
+ const entity = hasEntityMeta(data) ? (data as TChild) : this.createEntity(data as Row);
106
+ if (this.current && this.current !== entity) {
107
+ this.ctx.registerRelationChange(
108
+ this.root,
109
+ this.relationKey,
110
+ this.rootTable,
111
+ this.relationName,
112
+ this.relation,
113
+ { kind: 'remove', entity: this.current }
114
+ );
115
+ }
116
+
117
+ this.assignForeignKey(entity);
118
+ this.current = entity;
119
+ this.loaded = true;
120
+
121
+ this.ctx.registerRelationChange(
122
+ this.root,
123
+ this.relationKey,
124
+ this.rootTable,
125
+ this.relationName,
126
+ this.relation,
127
+ { kind: 'attach', entity }
128
+ );
129
+
130
+ return entity;
131
+ }
132
+
133
+ toJSON(): TChild | null {
134
+ return this.current;
135
+ }
136
+
137
+ private detachCurrent(): TChild | null {
138
+ const previous = this.current;
139
+ if (!previous) return null;
140
+ this.current = null;
141
+ this.loaded = true;
142
+ this.ctx.registerRelationChange(
143
+ this.root,
144
+ this.relationKey,
145
+ this.rootTable,
146
+ this.relationName,
147
+ this.relation,
148
+ { kind: 'remove', entity: previous }
149
+ );
150
+ return null;
151
+ }
152
+
153
+ private assignForeignKey(entity: TChild): void {
154
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
155
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
156
+ (entity as Row)[this.relation.foreignKey] = keyValue;
157
+ }
158
+
159
+ private get relationKey(): RelationKey {
160
+ return `${this.rootTable.name}.${this.relationName}`;
161
+ }
162
+
163
+ private populateFromHydrationCache(): void {
164
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
165
+ if (keyValue === undefined || keyValue === null) return;
166
+ const row = getHydrationRecord(this.meta, this.relationName, keyValue);
167
+ if (!row) return;
168
+ this.current = this.createEntity(row);
169
+ this.loaded = true;
170
+ }
171
+ }