metal-orm 1.0.45 → 1.0.47

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 (63) hide show
  1. package/dist/index.cjs +1092 -323
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +99 -9
  4. package/dist/index.d.ts +99 -9
  5. package/dist/index.js +1087 -323
  6. package/dist/index.js.map +1 -1
  7. package/package.json +1 -1
  8. package/scripts/generate-entities.mjs +36 -9
  9. package/src/codegen/typescript.ts +22 -10
  10. package/src/core/ast/adapters.ts +2 -1
  11. package/src/core/ast/expression-builders.ts +13 -0
  12. package/src/core/ast/expression-nodes.ts +25 -5
  13. package/src/core/ast/expression-visitor.ts +5 -0
  14. package/src/core/ast/query.ts +9 -1
  15. package/src/core/ddl/dialects/base-schema-dialect.ts +2 -1
  16. package/src/core/ddl/dialects/mssql-schema-dialect.ts +10 -23
  17. package/src/core/ddl/dialects/mysql-schema-dialect.ts +10 -24
  18. package/src/core/ddl/dialects/postgres-schema-dialect.ts +10 -23
  19. package/src/core/ddl/dialects/render-reference.test.ts +2 -1
  20. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +9 -23
  21. package/src/core/ddl/introspect/catalogs/index.ts +4 -1
  22. package/src/core/ddl/introspect/catalogs/mssql.ts +126 -0
  23. package/src/core/ddl/introspect/catalogs/mysql.ts +89 -0
  24. package/src/core/ddl/introspect/catalogs/postgres.ts +2 -1
  25. package/src/core/ddl/introspect/catalogs/sqlite.ts +47 -0
  26. package/src/core/ddl/introspect/functions/mssql.ts +84 -0
  27. package/src/core/ddl/introspect/mssql.ts +471 -194
  28. package/src/core/ddl/introspect/mysql.ts +336 -125
  29. package/src/core/ddl/introspect/postgres.ts +45 -5
  30. package/src/core/ddl/introspect/run-select.ts +3 -8
  31. package/src/core/ddl/introspect/sqlite.ts +113 -59
  32. package/src/core/ddl/schema-dialect.ts +2 -1
  33. package/src/core/ddl/schema-diff.ts +2 -1
  34. package/src/core/ddl/schema-generator.ts +2 -1
  35. package/src/core/ddl/schema-types.ts +3 -1
  36. package/src/core/ddl/sql-writing.ts +2 -1
  37. package/src/core/dialect/abstract.ts +12 -1
  38. package/src/core/dialect/mssql/index.ts +4 -10
  39. package/src/core/functions/datetime.ts +2 -1
  40. package/src/core/functions/numeric.ts +2 -1
  41. package/src/core/functions/text.ts +2 -1
  42. package/src/decorators/{column.ts → column-decorator.ts} +4 -1
  43. package/src/decorators/index.ts +1 -1
  44. package/src/index.ts +2 -1
  45. package/src/orm/entity-metadata.ts +2 -1
  46. package/src/orm/lazy-batch.ts +2 -1
  47. package/src/orm/orm-session.ts +2 -1
  48. package/src/query-builder/column-selector.ts +2 -1
  49. package/src/query-builder/delete.ts +2 -1
  50. package/src/query-builder/insert.ts +2 -1
  51. package/src/query-builder/query-ast-service.ts +4 -3
  52. package/src/query-builder/relation-projection-helper.ts +2 -1
  53. package/src/query-builder/relation-service.ts +2 -1
  54. package/src/query-builder/select/predicate-facet.ts +2 -1
  55. package/src/query-builder/select/projection-facet.ts +2 -1
  56. package/src/query-builder/select-helpers.ts +2 -1
  57. package/src/query-builder/select-query-state.ts +2 -0
  58. package/src/query-builder/select.ts +2 -1
  59. package/src/query-builder/update.ts +2 -1
  60. package/src/schema/{column.ts → column-types.ts} +317 -290
  61. package/src/schema/table-guards.ts +1 -1
  62. package/src/schema/table.ts +1 -1
  63. package/src/schema/types.ts +10 -8
