metal-orm 1.1.8 → 1.1.10

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 (75) hide show
  1. package/README.md +769 -764
  2. package/dist/index.cjs +2352 -226
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +605 -40
  5. package/dist/index.d.ts +605 -40
  6. package/dist/index.js +2324 -226
  7. package/dist/index.js.map +1 -1
  8. package/package.json +22 -17
  9. package/src/bulk/bulk-context.ts +83 -0
  10. package/src/bulk/bulk-delete-executor.ts +89 -0
  11. package/src/bulk/bulk-executor.base.ts +73 -0
  12. package/src/bulk/bulk-insert-executor.ts +74 -0
  13. package/src/bulk/bulk-types.ts +70 -0
  14. package/src/bulk/bulk-update-executor.ts +192 -0
  15. package/src/bulk/bulk-upsert-executor.ts +95 -0
  16. package/src/bulk/bulk-utils.ts +91 -0
  17. package/src/bulk/index.ts +18 -0
  18. package/src/codegen/typescript.ts +30 -21
  19. package/src/core/ast/expression-builders.ts +107 -10
  20. package/src/core/ast/expression-nodes.ts +52 -22
  21. package/src/core/ast/expression-visitor.ts +23 -13
  22. package/src/core/dialect/abstract.ts +30 -17
  23. package/src/core/dialect/mysql/index.ts +20 -5
  24. package/src/core/execution/db-executor.ts +96 -64
  25. package/src/core/execution/executors/better-sqlite3-executor.ts +94 -0
  26. package/src/core/execution/executors/mssql-executor.ts +66 -34
  27. package/src/core/execution/executors/mysql-executor.ts +98 -66
  28. package/src/core/execution/executors/postgres-executor.ts +33 -11
  29. package/src/core/execution/executors/sqlite-executor.ts +86 -30
  30. package/src/decorators/bootstrap.ts +482 -398
  31. package/src/decorators/column-decorator.ts +87 -96
  32. package/src/decorators/decorator-metadata.ts +100 -24
  33. package/src/decorators/entity.ts +27 -24
  34. package/src/decorators/relations.ts +231 -149
  35. package/src/decorators/transformers/transformer-decorators.ts +26 -29
  36. package/src/decorators/validators/country-validators-decorators.ts +9 -15
  37. package/src/dto/apply-filter.ts +568 -551
  38. package/src/index.ts +16 -9
  39. package/src/orm/entity-hydration.ts +116 -72
  40. package/src/orm/entity-metadata.ts +347 -301
  41. package/src/orm/entity-relations.ts +264 -207
  42. package/src/orm/entity.ts +199 -199
  43. package/src/orm/execute.ts +13 -13
  44. package/src/orm/lazy-batch/morph-many.ts +70 -0
  45. package/src/orm/lazy-batch/morph-one.ts +69 -0
  46. package/src/orm/lazy-batch/morph-to.ts +59 -0
  47. package/src/orm/lazy-batch.ts +4 -1
  48. package/src/orm/orm-session.ts +170 -104
  49. package/src/orm/pooled-executor-factory.ts +99 -58
  50. package/src/orm/query-logger.ts +49 -40
  51. package/src/orm/relation-change-processor.ts +198 -96
  52. package/src/orm/relations/belongs-to.ts +143 -143
  53. package/src/orm/relations/has-many.ts +204 -204
  54. package/src/orm/relations/has-one.ts +174 -174
  55. package/src/orm/relations/many-to-many.ts +288 -288
  56. package/src/orm/relations/morph-many.ts +156 -0
  57. package/src/orm/relations/morph-one.ts +151 -0
  58. package/src/orm/relations/morph-to.ts +162 -0
  59. package/src/orm/save-graph.ts +116 -1
  60. package/src/query-builder/expression-table-mapper.ts +5 -0
  61. package/src/query-builder/hydration-manager.ts +345 -345
  62. package/src/query-builder/hydration-planner.ts +178 -148
  63. package/src/query-builder/relation-conditions.ts +171 -151
  64. package/src/query-builder/relation-cte-builder.ts +5 -1
  65. package/src/query-builder/relation-filter-utils.ts +9 -6
  66. package/src/query-builder/relation-include-strategies.ts +44 -2
  67. package/src/query-builder/relation-join-strategies.ts +8 -1
  68. package/src/query-builder/relation-service.ts +250 -241
  69. package/src/query-builder/select/cursor-pagination.ts +323 -0
  70. package/src/query-builder/select/select-operations.ts +110 -105
  71. package/src/query-builder/select.ts +42 -1
  72. package/src/query-builder/update-include.ts +4 -0
  73. package/src/schema/relation.ts +296 -188
  74. package/src/schema/types.ts +138 -123
  75. package/src/tree/tree-decorator.ts +127 -137
@@ -1,204 +1,204 @@
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(): unknown[] {
197
- return this.items.map(item => {
198
- const entityWithToJSON = item as { toJSON?: () => unknown };
199
- return typeof entityWithToJSON.toJSON === 'function'
200
- ? entityWithToJSON.toJSON()
201
- : item;
202
- });
203
- }
204
- }
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(): unknown[] {
197
+ return this.items.map(item => {
198
+ const entityWithToJSON = item as { toJSON?: () => unknown };
199
+ return typeof entityWithToJSON.toJSON === 'function'
200
+ ? entityWithToJSON.toJSON()
201
+ : item;
202
+ });
203
+ }
204
+ }