metal-orm 1.0.104 → 1.0.106

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metal-orm",
3
- "version": "1.0.104",
3
+ "version": "1.0.106",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "engines": {
@@ -1,209 +1,207 @@
1
- import { TableDef } from '../schema/table.js';
2
- import { EntityInstance, HasManyCollection, HasOneReference, BelongsToReference, ManyToManyCollection } from '../schema/types.js';
3
- import { EntityMeta, RelationKey } from './entity-meta.js';
4
- import { DefaultHasManyCollection } from './relations/has-many.js';
5
- import { DefaultHasOneReference } from './relations/has-one.js';
6
- import { DefaultBelongsToReference } from './relations/belongs-to.js';
7
- import { DefaultManyToManyCollection } from './relations/many-to-many.js';
8
- import { HasManyRelation, HasOneRelation, BelongsToRelation, BelongsToManyRelation, RelationKinds } from '../schema/relation.js';
9
- import { loadHasManyRelation, loadHasOneRelation, loadBelongsToRelation, loadBelongsToManyRelation } from './lazy-batch.js';
10
- import { findPrimaryKey } from '../query-builder/hydration-planner.js';
11
- import { relationLoaderCache } from './entity-relation-cache.js';
12
-
13
- export type RelationEntityFactory = (
14
- table: TableDef,
15
- row: Record<string, unknown>
16
- ) => EntityInstance<TableDef>;
17
-
18
- const proxifyRelationWrapper = <T extends object>(wrapper: T): T => {
19
- return new Proxy(wrapper, {
20
- get(target, prop, _receiver) {
21
- if (typeof prop === 'symbol') {
22
- const value = Reflect.get(target, prop, target);
23
- return typeof value === 'function' ? value.bind(target) : value;
24
- }
25
-
26
- if (prop in target) {
27
- const value = Reflect.get(target, prop, target);
28
- return typeof value === 'function' ? value.bind(target) : value;
29
- }
30
-
31
- const getItems = (target as { getItems?: () => unknown }).getItems;
32
- if (typeof getItems === 'function') {
33
- const items = getItems.call(target);
34
- if (items && prop in (items as object)) {
35
- const propName = prop as string;
36
- const value = (items as Record<string, unknown>)[propName];
37
- return typeof value === 'function' ? value.bind(items) : value;
38
- }
39
- }
40
-
41
- const getRef = (target as { get?: () => unknown }).get;
42
- if (typeof getRef === 'function') {
43
- const current = getRef.call(target);
44
- if (current && prop in (current as object)) {
45
- const propName = prop as string;
46
- const value = (current as Record<string, unknown>)[propName];
47
- return typeof value === 'function' ? value.bind(current) : value;
48
- }
49
- }
50
-
51
- return undefined;
52
- },
53
-
54
- set(target, prop, value, _receiver) {
55
- if (typeof prop === 'symbol') {
56
- return Reflect.set(target, prop, value, target);
57
- }
58
-
59
- if (prop in target) {
60
- return Reflect.set(target, prop, value, target);
61
- }
62
-
63
- const getRef = (target as { get?: () => unknown }).get;
64
- if (typeof getRef === 'function') {
65
- const current = getRef.call(target);
66
- if (current && typeof current === 'object') {
67
- return Reflect.set(current as object, prop, value);
68
- }
69
- }
70
-
71
- const getItems = (target as { getItems?: () => unknown }).getItems;
72
- if (typeof getItems === 'function') {
73
- const items = getItems.call(target);
74
- return Reflect.set(items as object, prop, value);
75
- }
76
-
77
- return Reflect.set(target, prop, value, target);
78
- }
79
- });
80
- };
81
-
82
- /**
83
- * Gets a relation wrapper for an entity.
84
- * @param meta - The entity metadata
85
- * @param relationName - The relation name
86
- * @param owner - The owner entity
87
- * @param createEntity - The entity factory for relation rows
88
- * @returns The relation wrapper or undefined
89
- */
90
- export const getRelationWrapper = <TTable extends TableDef>(
91
- meta: EntityMeta<TTable>,
92
- relationName: RelationKey<TTable> | string,
93
- owner: unknown,
94
- createEntity: RelationEntityFactory
95
- ): HasManyCollection<unknown> | HasOneReference<object> | BelongsToReference<object> | ManyToManyCollection<unknown> | undefined => {
96
- const relationKey = relationName as string;
97
-
98
- if (meta.relationWrappers.has(relationKey)) {
99
- return meta.relationWrappers.get(relationKey) as HasManyCollection<unknown>;
100
- }
101
-
102
- const relation = meta.table.relations[relationKey];
103
- if (!relation) return undefined;
104
-
105
- const wrapper = instantiateWrapper(meta, relationKey, relation, owner, createEntity);
106
- if (!wrapper) return undefined;
107
-
108
- const proxied = proxifyRelationWrapper(wrapper as object);
109
- meta.relationWrappers.set(relationKey, proxied);
110
- return proxied as HasManyCollection<unknown>;
111
- };
112
-
113
- /**
114
- * Instantiates the appropriate relation wrapper based on relation type.
115
- * @param meta - The entity metadata
116
- * @param relationName - The relation name
117
- * @param relation - The relation definition
118
- * @param owner - The owner entity
119
- * @param createEntity - The entity factory for relation rows
120
- * @returns The relation wrapper or undefined
121
- */
122
- const instantiateWrapper = <TTable extends TableDef>(
123
- meta: EntityMeta<TTable>,
124
- relationName: string,
125
- relation: HasManyRelation | HasOneRelation | BelongsToRelation | BelongsToManyRelation,
126
- owner: unknown,
127
- createEntity: RelationEntityFactory
128
- ): HasManyCollection<unknown> | HasOneReference<object> | BelongsToReference<object> | ManyToManyCollection<unknown> | undefined => {
129
- const metaBase = meta as unknown as EntityMeta<TableDef>;
130
- const loadCached = <T extends Map<string, unknown>>(factory: () => Promise<T>) =>
131
- relationLoaderCache(metaBase, relationName, factory);
132
- const resolveOptions = () => meta.lazyRelationOptions.get(relationName);
133
- switch (relation.type) {
134
- case RelationKinds.HasOne: {
135
- const hasOne = relation as HasOneRelation;
136
- const localKey = hasOne.localKey || findPrimaryKey(meta.table);
137
- const loader = () => loadCached(() =>
138
- loadHasOneRelation(meta.ctx, meta.table, relationName, hasOne, resolveOptions())
139
- );
140
- return new DefaultHasOneReference(
141
- meta.ctx,
142
- metaBase,
143
- owner,
144
- relationName,
145
- hasOne,
146
- meta.table,
147
- loader,
148
- (row: Record<string, unknown>) => createEntity(hasOne.target, row),
149
- localKey
150
- );
151
- }
152
- case RelationKinds.HasMany: {
153
- const hasMany = relation as HasManyRelation;
154
- const localKey = hasMany.localKey || findPrimaryKey(meta.table);
155
- const loader = () => loadCached(() =>
156
- loadHasManyRelation(meta.ctx, meta.table, relationName, hasMany, resolveOptions())
157
- );
158
- return new DefaultHasManyCollection(
159
- meta.ctx,
160
- metaBase,
161
- owner,
162
- relationName,
163
- hasMany,
164
- meta.table,
165
- loader,
166
- (row: Record<string, unknown>) => createEntity(relation.target, row),
167
- localKey
168
- );
169
- }
170
- case RelationKinds.BelongsTo: {
171
- const belongsTo = relation as BelongsToRelation;
172
- const targetKey = belongsTo.localKey || findPrimaryKey(belongsTo.target);
173
- const loader = () => loadCached(() =>
174
- loadBelongsToRelation(meta.ctx, meta.table, relationName, belongsTo, resolveOptions())
175
- );
176
- return new DefaultBelongsToReference(
177
- meta.ctx,
178
- metaBase,
179
- owner,
180
- relationName,
181
- belongsTo,
182
- meta.table,
183
- loader,
184
- (row: Record<string, unknown>) => createEntity(relation.target, row),
185
- targetKey
186
- );
187
- }
188
- case RelationKinds.BelongsToMany: {
189
- const many = relation as BelongsToManyRelation;
190
- const localKey = many.localKey || findPrimaryKey(meta.table);
191
- const loader = () => loadCached(() =>
192
- loadBelongsToManyRelation(meta.ctx, meta.table, relationName, many, resolveOptions())
193
- );
194
- return new DefaultManyToManyCollection(
195
- meta.ctx,
196
- metaBase,
197
- owner,
198
- relationName,
199
- many,
200
- meta.table,
201
- loader,
202
- (row: Record<string, unknown>) => createEntity(relation.target, row),
203
- localKey
204
- );
205
- }
206
- default:
207
- return undefined;
208
- }
209
- };
1
+ import { TableDef } from '../schema/table.js';
2
+ import { EntityInstance, HasManyCollection, HasOneReference, BelongsToReference, ManyToManyCollection } from '../schema/types.js';
3
+ import { EntityMeta, RelationKey } from './entity-meta.js';
4
+ import { DefaultHasManyCollection } from './relations/has-many.js';
5
+ import { DefaultHasOneReference } from './relations/has-one.js';
6
+ import { DefaultBelongsToReference } from './relations/belongs-to.js';
7
+ import { DefaultManyToManyCollection } from './relations/many-to-many.js';
8
+ import { HasManyRelation, HasOneRelation, BelongsToRelation, BelongsToManyRelation, RelationKinds } from '../schema/relation.js';
9
+ import { loadHasManyRelation, loadHasOneRelation, loadBelongsToRelation, loadBelongsToManyRelation } from './lazy-batch.js';
10
+ import { findPrimaryKey } from '../query-builder/hydration-planner.js';
11
+ import { relationLoaderCache } from './entity-relation-cache.js';
12
+
13
+ export type RelationEntityFactory = (
14
+ table: TableDef,
15
+ row: Record<string, unknown>
16
+ ) => EntityInstance<TableDef>;
17
+
18
+ const proxifyRelationWrapper = <T extends object>(wrapper: T): T => {
19
+ return new Proxy(wrapper, {
20
+ get(target, prop, receiver) {
21
+ if (typeof prop === 'symbol') {
22
+ return Reflect.get(target, prop, receiver);
23
+ }
24
+
25
+ if (prop in target) {
26
+ return Reflect.get(target, prop, receiver);
27
+ }
28
+
29
+ const getItems = (target as { getItems?: () => unknown }).getItems;
30
+ if (typeof getItems === 'function') {
31
+ const items = getItems.call(target);
32
+ if (items && prop in (items as object)) {
33
+ const propName = prop as string;
34
+ const value = (items as Record<string, unknown>)[propName];
35
+ return typeof value === 'function' ? value.bind(items) : value;
36
+ }
37
+ }
38
+
39
+ const getRef = (target as { get?: () => unknown }).get;
40
+ if (typeof getRef === 'function') {
41
+ const current = getRef.call(target);
42
+ if (current && prop in (current as object)) {
43
+ const propName = prop as string;
44
+ const value = (current as Record<string, unknown>)[propName];
45
+ return typeof value === 'function' ? value.bind(current) : value;
46
+ }
47
+ }
48
+
49
+ return undefined;
50
+ },
51
+
52
+ set(target, prop, value, receiver) {
53
+ if (typeof prop === 'symbol') {
54
+ return Reflect.set(target, prop, value, receiver);
55
+ }
56
+
57
+ if (prop in target) {
58
+ return Reflect.set(target, prop, value, receiver);
59
+ }
60
+
61
+ const getRef = (target as { get?: () => unknown }).get;
62
+ if (typeof getRef === 'function') {
63
+ const current = getRef.call(target);
64
+ if (current && typeof current === 'object') {
65
+ return Reflect.set(current as object, prop, value);
66
+ }
67
+ }
68
+
69
+ const getItems = (target as { getItems?: () => unknown }).getItems;
70
+ if (typeof getItems === 'function') {
71
+ const items = getItems.call(target);
72
+ return Reflect.set(items as object, prop, value);
73
+ }
74
+
75
+ return Reflect.set(target, prop, value, receiver);
76
+ }
77
+ });
78
+ };
79
+
80
+ /**
81
+ * Gets a relation wrapper for an entity.
82
+ * @param meta - The entity metadata
83
+ * @param relationName - The relation name
84
+ * @param owner - The owner entity
85
+ * @param createEntity - The entity factory for relation rows
86
+ * @returns The relation wrapper or undefined
87
+ */
88
+ export const getRelationWrapper = <TTable extends TableDef>(
89
+ meta: EntityMeta<TTable>,
90
+ relationName: RelationKey<TTable> | string,
91
+ owner: unknown,
92
+ createEntity: RelationEntityFactory
93
+ ): HasManyCollection<unknown> | HasOneReference<object> | BelongsToReference<object> | ManyToManyCollection<unknown> | undefined => {
94
+ const relationKey = relationName as string;
95
+
96
+ if (meta.relationWrappers.has(relationKey)) {
97
+ return meta.relationWrappers.get(relationKey) as HasManyCollection<unknown>;
98
+ }
99
+
100
+ const relation = meta.table.relations[relationKey];
101
+ if (!relation) return undefined;
102
+
103
+ const wrapper = instantiateWrapper(meta, relationKey, relation, owner, createEntity);
104
+ if (!wrapper) return undefined;
105
+
106
+ const proxied = proxifyRelationWrapper(wrapper as object);
107
+ meta.relationWrappers.set(relationKey, proxied);
108
+ return proxied as HasManyCollection<unknown>;
109
+ };
110
+
111
+ /**
112
+ * Instantiates the appropriate relation wrapper based on relation type.
113
+ * @param meta - The entity metadata
114
+ * @param relationName - The relation name
115
+ * @param relation - The relation definition
116
+ * @param owner - The owner entity
117
+ * @param createEntity - The entity factory for relation rows
118
+ * @returns The relation wrapper or undefined
119
+ */
120
+ const instantiateWrapper = <TTable extends TableDef>(
121
+ meta: EntityMeta<TTable>,
122
+ relationName: string,
123
+ relation: HasManyRelation | HasOneRelation | BelongsToRelation | BelongsToManyRelation,
124
+ owner: unknown,
125
+ createEntity: RelationEntityFactory
126
+ ): HasManyCollection<unknown> | HasOneReference<object> | BelongsToReference<object> | ManyToManyCollection<unknown> | undefined => {
127
+ const metaBase = meta as unknown as EntityMeta<TableDef>;
128
+ const loadCached = <T extends Map<string, unknown>>(factory: () => Promise<T>) =>
129
+ relationLoaderCache(metaBase, relationName, factory);
130
+ const resolveOptions = () => meta.lazyRelationOptions.get(relationName);
131
+ switch (relation.type) {
132
+ case RelationKinds.HasOne: {
133
+ const hasOne = relation as HasOneRelation;
134
+ const localKey = hasOne.localKey || findPrimaryKey(meta.table);
135
+ const loader = () => loadCached(() =>
136
+ loadHasOneRelation(meta.ctx, meta.table, relationName, hasOne, resolveOptions())
137
+ );
138
+ return new DefaultHasOneReference(
139
+ meta.ctx,
140
+ metaBase,
141
+ owner,
142
+ relationName,
143
+ hasOne,
144
+ meta.table,
145
+ loader,
146
+ (row: Record<string, unknown>) => createEntity(hasOne.target, row),
147
+ localKey
148
+ );
149
+ }
150
+ case RelationKinds.HasMany: {
151
+ const hasMany = relation as HasManyRelation;
152
+ const localKey = hasMany.localKey || findPrimaryKey(meta.table);
153
+ const loader = () => loadCached(() =>
154
+ loadHasManyRelation(meta.ctx, meta.table, relationName, hasMany, resolveOptions())
155
+ );
156
+ return new DefaultHasManyCollection(
157
+ meta.ctx,
158
+ metaBase,
159
+ owner,
160
+ relationName,
161
+ hasMany,
162
+ meta.table,
163
+ loader,
164
+ (row: Record<string, unknown>) => createEntity(relation.target, row),
165
+ localKey
166
+ );
167
+ }
168
+ case RelationKinds.BelongsTo: {
169
+ const belongsTo = relation as BelongsToRelation;
170
+ const targetKey = belongsTo.localKey || findPrimaryKey(belongsTo.target);
171
+ const loader = () => loadCached(() =>
172
+ loadBelongsToRelation(meta.ctx, meta.table, relationName, belongsTo, resolveOptions())
173
+ );
174
+ return new DefaultBelongsToReference(
175
+ meta.ctx,
176
+ metaBase,
177
+ owner,
178
+ relationName,
179
+ belongsTo,
180
+ meta.table,
181
+ loader,
182
+ (row: Record<string, unknown>) => createEntity(relation.target, row),
183
+ targetKey
184
+ );
185
+ }
186
+ case RelationKinds.BelongsToMany: {
187
+ const many = relation as BelongsToManyRelation;
188
+ const localKey = many.localKey || findPrimaryKey(meta.table);
189
+ const loader = () => loadCached(() =>
190
+ loadBelongsToManyRelation(meta.ctx, meta.table, relationName, many, resolveOptions())
191
+ );
192
+ return new DefaultManyToManyCollection(
193
+ meta.ctx,
194
+ metaBase,
195
+ owner,
196
+ relationName,
197
+ many,
198
+ meta.table,
199
+ loader,
200
+ (row: Record<string, unknown>) => createEntity(relation.target, row),
201
+ localKey
202
+ );
203
+ }
204
+ default:
205
+ return undefined;
206
+ }
207
+ };