metal-orm 1.0.58 → 1.0.60

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.
Files changed (41) hide show
  1. package/README.md +34 -31
  2. package/dist/index.cjs +1583 -901
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +400 -129
  5. package/dist/index.d.ts +400 -129
  6. package/dist/index.js +1575 -901
  7. package/dist/index.js.map +1 -1
  8. package/package.json +1 -1
  9. package/src/core/ddl/schema-generator.ts +44 -1
  10. package/src/decorators/bootstrap.ts +183 -146
  11. package/src/decorators/column-decorator.ts +8 -49
  12. package/src/decorators/decorator-metadata.ts +10 -46
  13. package/src/decorators/entity.ts +30 -40
  14. package/src/decorators/relations.ts +30 -56
  15. package/src/index.ts +7 -7
  16. package/src/orm/entity-hydration.ts +72 -0
  17. package/src/orm/entity-meta.ts +13 -11
  18. package/src/orm/entity-metadata.ts +240 -238
  19. package/src/orm/entity-relation-cache.ts +39 -0
  20. package/src/orm/entity-relations.ts +207 -0
  21. package/src/orm/entity.ts +124 -410
  22. package/src/orm/execute.ts +4 -4
  23. package/src/orm/lazy-batch/belongs-to-many.ts +134 -0
  24. package/src/orm/lazy-batch/belongs-to.ts +108 -0
  25. package/src/orm/lazy-batch/has-many.ts +69 -0
  26. package/src/orm/lazy-batch/has-one.ts +68 -0
  27. package/src/orm/lazy-batch/shared.ts +125 -0
  28. package/src/orm/lazy-batch.ts +4 -492
  29. package/src/orm/relations/many-to-many.ts +2 -1
  30. package/src/query-builder/relation-cte-builder.ts +63 -0
  31. package/src/query-builder/relation-filter-utils.ts +159 -0
  32. package/src/query-builder/relation-include-strategies.ts +177 -0
  33. package/src/query-builder/relation-join-planner.ts +80 -0
  34. package/src/query-builder/relation-service.ts +119 -479
  35. package/src/query-builder/relation-types.ts +41 -10
  36. package/src/query-builder/select/projection-facet.ts +23 -23
  37. package/src/query-builder/select/select-operations.ts +145 -0
  38. package/src/query-builder/select.ts +329 -221
  39. package/src/schema/relation.ts +22 -18
  40. package/src/schema/table.ts +22 -9
  41. package/src/schema/types.ts +14 -12
@@ -0,0 +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
+ 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 lazyOptions = meta.lazyRelationOptions.get(relationName);
129
+ const loadCached = <T extends Map<string, unknown>>(factory: () => Promise<T>) =>
130
+ relationLoaderCache(metaBase, relationName, factory);
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, lazyOptions)
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, lazyOptions)
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, lazyOptions)
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, lazyOptions)
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
+ };