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,174 +1,174 @@
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(): unknown {
134
- if (!this.current) return null;
135
- const entityWithToJSON = this.current as { toJSON?: () => unknown };
136
- return typeof entityWithToJSON.toJSON === 'function'
137
- ? entityWithToJSON.toJSON()
138
- : this.current;
139
- }
140
-
141
- private detachCurrent(): TChild | null {
142
- const previous = this.current;
143
- if (!previous) return null;
144
- this.current = null;
145
- this.loaded = true;
146
- this.ctx.registerRelationChange(
147
- this.root,
148
- this.relationKey,
149
- this.rootTable,
150
- this.relationName,
151
- this.relation,
152
- { kind: 'remove', entity: previous }
153
- );
154
- return null;
155
- }
156
-
157
- private assignForeignKey(entity: TChild): void {
158
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
159
- (entity as Row)[this.relation.foreignKey] = keyValue;
160
- }
161
-
162
- private get relationKey(): RelationKey {
163
- return `${this.rootTable.name}.${this.relationName}`;
164
- }
165
-
166
- private populateFromHydrationCache(): void {
167
- const keyValue = (this.root as Record<string, unknown>)[this.localKey];
168
- if (keyValue === undefined || keyValue === null) return;
169
- const row = getHydrationRecord(this.meta, this.relationName, keyValue);
170
- if (!row) return;
171
- this.current = this.createEntity(row);
172
- this.loaded = true;
173
- }
174
- }
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(): unknown {
134
+ if (!this.current) return null;
135
+ const entityWithToJSON = this.current as { toJSON?: () => unknown };
136
+ return typeof entityWithToJSON.toJSON === 'function'
137
+ ? entityWithToJSON.toJSON()
138
+ : this.current;
139
+ }
140
+
141
+ private detachCurrent(): TChild | null {
142
+ const previous = this.current;
143
+ if (!previous) return null;
144
+ this.current = null;
145
+ this.loaded = true;
146
+ this.ctx.registerRelationChange(
147
+ this.root,
148
+ this.relationKey,
149
+ this.rootTable,
150
+ this.relationName,
151
+ this.relation,
152
+ { kind: 'remove', entity: previous }
153
+ );
154
+ return null;
155
+ }
156
+
157
+ private assignForeignKey(entity: TChild): void {
158
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
159
+ (entity as Row)[this.relation.foreignKey] = keyValue;
160
+ }
161
+
162
+ private get relationKey(): RelationKey {
163
+ return `${this.rootTable.name}.${this.relationName}`;
164
+ }
165
+
166
+ private populateFromHydrationCache(): void {
167
+ const keyValue = (this.root as Record<string, unknown>)[this.localKey];
168
+ if (keyValue === undefined || keyValue === null) return;
169
+ const row = getHydrationRecord(this.meta, this.relationName, keyValue);
170
+ if (!row) return;
171
+ this.current = this.createEntity(row);
172
+ this.loaded = true;
173
+ }
174
+ }