metal-orm 1.0.42 → 1.0.43

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 (86) hide show
  1. package/README.md +22 -7
  2. package/dist/index.cjs +130 -74
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +121 -96
  5. package/dist/index.d.ts +121 -96
  6. package/dist/index.js +128 -74
  7. package/dist/index.js.map +1 -1
  8. package/package.json +8 -2
  9. package/scripts/run-eslint.mjs +34 -0
  10. package/src/codegen/typescript.ts +32 -15
  11. package/src/core/ast/builders.ts +7 -2
  12. package/src/core/ast/expression-builders.ts +0 -2
  13. package/src/core/ast/expression-nodes.ts +14 -5
  14. package/src/core/ast/expression-visitor.ts +11 -8
  15. package/src/core/ast/join-node.ts +1 -1
  16. package/src/core/ast/query.ts +6 -6
  17. package/src/core/ast/window-functions.ts +10 -2
  18. package/src/core/ddl/dialects/base-schema-dialect.ts +30 -3
  19. package/src/core/ddl/dialects/mssql-schema-dialect.ts +4 -0
  20. package/src/core/ddl/dialects/mysql-schema-dialect.ts +2 -0
  21. package/src/core/ddl/dialects/postgres-schema-dialect.ts +13 -1
  22. package/src/core/ddl/dialects/render-reference.test.ts +69 -0
  23. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +9 -0
  24. package/src/core/ddl/introspect/mssql.ts +42 -8
  25. package/src/core/ddl/introspect/mysql.ts +30 -6
  26. package/src/core/ddl/introspect/postgres.ts +88 -34
  27. package/src/core/ddl/introspect/run-select.ts +6 -4
  28. package/src/core/ddl/introspect/sqlite.ts +56 -11
  29. package/src/core/ddl/introspect/types.ts +0 -1
  30. package/src/core/ddl/introspect/utils.ts +3 -3
  31. package/src/core/ddl/schema-dialect.ts +1 -0
  32. package/src/core/ddl/schema-generator.ts +4 -12
  33. package/src/core/ddl/sql-writing.ts +4 -4
  34. package/src/core/dialect/abstract.ts +18 -6
  35. package/src/core/dialect/base/function-table-formatter.ts +3 -2
  36. package/src/core/dialect/base/join-compiler.ts +5 -3
  37. package/src/core/dialect/base/returning-strategy.ts +1 -0
  38. package/src/core/dialect/base/sql-dialect.ts +3 -3
  39. package/src/core/dialect/mssql/functions.ts +24 -25
  40. package/src/core/dialect/mssql/index.ts +1 -4
  41. package/src/core/dialect/mysql/functions.ts +0 -1
  42. package/src/core/dialect/postgres/functions.ts +33 -34
  43. package/src/core/dialect/postgres/index.ts +1 -0
  44. package/src/core/dialect/sqlite/functions.ts +18 -19
  45. package/src/core/dialect/sqlite/index.ts +2 -0
  46. package/src/core/execution/db-executor.ts +1 -1
  47. package/src/core/execution/executors/mysql-executor.ts +2 -2
  48. package/src/core/execution/executors/postgres-executor.ts +1 -1
  49. package/src/core/execution/pooling/pool.ts +2 -0
  50. package/src/core/functions/datetime.ts +1 -1
  51. package/src/core/functions/numeric.ts +1 -1
  52. package/src/core/functions/text.ts +1 -1
  53. package/src/decorators/bootstrap.ts +27 -8
  54. package/src/decorators/column.ts +3 -11
  55. package/src/decorators/decorator-metadata.ts +3 -9
  56. package/src/decorators/entity.ts +21 -5
  57. package/src/decorators/relations.ts +2 -11
  58. package/src/orm/entity-context.ts +8 -8
  59. package/src/orm/entity-meta.ts +8 -8
  60. package/src/orm/entity-metadata.ts +11 -9
  61. package/src/orm/entity.ts +28 -29
  62. package/src/orm/execute.ts +4 -4
  63. package/src/orm/hydration.ts +42 -39
  64. package/src/orm/identity-map.ts +1 -1
  65. package/src/orm/lazy-batch.ts +9 -9
  66. package/src/orm/orm-session.ts +24 -23
  67. package/src/orm/orm.ts +2 -5
  68. package/src/orm/relation-change-processor.ts +12 -11
  69. package/src/orm/relations/belongs-to.ts +11 -11
  70. package/src/orm/relations/has-many.ts +10 -10
  71. package/src/orm/relations/has-one.ts +8 -7
  72. package/src/orm/relations/many-to-many.ts +13 -13
  73. package/src/orm/runtime-types.ts +4 -4
  74. package/src/orm/save-graph.ts +31 -25
  75. package/src/orm/unit-of-work.ts +17 -17
  76. package/src/query-builder/delete.ts +4 -3
  77. package/src/query-builder/hydration-manager.ts +6 -5
  78. package/src/query-builder/insert.ts +12 -8
  79. package/src/query-builder/query-ast-service.ts +2 -2
  80. package/src/query-builder/raw-column-parser.ts +2 -1
  81. package/src/query-builder/select-helpers.ts +2 -2
  82. package/src/query-builder/select.ts +31 -31
  83. package/src/query-builder/update.ts +4 -3
  84. package/src/schema/column.ts +26 -26
  85. package/src/schema/table.ts +47 -18
  86. package/src/schema/types.ts +22 -22