@@ -1,7 +1,7 @@
1
1
  import { BaseSchemaDialect } from './base-schema-dialect.js';
2
2
  import { deriveIndexName } from '../naming-strategy.js';
3
3
  import { renderIndexColumns, resolvePrimaryKey, createLiteralFormatter } from '../sql-writing.js';
4
- import { ColumnDef } from '../../../schema/column.js';
4
+ import { ColumnDef, normalizeColumnType, renderTypeWithArgs } from '../../../schema/column-types.js';
5
5
  import { IndexDef, TableDef } from '../../../schema/table.js';
6
6
  import { ColumnDiff, DatabaseColumn, DatabaseTable } from '../schema-types.js';
7
7
  import { DialectName } from '../schema-dialect.js';
@@ -24,52 +24,37 @@ export class SQLiteSchemaDialect extends BaseSchemaDialect {
24
24
  }
25
25
 
26
26
  renderColumnType(column: ColumnDef): string {
27
- switch (column.type) {
28
- case 'INT':
29
- case 'INTEGER':
27
+ const override = column.dialectTypes?.[this.name] ?? column.dialectTypes?.default;
28
+ if (override) {
29
+ return renderTypeWithArgs(override, column.args);
30
+ }
31
+
32
+ const type = normalizeColumnType(column.type);
33
+ switch (type) {
30
34
  case 'int':
31
35
  case 'integer':
32
- case 'BIGINT':
33
36
  case 'bigint':
34
37
  return 'INTEGER';
35
- case 'BOOLEAN':
36
38
  case 'boolean':
37
39
  return 'INTEGER';
38
- case 'DECIMAL':
39
40
  case 'decimal':
40
- case 'FLOAT':
41
41
  case 'float':
42
- case 'DOUBLE':
43
42
  case 'double':
44
43
  return 'REAL';
45
- case 'DATE':
46
44
  case 'date':
47
- case 'DATETIME':
48
45
  case 'datetime':
49
- case 'TIMESTAMP':
50
46
  case 'timestamp':
51
- case 'TIMESTAMPTZ':
52
47
  case 'timestamptz':
53
48
  return 'TEXT';
54
- case 'VARCHAR':
55
49
  case 'varchar':
56
- case 'TEXT':
57
50
  case 'text':
58
- case 'JSON':
59
51
  case 'json':
60
- case 'UUID':
61
52
  case 'uuid':
62
- return 'TEXT';
63
- case 'ENUM':
64
53
  case 'enum':
65
54
  return 'TEXT';
66
- case 'BINARY':
67
55
  case 'binary':
68
- case 'VARBINARY':
69
56
  case 'varbinary':
70
- case 'BLOB':
71
57
  case 'blob':
72
- case 'BYTEA':
73
58
  case 'bytea':
74
59
  return 'BLOB';
75
60
  default:
@@ -134,3 +119,4 @@ export class SQLiteSchemaDialect extends BaseSchemaDialect {
134
119
  return `SQLite ALTER COLUMN is not supported; rebuild table ${key} to change column ${column.name}.`;
135
120
  }
136
121
  }
122
+
@@ -1,2 +1,5 @@
1
- /** Re-exports for PostgreSQL catalog definitions. */
1
+ /** Re-exports for supported catalog definitions. */
2
2
  export * from './postgres.js';
3
+ export * from './mssql.js';
4
+ export * from './mysql.js';
5
+ export * from './sqlite.js';
@@ -0,0 +1,126 @@
1
+ import { defineTable } from '../../../../schema/table.js';
2
+ import { col } from '../../../../schema/column-types.js';
3
+
4
+ /** Table definitions for SQL Server catalog views used during introspection. */
5
+ export const SysColumns = defineTable(
6
+ 'columns',
7
+ {
8
+ object_id: col.int(),
9
+ name: col.varchar(255),
10
+ column_id: col.int(),
11
+ max_length: col.int(),
12
+ precision: col.int(),
13
+ scale: col.int(),
14
+ is_nullable: col.boolean(),
15
+ is_identity: col.boolean(),
16
+ default_object_id: col.int(),
17
+ user_type_id: col.int()
18
+ },
19
+ {},
20
+ undefined,
21
+ { schema: 'sys' }
22
+ );
23
+
24
+ export const SysTables = defineTable(
25
+ 'tables',
26
+ {
27
+ object_id: col.int(),
28
+ name: col.varchar(255),
29
+ schema_id: col.int(),
30
+ is_ms_shipped: col.boolean()
31
+ },
32
+ {},
33
+ undefined,
34
+ { schema: 'sys' }
35
+ );
36
+
37
+ export const SysSchemas = defineTable(
38
+ 'schemas',
39
+ {
40
+ schema_id: col.int(),
41
+ name: col.varchar(255)
42
+ },
43
+ {},
44
+ undefined,
45
+ { schema: 'sys' }
46
+ );
47
+
48
+ export const SysTypes = defineTable(
49
+ 'types',
50
+ {
51
+ user_type_id: col.int(),
52
+ name: col.varchar(255)
53
+ },
54
+ {},
55
+ undefined,
56
+ { schema: 'sys' }
57
+ );
58
+
59
+ export const SysIndexes = defineTable(
60
+ 'indexes',
61
+ {
62
+ object_id: col.int(),
63
+ index_id: col.int(),
64
+ name: col.varchar(255),
65
+ is_primary_key: col.boolean(),
66
+ is_unique: col.boolean(),
67
+ has_filter: col.boolean(),
68
+ filter_definition: col.varchar(1024),
69
+ is_hypothetical: col.boolean()
70
+ },
71
+ {},
72
+ undefined,
73
+ { schema: 'sys' }
74
+ );
75
+
76
+ export const SysIndexColumns = defineTable(
77
+ 'index_columns',
78
+ {
79
+ object_id: col.int(),
80
+ index_id: col.int(),
81
+ column_id: col.int(),
82
+ key_ordinal: col.int()
83
+ },
84
+ {},
85
+ undefined,
86
+ { schema: 'sys' }
87
+ );
88
+
89
+ export const SysForeignKeys = defineTable(
90
+ 'foreign_keys',
91
+ {
92
+ object_id: col.int(),
93
+ name: col.varchar(255),
94
+ delete_referential_action_desc: col.varchar(64),
95
+ update_referential_action_desc: col.varchar(64)
96
+ },
97
+ {},
98
+ undefined,
99
+ { schema: 'sys' }
100
+ );
101
+
102
+ export const SysForeignKeyColumns = defineTable(
103
+ 'foreign_key_columns',
104
+ {
105
+ constraint_object_id: col.int(),
106
+ parent_object_id: col.int(),
107
+ parent_column_id: col.int(),
108
+ referenced_object_id: col.int(),
109
+ referenced_column_id: col.int(),
110
+ constraint_column_id: col.int()
111
+ },
112
+ {},
113
+ undefined,
114
+ { schema: 'sys' }
115
+ );
116
+
117
+ export default {
118
+ SysColumns,
119
+ SysTables,
120
+ SysSchemas,
121
+ SysTypes,
122
+ SysIndexes,
123
+ SysIndexColumns,
124
+ SysForeignKeys,
125
+ SysForeignKeyColumns
126
+ };
@@ -0,0 +1,89 @@
1
+ import { defineTable } from '../../../../schema/table.js';
2
+ import { col } from '../../../../schema/column-types.js';
3
+
4
+ const INFORMATION_SCHEMA = 'information_schema';
5
+
6
+ export const InformationSchemaTables = defineTable(
7
+ 'tables',
8
+ {
9
+ table_schema: col.varchar(255),
10
+ table_name: col.varchar(255),
11
+ table_comment: col.varchar(1024)
12
+ },
13
+ {},
14
+ undefined,
15
+ { schema: INFORMATION_SCHEMA }
16
+ );
17
+
18
+ export const InformationSchemaColumns = defineTable(
19
+ 'columns',
20
+ {
21
+ table_schema: col.varchar(255),
22
+ table_name: col.varchar(255),
23
+ column_name: col.varchar(255),
24
+ column_type: col.varchar(255),
25
+ data_type: col.varchar(255),
26
+ is_nullable: col.varchar(3),
27
+ column_default: col.varchar(1024),
28
+ extra: col.varchar(255),
29
+ column_comment: col.varchar(1024),
30
+ ordinal_position: col.int()
31
+ },
32
+ {},
33
+ undefined,
34
+ { schema: INFORMATION_SCHEMA }
35
+ );
36
+
37
+ export const InformationSchemaKeyColumnUsage = defineTable(
38
+ 'key_column_usage',
39
+ {
40
+ constraint_schema: col.varchar(255),
41
+ constraint_name: col.varchar(255),
42
+ table_schema: col.varchar(255),
43
+ table_name: col.varchar(255),
44
+ column_name: col.varchar(255),
45
+ ordinal_position: col.int(),
46
+ referenced_table_schema: col.varchar(255),
47
+ referenced_table_name: col.varchar(255),
48
+ referenced_column_name: col.varchar(255)
49
+ },
50
+ {},
51
+ undefined,
52
+ { schema: INFORMATION_SCHEMA }
53
+ );
54
+
55
+ export const InformationSchemaReferentialConstraints = defineTable(
56
+ 'referential_constraints',
57
+ {
58
+ constraint_schema: col.varchar(255),
59
+ constraint_name: col.varchar(255),
60
+ delete_rule: col.varchar(255),
61
+ update_rule: col.varchar(255)
62
+ },
63
+ {},
64
+ undefined,
65
+ { schema: INFORMATION_SCHEMA }
66
+ );
67
+
68
+ export const InformationSchemaStatistics = defineTable(
69
+ 'statistics',
70
+ {
71
+ table_schema: col.varchar(255),
72
+ table_name: col.varchar(255),
73
+ index_name: col.varchar(255),
74
+ non_unique: col.int(),
75
+ column_name: col.varchar(255),
76
+ seq_in_index: col.int()
77
+ },
78
+ {},
79
+ undefined,
80
+ { schema: INFORMATION_SCHEMA }
81
+ );
82
+
83
+ export default {
84
+ InformationSchemaTables,
85
+ InformationSchemaColumns,
86
+ InformationSchemaKeyColumnUsage,
87
+ InformationSchemaReferentialConstraints,
88
+ InformationSchemaStatistics
89
+ };
@@ -1,5 +1,5 @@
1
1
  import { defineTable } from '../../../../schema/table.js';
2
- import { col } from '../../../../schema/column.js';
2
+ import { col } from '../../../../schema/column-types.js';
3
3
 
4
4
  /** Table definition for information_schema.columns, providing metadata about table columns. */
5
5
  export const PgInformationSchemaColumns = defineTable(
@@ -143,3 +143,4 @@ export default {
143
143
  PgIndex,
144
144
  PgAttribute
145
145
  };
146
+
@@ -0,0 +1,47 @@
1
+ import { defineTable } from '../../../../schema/table.js';
2
+ import { col } from '../../../../schema/column-types.js';
3
+
4
+ // SQLite catalogs are limited; most metadata comes from PRAGMAs, but these tables are queryable.
5
+
6
+ export const SqliteMaster = defineTable(
7
+ 'sqlite_master',
8
+ {
9
+ type: col.varchar(255), // 'table', 'index', 'view', 'trigger'
10
+ name: col.varchar(255), // Object name
11
+ tbl_name: col.varchar(255), // Table the object belongs to
12
+ rootpage: col.int(), // B-tree root page
13
+ sql: col.varchar(4096) // Original DDL
14
+ },
15
+ {},
16
+ undefined,
17
+ { schema: undefined }
18
+ );
19
+
20
+ export const SqliteSequence = defineTable(
21
+ 'sqlite_sequence',
22
+ {
23
+ name: col.varchar(255), // Table name
24
+ seq: col.int() // Last autoincrement value
25
+ },
26
+ {},
27
+ undefined,
28
+ { schema: undefined }
29
+ );
30
+
31
+ export const SqliteStat1 = defineTable(
32
+ 'sqlite_stat1',
33
+ {
34
+ tbl: col.varchar(255), // Table name
35
+ idx: col.varchar(255), // Index name
36
+ stat: col.varchar(255) // Statistics string
37
+ },
38
+ {},
39
+ undefined,
40
+ { schema: undefined }
41
+ );
42
+
43
+ export default {
44
+ SqliteMaster,
45
+ SqliteSequence,
46
+ SqliteStat1
47
+ };
@@ -0,0 +1,84 @@
1
+ import { caseWhen, cast, div, eq, inList, valueToOperand, columnOperand, ValueOperandInput } from '../../../ast/expression-builders.js';
2
+ import { isOperandNode } from '../../../ast/expression-nodes.js';
3
+ import type { OperandNode } from '../../../ast/expression.js';
4
+ import type { ColumnRef } from '../../../ast/types.js';
5
+ import { concat, lower } from '../../../functions/text.js';
6
+
7
+ type OperandInput = OperandNode | ColumnRef | string | number | boolean | null;
8
+
9
+ const isColumnReference = (value: unknown): value is ColumnRef =>
10
+ typeof value === 'object' &&
11
+ value !== null &&
12
+ !('type' in value) &&
13
+ 'name' in value &&
14
+ typeof (value as ColumnRef).name === 'string';
15
+
16
+ const toOperandNode = (value: OperandInput): OperandNode => {
17
+ if (isOperandNode(value)) return value;
18
+ if (isColumnReference(value)) return columnOperand(value);
19
+ return valueToOperand(value as ValueOperandInput);
20
+ };
21
+
22
+ const fn = (name: string, args: OperandInput[]): OperandNode => ({
23
+ type: 'Function',
24
+ name,
25
+ fn: name,
26
+ args: args.map(arg => toOperandNode(arg))
27
+ });
28
+
29
+ const CHAR_TYPES = ['varchar', 'char', 'varbinary', 'binary', 'nvarchar', 'nchar'];
30
+ const DECIMAL_TYPES = ['decimal', 'numeric'];
31
+
32
+ export const objectDefinition = (objectId: OperandInput): OperandNode => fn('OBJECT_DEFINITION', [objectId]);
33
+
34
+ export const buildMssqlDataType = (
35
+ typeName: OperandInput,
36
+ maxLength: OperandInput,
37
+ precision: OperandInput,
38
+ scale: OperandInput
39
+ ): OperandNode => {
40
+ const typeOperand = toOperandNode(typeName);
41
+ const maxLenOperand = toOperandNode(maxLength);
42
+ const precisionOperand = toOperandNode(precision);
43
+ const scaleOperand = toOperandNode(scale);
44
+ const typeLower = lower(typeOperand);
45
+
46
+ const lengthCase = caseWhen(
47
+ [
48
+ {
49
+ when: eq(maxLenOperand, -1),
50
+ then: 'max'
51
+ },
52
+ {
53
+ when: inList(typeLower, ['nvarchar', 'nchar']),
54
+ then: cast(div(maxLenOperand, 2), 'varchar(10)')
55
+ }
56
+ ],
57
+ cast(maxLenOperand, 'varchar(10)')
58
+ );
59
+
60
+ const charSuffix = concat('(', lengthCase, ')');
61
+
62
+ const decimalSuffix = concat(
63
+ '(',
64
+ cast(precisionOperand, 'varchar(10)'),
65
+ ',',
66
+ cast(scaleOperand, 'varchar(10)'),
67
+ ')'
68
+ );
69
+
70
+ const suffix = caseWhen(
71
+ [
72
+ { when: inList(typeLower, CHAR_TYPES), then: charSuffix },
73
+ { when: inList(typeLower, DECIMAL_TYPES), then: decimalSuffix }
74
+ ],
75
+ ''
76
+ );
77
+
78
+ return concat(typeLower, suffix);
79
+ };
80
+
81
+ export default {
82
+ objectDefinition,
83
+ buildMssqlDataType
84
+ };