@@ -81,9 +81,10 @@ export class UpdateQueryBuilder<T> {
81
81
  compile(dialect: UpdateDialectInput): CompiledQuery;
82
82
 
83
83
  compile(arg: UpdateCompiler | UpdateDialectInput): CompiledQuery {
84
- if (typeof (arg as any).compileUpdate === 'function') {
84
+ const candidate = arg as { compileUpdate?: (ast: UpdateQueryNode) => CompiledQuery };
85
+ if (typeof candidate.compileUpdate === 'function') {
85
86
  // UpdateCompiler path – old behavior
86
- return (arg as UpdateCompiler).compileUpdate(this.state.ast);
87
+ return candidate.compileUpdate(this.state.ast);
87
88
  }
88
89
 
89
90
  // Dialect | string path – new behavior
@@ -92,7 +93,7 @@ export class UpdateQueryBuilder<T> {
92
93
  }
93
94
 
94
95
  toSql(arg: UpdateCompiler | UpdateDialectInput): string {
95
- return this.compile(arg as any).sql;
96
+ return this.compile(arg as UpdateCompiler).sql;
96
97
  }
97
98
 
98
99
  getAST(): UpdateQueryNode {
@@ -100,7 +100,7 @@ export interface ColumnDef<T extends ColumnType = ColumnType, TRuntime = unknown
100
100
  /** Column comment/description */
101
101
  comment?: string;
102
102
  /** Additional arguments for the column type (e.g., VARCHAR length) */
103
- args?: any[];
103
+ args?: unknown[];
104
104
  /** Table name this column belongs to (filled at runtime by defineTable) */
105
105
  table?: string;
106
106
  }
@@ -234,28 +234,28 @@ export const col = {
234
234
  * Marks a column as UNIQUE
235
235
  */
236
236
  unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
237
- ({
238
- ...def,
239
- unique: name ?? true
240
- }),
237
+ ({
238
+ ...def,
239
+ unique: name ?? true
240
+ }),
241
241
 
242
242
  /**
243
243
  * Sets a default value for the column
244
244
  */
245
245
  default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
246
- ({
247
- ...def,
248
- default: value
249
- }),
246
+ ({
247
+ ...def,
248
+ default: value
249
+ }),
250
250
 
251
251
  /**
252
252
  * Sets a raw SQL default value for the column
253
253
  */
254
254
  defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
255
- ({
256
- ...def,
257
- default: { raw: expression }
258
- }),
255
+ ({
256
+ ...def,
257
+ default: { raw: expression }
258
+ }),
259
259
 
260
260
  /**
261
261
  * Marks a column as auto-increment / identity
@@ -264,27 +264,27 @@ export const col = {
264
264
  def: ColumnDef<T>,
265
265
  strategy: ColumnDef['generated'] = 'byDefault'
266
266
  ): ColumnDef<T> =>
267
- ({
268
- ...def,
269
- autoIncrement: true,
270
- generated: strategy
271
- }),
267
+ ({
268
+ ...def,
269
+ autoIncrement: true,
270
+ generated: strategy
271
+ }),
272
272
 
273
273
  /**
274
274
  * Adds a foreign key reference
275
275
  */
276
276
  references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
277
- ({
278
- ...def,
279
- references: ref
280
- }),
277
+ ({
278
+ ...def,
279
+ references: ref
280
+ }),
281
281
 
282
282
  /**
283
283
  * Adds a check constraint to the column
284
284
  */
285
285
  check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
286
- ({
287
- ...def,
288
- check: expression
289
- })
286
+ ({
287
+ ...def,
288
+ check: expression
289
+ })
290
290
  };
@@ -31,12 +31,12 @@ export interface TableOptions {
31
31
  }
32
32
 
33
33
  export interface TableHooks {
34
- beforeInsert?(ctx: unknown, entity: any): Promise<void> | void;
35
- afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
36
- beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
37
- afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
38
- beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
39
- afterDelete?(ctx: unknown, entity: any): Promise<void> | void;
34
+ beforeInsert?(ctx: unknown, entity: unknown): Promise<void> | void;
35
+ afterInsert?(ctx: unknown, entity: unknown): Promise<void> | void;
36
+ beforeUpdate?(ctx: unknown, entity: unknown): Promise<void> | void;
37
+ afterUpdate?(ctx: unknown, entity: unknown): Promise<void> | void;
38
+ beforeDelete?(ctx: unknown, entity: unknown): Promise<void> | void;
39
+ afterDelete?(ctx: unknown, entity: unknown): Promise<void> | void;
40
40
  }
41
41
 
42
42
  /**
@@ -94,7 +94,8 @@ export const defineTable = <T extends Record<string, ColumnDef>>(
94
94
  ): TableDef<T> => {
95
95
  // Runtime mutability to assign names to column definitions for convenience
96
96
  const colsWithNames = Object.entries(columns).reduce((acc, [key, def]) => {
97
- (acc as any)[key] = { ...def, name: key, table: name };
97
+ const colDef = { ...def, name: key, table: name };
98
+ (acc as Record<string, unknown>)[key] = colDef;
98
99
  return acc;
99
100
  }, {} as T);
100
101
 
@@ -128,36 +129,39 @@ export type TableRef<T extends TableDef> =
128
129
  $: T["columns"];
129
130
  };
130
131
 
131
- const TABLE_REF_CACHE: WeakMap<object, any> = new WeakMap();
132
+ const TABLE_REF_CACHE: WeakMap<object, unknown> = new WeakMap();
132
133
 
133
134
  const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
134
- const cached = TABLE_REF_CACHE.get(table as any);
135
+ const cached = TABLE_REF_CACHE.get(table);
135
136
  if (cached) return cached as TableRef<T>;
136
137
 
137
- const proxy = new Proxy(table as any, {
138
+ const proxy = new Proxy(table as object, {
138
139
  get(target, prop, receiver) {
139
- if (prop === "$") return target.columns;
140
+ const t = target as TableDef;
141
+ if (prop === "$") return t.columns;
140
142
 
141
143
  // Prefer real table fields first (prevents collision surprises)
142
144
  if (Reflect.has(target, prop)) return Reflect.get(target, prop, receiver);
143
145
 
144
146
  // Fall back to columns bag
145
- if (typeof prop === "string" && prop in target.columns) return target.columns[prop];
147
+ if (typeof prop === "string" && prop in t.columns) return t.columns[prop];
146
148
 
147
149
  return undefined;
148
150
  },
149
151
 
150
152
  has(target, prop) {
153
+ const t = target as TableDef;
151
154
  return (
152
155
  prop === "$" ||
153
156
  Reflect.has(target, prop) ||
154
- (typeof prop === "string" && prop in target.columns)
157
+ (typeof prop === "string" && prop in t.columns)
155
158
  );
156
159
  },
157
160
 
158
161
  ownKeys(target) {
162
+ const t = target as TableDef;
159
163
  const base = Reflect.ownKeys(target);
160
- const cols = Object.keys(target.columns);
164
+ const cols = Object.keys(t.columns);
161
165
 
162
166
  for (const k of cols) {
163
167
  if (!base.includes(k)) base.push(k);
@@ -172,20 +176,20 @@ const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
172
176
  configurable: true,
173
177
  enumerable: false,
174
178
  get() {
175
- return target.columns;
179
+ return (target as TableDef).columns;
176
180
  },
177
181
  };
178
182
  }
179
183
 
180
184
  if (
181
185
  typeof prop === "string" &&
182
- prop in target.columns &&
186
+ prop in (target as TableDef).columns &&
183
187
  !Reflect.has(target, prop)
184
188
  ) {
185
189
  return {
186
190
  configurable: true,
187
191
  enumerable: true,
188
- value: target.columns[prop],
192
+ value: (target as TableDef).columns[prop],
189
193
  writable: false,
190
194
  };
191
195
  }
@@ -194,7 +198,7 @@ const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
194
198
  },
195
199
  }) as TableRef<T>;
196
200
 
197
- TABLE_REF_CACHE.set(table as any, proxy);
201
+ TABLE_REF_CACHE.set(table, proxy);
198
202
  return proxy;
199
203
  };
200
204
 
@@ -208,3 +212,28 @@ const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
208
212
  * t.$.name is the "name" column (escape hatch)
209
213
  */
210
214
  export const tableRef = <T extends TableDef>(table: T): TableRef<T> => withColumnProps(table);
215
+
216
+ /**
217
+ * Public API: dynamic column lookup by string key.
218
+ *
219
+ * Useful when the column name is only known at runtime, or when a column name
220
+ * collides with a real table field (e.g. "name").
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * const t = tableRef(todos);
225
+ * const key = runtimeKey();
226
+ * where(eq(getColumn(t, key), 123));
227
+ * // or: t.$.name (escape hatch)
228
+ * ```
229
+ */
230
+ export function getColumn<T extends TableDef, K extends keyof T['columns'] & string>(table: T, key: K): T['columns'][K];
231
+ export function getColumn<T extends TableDef>(table: T, key: string): ColumnDef;
232
+ export function getColumn<T extends TableDef>(table: T, key: string): ColumnDef {
233
+ const col = table.columns[key] as ColumnDef | undefined;
234
+ if (!col) {
235
+ const tableName = table.name || '<unknown>';
236
+ throw new Error(`Column '${key}' does not exist on table '${tableName}'`);
237
+ }
238
+ return col;
239
+ }
@@ -23,15 +23,15 @@ export type RelationTargetTable<TRel extends RelationDef> =
23
23
  */
24
24
  export type ColumnToTs<T extends ColumnDef> =
25
25
  [unknown] extends [T['tsType']]
26
- ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number :
27
- T['type'] extends 'BIGINT' | 'bigint' ? number | bigint :
28
- T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number :
29
- T['type'] extends 'BOOLEAN' | 'boolean' ? boolean :
30
- T['type'] extends 'JSON' | 'json' ? unknown :
31
- T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer :
32
- T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string :
33
- string
34
- : Exclude<T['tsType'], undefined>;
26
+ ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number :
27
+ T['type'] extends 'BIGINT' | 'bigint' ? number | bigint :
28
+ T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number :
29
+ T['type'] extends 'BOOLEAN' | 'boolean' ? boolean :
30
+ T['type'] extends 'JSON' | 'json' ? unknown :
31
+ T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer :
32
+ T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string :
33
+ string
34
+ : Exclude<T['tsType'], undefined>;
35
35
 
36
36
  /**
37
37
  * Infers a row shape from a table definition
@@ -41,10 +41,10 @@ export type InferRow<TTable extends TableDef> = {
41
41
  };
42
42
 
43
43
  type RelationResult<T extends RelationDef> =
44
- T extends HasManyRelation<infer TTarget> ? InferRow<TTarget>[] :
45
- T extends HasOneRelation<infer TTarget> ? InferRow<TTarget> | null :
46
- T extends BelongsToRelation<infer TTarget> ? InferRow<TTarget> | null :
47
- T extends BelongsToManyRelation<infer TTarget> ? (InferRow<TTarget> & { _pivot?: any })[] :
44
+ T extends HasManyRelation<infer TTarget> ? InferRow<TTarget>[] :
45
+ T extends HasOneRelation<infer TTarget> ? InferRow<TTarget> | null :
46
+ T extends BelongsToRelation<infer TTarget> ? InferRow<TTarget> | null :
47
+ T extends BelongsToManyRelation<infer TTarget> ? (InferRow<TTarget> & { _pivot?: unknown })[] :
48
48
  never;
49
49
 
50
50
  /**
@@ -88,15 +88,15 @@ export type EntityInstance<
88
88
  TRow = InferRow<TTable>
89
89
  > = TRow & {
90
90
  [K in keyof RelationMap<TTable>]:
91
- TTable['relations'][K] extends HasManyRelation<infer TTarget>
92
- ? HasManyCollection<EntityInstance<TTarget>>
93
- : TTable['relations'][K] extends HasOneRelation<infer TTarget>
94
- ? HasOneReference<EntityInstance<TTarget>>
95
- : TTable['relations'][K] extends BelongsToManyRelation<infer TTarget>
96
- ? ManyToManyCollection<EntityInstance<TTarget>>
97
- : TTable['relations'][K] extends BelongsToRelation<infer TTarget>
98
- ? BelongsToReference<EntityInstance<TTarget>>
99
- : never;
91
+ TTable['relations'][K] extends HasManyRelation<infer TTarget>
92
+ ? HasManyCollection<EntityInstance<TTarget>>
93
+ : TTable['relations'][K] extends HasOneRelation<infer TTarget>
94
+ ? HasOneReference<EntityInstance<TTarget>>
95
+ : TTable['relations'][K] extends BelongsToManyRelation<infer TTarget>
96
+ ? ManyToManyCollection<EntityInstance<TTarget>>
97
+ : TTable['relations'][K] extends BelongsToRelation<infer TTarget>
98
+ ? BelongsToReference<EntityInstance<TTarget>>
99
+ : never;
100
100
  } & {
101
101
  $load<K extends keyof RelationMap<TTable>>(relation: K): Promise<RelationMap<TTable>[K]>;
102
102
  };