metal-orm 1.0.14 → 1.0.16

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 (129) hide show
  1. package/README.md +69 -67
  2. package/dist/decorators/index.cjs +1983 -224
  3. package/dist/decorators/index.cjs.map +1 -1
  4. package/dist/decorators/index.d.cts +6 -6
  5. package/dist/decorators/index.d.ts +6 -6
  6. package/dist/decorators/index.js +1982 -224
  7. package/dist/decorators/index.js.map +1 -1
  8. package/dist/index.cjs +5284 -3751
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +524 -169
  11. package/dist/index.d.ts +524 -169
  12. package/dist/index.js +5197 -3736
  13. package/dist/index.js.map +1 -1
  14. package/dist/{select-CCp1oz9p.d.cts → select-BKZrMRCQ.d.cts} +555 -94
  15. package/dist/{select-CCp1oz9p.d.ts → select-BKZrMRCQ.d.ts} +555 -94
  16. package/package.json +1 -1
  17. package/src/codegen/naming-strategy.ts +64 -0
  18. package/src/codegen/typescript.ts +19 -21
  19. package/src/core/ast/adapters.ts +21 -0
  20. package/src/core/ast/aggregate-functions.ts +13 -13
  21. package/src/core/ast/builders.ts +56 -43
  22. package/src/core/ast/expression-builders.ts +34 -34
  23. package/src/core/ast/expression-nodes.ts +18 -16
  24. package/src/core/ast/expression-visitor.ts +122 -69
  25. package/src/core/ast/expression.ts +6 -4
  26. package/src/core/ast/join-metadata.ts +15 -0
  27. package/src/core/ast/join-node.ts +22 -20
  28. package/src/core/ast/join.ts +5 -5
  29. package/src/core/ast/query.ts +52 -88
  30. package/src/core/ast/types.ts +20 -0
  31. package/src/core/ast/window-functions.ts +55 -55
  32. package/src/core/ddl/dialects/base-schema-dialect.ts +20 -6
  33. package/src/core/ddl/dialects/mssql-schema-dialect.ts +32 -8
  34. package/src/core/ddl/dialects/mysql-schema-dialect.ts +21 -10
  35. package/src/core/ddl/dialects/postgres-schema-dialect.ts +52 -7
  36. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +23 -9
  37. package/src/core/ddl/introspect/catalogs/index.ts +1 -0
  38. package/src/core/ddl/introspect/catalogs/postgres.ts +143 -0
  39. package/src/core/ddl/introspect/context.ts +9 -0
  40. package/src/core/ddl/introspect/functions/postgres.ts +26 -0
  41. package/src/core/ddl/introspect/mssql.ts +149 -149
  42. package/src/core/ddl/introspect/mysql.ts +99 -99
  43. package/src/core/ddl/introspect/postgres.ts +245 -154
  44. package/src/core/ddl/introspect/registry.ts +26 -0
  45. package/src/core/ddl/introspect/run-select.ts +25 -0
  46. package/src/core/ddl/introspect/sqlite.ts +7 -7
  47. package/src/core/ddl/introspect/types.ts +23 -19
  48. package/src/core/ddl/introspect/utils.ts +1 -1
  49. package/src/core/ddl/naming-strategy.ts +10 -0
  50. package/src/core/ddl/schema-dialect.ts +41 -0
  51. package/src/core/ddl/schema-diff.ts +211 -179
  52. package/src/core/ddl/schema-generator.ts +17 -90
  53. package/src/core/ddl/schema-introspect.ts +25 -32
  54. package/src/core/ddl/schema-plan-executor.ts +17 -0
  55. package/src/core/ddl/schema-types.ts +46 -39
  56. package/src/core/ddl/sql-writing.ts +170 -0
  57. package/src/core/dialect/abstract.ts +172 -126
  58. package/src/core/dialect/base/cte-compiler.ts +33 -0
  59. package/src/core/dialect/base/function-table-formatter.ts +132 -0
  60. package/src/core/dialect/base/groupby-compiler.ts +21 -0
  61. package/src/core/dialect/base/join-compiler.ts +26 -0
  62. package/src/core/dialect/base/orderby-compiler.ts +21 -0
  63. package/src/core/dialect/base/pagination-strategy.ts +32 -0
  64. package/src/core/dialect/base/returning-strategy.ts +56 -0
  65. package/src/core/dialect/base/sql-dialect.ts +181 -204
  66. package/src/core/dialect/dialect-factory.ts +91 -0
  67. package/src/core/dialect/mssql/functions.ts +101 -0
  68. package/src/core/dialect/mssql/index.ts +128 -126
  69. package/src/core/dialect/mysql/functions.ts +101 -0
  70. package/src/core/dialect/mysql/index.ts +20 -18
  71. package/src/core/dialect/postgres/functions.ts +95 -0
  72. package/src/core/dialect/postgres/index.ts +30 -28
  73. package/src/core/dialect/sqlite/functions.ts +115 -0
  74. package/src/core/dialect/sqlite/index.ts +30 -28
  75. package/src/core/driver/database-driver.ts +11 -0
  76. package/src/core/driver/mssql-driver.ts +20 -0
  77. package/src/core/driver/mysql-driver.ts +20 -0
  78. package/src/core/driver/postgres-driver.ts +20 -0
  79. package/src/core/driver/sqlite-driver.ts +20 -0
  80. package/src/core/execution/db-executor.ts +63 -0
  81. package/src/core/execution/executors/mssql-executor.ts +39 -0
  82. package/src/core/execution/executors/mysql-executor.ts +47 -0
  83. package/src/core/execution/executors/postgres-executor.ts +32 -0
  84. package/src/core/execution/executors/sqlite-executor.ts +31 -0
  85. package/src/core/functions/datetime.ts +132 -0
  86. package/src/core/functions/numeric.ts +179 -0
  87. package/src/core/functions/standard-strategy.ts +47 -0
  88. package/src/core/functions/text.ts +147 -0
  89. package/src/core/functions/types.ts +18 -0
  90. package/src/core/hydration/types.ts +57 -0
  91. package/src/decorators/bootstrap.ts +10 -0
  92. package/src/decorators/column.ts +13 -4
  93. package/src/decorators/relations.ts +15 -0
  94. package/src/index.ts +37 -19
  95. package/src/orm/entity-context.ts +30 -0
  96. package/src/orm/entity-meta.ts +2 -2
  97. package/src/orm/entity-metadata.ts +8 -6
  98. package/src/orm/entity.ts +72 -41
  99. package/src/orm/execute.ts +42 -25
  100. package/src/orm/execution-context.ts +12 -0
  101. package/src/orm/hydration-context.ts +14 -0
  102. package/src/orm/hydration.ts +25 -17
  103. package/src/orm/identity-map.ts +4 -0
  104. package/src/orm/interceptor-pipeline.ts +29 -0
  105. package/src/orm/lazy-batch.ts +50 -6
  106. package/src/orm/orm-session.ts +234 -0
  107. package/src/orm/orm.ts +58 -0
  108. package/src/orm/query-logger.ts +1 -1
  109. package/src/orm/relation-change-processor.ts +48 -3
  110. package/src/orm/relations/belongs-to.ts +45 -44
  111. package/src/orm/relations/has-many.ts +44 -43
  112. package/src/orm/relations/has-one.ts +140 -0
  113. package/src/orm/relations/many-to-many.ts +46 -45
  114. package/src/orm/transaction-runner.ts +1 -1
  115. package/src/orm/unit-of-work.ts +66 -61
  116. package/src/query-builder/delete.ts +22 -5
  117. package/src/query-builder/hydration-manager.ts +2 -1
  118. package/src/query-builder/hydration-planner.ts +8 -7
  119. package/src/query-builder/insert.ts +22 -5
  120. package/src/query-builder/relation-conditions.ts +9 -8
  121. package/src/query-builder/relation-service.ts +3 -2
  122. package/src/query-builder/select.ts +575 -64
  123. package/src/query-builder/update.ts +22 -5
  124. package/src/schema/column.ts +246 -246
  125. package/src/schema/relation.ts +35 -1
  126. package/src/schema/table.ts +28 -28
  127. package/src/schema/types.ts +41 -31
  128. package/src/orm/db-executor.ts +0 -11
  129. package/src/orm/orm-context.ts +0 -159
@@ -1,7 +1,8 @@
1
- import { SchemaDialect, DialectName, formatLiteral, quoteQualified } from '../schema-generator.js';
1
+ import { SchemaDialect, DialectName } from '../schema-dialect.js';
2
+ import { formatLiteral, quoteQualified, createLiteralFormatter, LiteralFormatter } from '../sql-writing.js';
2
3
  import { ColumnDef, ForeignKeyReference } from '../../../schema/column.js';
3
4
  import { IndexDef, TableDef } from '../../../schema/table.js';
4
- import { DatabaseTable } from '../schema-types.js';
5
+ import { DatabaseTable, DatabaseColumn, ColumnDiff } from '../schema-types.js';
5
6
 
6
7
  type TableLike = { name: string; schema?: string };
7
8
 
@@ -10,7 +11,7 @@ type TableLike = { name: string; schema?: string };
10
11
  * Concrete dialects only override the small surface area instead of reimplementing everything.
11
12
  */
12
13
  export abstract class BaseSchemaDialect implements SchemaDialect {
13
- abstract name: DialectName;
14
+ abstract readonly name: DialectName;
14
15
  abstract quoteIdentifier(id: string): string;
15
16
  abstract renderColumnType(column: ColumnDef): string;
16
17
  abstract renderAutoIncrement(column: ColumnDef, table: TableDef): string | undefined;
@@ -24,8 +25,11 @@ export abstract class BaseSchemaDialect implements SchemaDialect {
24
25
  }
25
26
  return this.quoteIdentifier(table.name);
26
27
  }
28
+ // Each dialect should provide its own formatter
29
+ abstract get literalFormatter(): LiteralFormatter;
30
+
27
31
  renderDefault(value: unknown, _column: ColumnDef): string {
28
- return formatLiteral(value, this.name);
32
+ return formatLiteral(this.literalFormatter, value);
29
33
  }
30
34
  renderReference(ref: ForeignKeyReference, _table: TableDef): string {
31
35
  const parts = ['REFERENCES', quoteQualified(this, ref.table), `(${this.quoteIdentifier(ref.column)})`];
@@ -40,9 +44,19 @@ export abstract class BaseSchemaDialect implements SchemaDialect {
40
44
  dropTableSql(table: DatabaseTable): string[] {
41
45
  return [`DROP TABLE IF EXISTS ${this.formatTableName(table)};`];
42
46
  }
43
- abstract dropColumnSql(table: DatabaseTable, column: string): string[];
44
- abstract dropIndexSql(table: DatabaseTable, index: string): string[];
47
+ dropColumnSql(table: DatabaseTable, column: string): string[] {
48
+ return [`ALTER TABLE ${this.formatTableName(table)} DROP COLUMN ${this.quoteIdentifier(column)};`];
49
+ }
50
+ dropIndexSql(table: DatabaseTable, index: string): string[] {
51
+ return [`DROP INDEX ${this.quoteIdentifier(index)};`];
52
+ }
45
53
  warnDropColumn(_table: DatabaseTable, _column: string): string | undefined {
46
54
  return undefined;
47
55
  }
56
+ alterColumnSql?(table: TableDef, column: ColumnDef, actualColumn: DatabaseColumn, diff: ColumnDiff): string[] {
57
+ return [];
58
+ }
59
+ warnAlterColumn?(_table: TableDef, _column: ColumnDef, _actual: DatabaseColumn, _diff: ColumnDiff): string | undefined {
60
+ return undefined;
61
+ }
48
62
  }
@@ -1,17 +1,23 @@
1
1
  import { BaseSchemaDialect } from './base-schema-dialect.js';
2
- import {
3
- deriveIndexName,
4
- renderIndexColumns,
5
- DialectName,
6
- formatLiteral
7
- } from '../schema-generator.js';
2
+ import { deriveIndexName } from '../naming-strategy.js';
3
+ import { renderIndexColumns, createLiteralFormatter } from '../sql-writing.js';
8
4
  import { ColumnDef } from '../../../schema/column.js';
9
5
  import { IndexDef, TableDef } from '../../../schema/table.js';
10
- import { DatabaseTable } from '../schema-types.js';
6
+ import { ColumnDiff, DatabaseColumn, DatabaseTable } from '../schema-types.js';
7
+ import { DialectName } from '../schema-dialect.js';
11
8
 
12
9
  export class MSSqlSchemaDialect extends BaseSchemaDialect {
13
10
  name: DialectName = 'mssql';
14
11
 
12
+ private _literalFormatter = createLiteralFormatter({
13
+ booleanTrue: '1',
14
+ booleanFalse: '0',
15
+ });
16
+
17
+ get literalFormatter() {
18
+ return this._literalFormatter;
19
+ }
20
+
15
21
  quoteIdentifier(id: string): string {
16
22
  return `[${id.replace(/]/g, ']]')}]`;
17
23
  }
@@ -68,7 +74,7 @@ export class MSSqlSchemaDialect extends BaseSchemaDialect {
68
74
  }
69
75
 
70
76
  renderDefault(value: unknown): string {
71
- return formatLiteral(value, this.name);
77
+ return this.literalFormatter.formatLiteral(value);
72
78
  }
73
79
 
74
80
  renderAutoIncrement(column: ColumnDef): string | undefined {
@@ -94,4 +100,22 @@ export class MSSqlSchemaDialect extends BaseSchemaDialect {
94
100
  dropIndexSql(table: DatabaseTable, index: string): string[] {
95
101
  return [`DROP INDEX ${this.quoteIdentifier(index)} ON ${this.formatTableName(table)};`];
96
102
  }
103
+
104
+ alterColumnSql(table: TableDef, column: ColumnDef, _actual: DatabaseColumn, diff: ColumnDiff): string[] {
105
+ const stmts: string[] = [];
106
+ if (diff.typeChanged || diff.nullabilityChanged) {
107
+ const nullability = column.notNull ? 'NOT NULL' : 'NULL';
108
+ stmts.push(
109
+ `ALTER TABLE ${this.formatTableName(table)} ALTER COLUMN ${this.quoteIdentifier(column.name)} ${this.renderColumnType(column)} ${nullability};`
110
+ );
111
+ }
112
+ return stmts;
113
+ }
114
+
115
+ warnAlterColumn(_table: TableDef, _column: ColumnDef, _actual: DatabaseColumn, diff: ColumnDiff): string | undefined {
116
+ if (diff.defaultChanged || diff.autoIncrementChanged) {
117
+ return 'Altering defaults or identity on MSSQL is not automated (requires dropping/adding default or identity constraints manually).';
118
+ }
119
+ return undefined;
120
+ }
97
121
  }
@@ -1,18 +1,24 @@
1
1
  import { BaseSchemaDialect } from './base-schema-dialect.js';
2
- import {
3
- deriveIndexName,
4
- renderIndexColumns,
5
- DialectName,
6
- formatLiteral,
7
- escapeLiteral
8
- } from '../schema-generator.js';
2
+ import { deriveIndexName } from '../naming-strategy.js';
3
+ import { renderIndexColumns, escapeSqlString, createLiteralFormatter } from '../sql-writing.js';
9
4
  import { ColumnDef } from '../../../schema/column.js';
10
5
  import { IndexDef, TableDef } from '../../../schema/table.js';
11
- import { DatabaseTable } from '../schema-types.js';
6
+ import { ColumnDiff, DatabaseColumn, DatabaseTable } from '../schema-types.js';
7
+ import { renderColumnDefinition } from '../schema-generator.js';
8
+ import { DialectName } from '../schema-dialect.js';
12
9
 
13
10
  export class MySqlSchemaDialect extends BaseSchemaDialect {
14
11
  name: DialectName = 'mysql';
15
12
 
13
+ private _literalFormatter = createLiteralFormatter({
14
+ booleanTrue: '1',
15
+ booleanFalse: '0',
16
+ });
17
+
18
+ get literalFormatter() {
19
+ return this._literalFormatter;
20
+ }
21
+
16
22
  quoteIdentifier(id: string): string {
17
23
  return `\`${id}\``;
18
24
  }
@@ -66,7 +72,7 @@ export class MySqlSchemaDialect extends BaseSchemaDialect {
66
72
  case 'ENUM':
67
73
  case 'enum':
68
74
  return column.args && Array.isArray(column.args) && column.args.length
69
- ? `ENUM(${column.args.map((v: string) => `'${escapeLiteral(v)}'`).join(',')})`
75
+ ? `ENUM(${column.args.map((v: string) => `'${escapeSqlString(v)}'`).join(',')})`
70
76
  : 'ENUM';
71
77
  default:
72
78
  return String(column.type).toUpperCase();
@@ -74,7 +80,7 @@ export class MySqlSchemaDialect extends BaseSchemaDialect {
74
80
  }
75
81
 
76
82
  renderDefault(value: unknown): string {
77
- return formatLiteral(value, this.name);
83
+ return this.literalFormatter.formatLiteral(value);
78
84
  }
79
85
 
80
86
  renderAutoIncrement(column: ColumnDef): string | undefined {
@@ -106,4 +112,9 @@ export class MySqlSchemaDialect extends BaseSchemaDialect {
106
112
  dropIndexSql(table: DatabaseTable, index: string): string[] {
107
113
  return [`DROP INDEX ${this.quoteIdentifier(index)} ON ${this.formatTableName(table)};`];
108
114
  }
115
+
116
+ alterColumnSql(table: TableDef, column: ColumnDef, _actual: DatabaseColumn, _diff: ColumnDiff): string[] {
117
+ const rendered = renderColumnDefinition(table, column, this);
118
+ return [`ALTER TABLE ${this.formatTableName(table)} MODIFY COLUMN ${rendered.sql};`];
119
+ }
109
120
  }
@@ -1,15 +1,22 @@
1
1
  import { BaseSchemaDialect } from './base-schema-dialect.js';
2
- import {
3
- deriveIndexName,
4
- renderIndexColumns,
5
- DialectName
6
- } from '../schema-generator.js';
2
+ import { deriveIndexName } from '../naming-strategy.js';
3
+ import { renderIndexColumns, createLiteralFormatter } from '../sql-writing.js';
7
4
  import { ColumnDef } from '../../../schema/column.js';
8
5
  import { IndexDef, TableDef } from '../../../schema/table.js';
9
- import { DatabaseTable } from '../schema-types.js';
6
+ import { ColumnDiff, DatabaseColumn, DatabaseTable } from '../schema-types.js';
7
+ import { DialectName } from '../schema-dialect.js';
10
8
 
11
9
  export class PostgresSchemaDialect extends BaseSchemaDialect {
12
- name: DialectName = 'postgres';
10
+ readonly name: DialectName = 'postgres';
11
+
12
+ private _literalFormatter = createLiteralFormatter({
13
+ booleanTrue: 'TRUE',
14
+ booleanFalse: 'FALSE',
15
+ });
16
+
17
+ get literalFormatter() {
18
+ return this._literalFormatter;
19
+ }
13
20
 
14
21
  quoteIdentifier(id: string): string {
15
22
  return `"${id}"`;
@@ -96,4 +103,42 @@ export class PostgresSchemaDialect extends BaseSchemaDialect {
96
103
  : this.quoteIdentifier(index);
97
104
  return [`DROP INDEX IF EXISTS ${qualified};`];
98
105
  }
106
+
107
+ alterColumnSql(table: TableDef, column: ColumnDef, actualColumn: DatabaseColumn, diff: ColumnDiff): string[] {
108
+ const stmts: string[] = [];
109
+ const tableName = this.formatTableName(table);
110
+ const colName = this.quoteIdentifier(column.name);
111
+
112
+ if (diff.typeChanged) {
113
+ stmts.push(`ALTER TABLE ${tableName} ALTER COLUMN ${colName} TYPE ${this.renderColumnType(column)};`);
114
+ }
115
+ if (diff.defaultChanged) {
116
+ if (column.default === undefined) {
117
+ stmts.push(`ALTER TABLE ${tableName} ALTER COLUMN ${colName} DROP DEFAULT;`);
118
+ } else {
119
+ stmts.push(
120
+ `ALTER TABLE ${tableName} ALTER COLUMN ${colName} SET DEFAULT ${this.renderDefault(column.default, column)};`
121
+ );
122
+ }
123
+ }
124
+ if (diff.nullabilityChanged) {
125
+ stmts.push(`ALTER TABLE ${tableName} ALTER COLUMN ${colName} ${column.notNull ? 'SET' : 'DROP'} NOT NULL;`);
126
+ }
127
+ if (diff.autoIncrementChanged) {
128
+ if (column.autoIncrement) {
129
+ const strategy = column.generated === 'always' ? 'ALWAYS' : 'BY DEFAULT';
130
+ stmts.push(`ALTER TABLE ${tableName} ALTER COLUMN ${colName} ADD GENERATED ${strategy} AS IDENTITY;`);
131
+ } else {
132
+ stmts.push(`ALTER TABLE ${tableName} ALTER COLUMN ${colName} DROP IDENTITY IF EXISTS;`);
133
+ }
134
+ }
135
+ return stmts;
136
+ }
137
+
138
+ warnAlterColumn(_table: TableDef, _column: ColumnDef, _actual: DatabaseColumn, diff: ColumnDiff): string | undefined {
139
+ if (diff.autoIncrementChanged) {
140
+ return 'Altering identity properties may fail if an existing sequence is attached; verify generated column state.';
141
+ }
142
+ return undefined;
143
+ }
99
144
  }
@@ -1,18 +1,23 @@
1
1
  import { BaseSchemaDialect } from './base-schema-dialect.js';
2
- import {
3
- deriveIndexName,
4
- renderIndexColumns,
5
- DialectName,
6
- formatLiteral,
7
- resolvePrimaryKey
8
- } from '../schema-generator.js';
2
+ import { deriveIndexName } from '../naming-strategy.js';
3
+ import { renderIndexColumns, resolvePrimaryKey, createLiteralFormatter } from '../sql-writing.js';
9
4
  import { ColumnDef } from '../../../schema/column.js';
10
5
  import { IndexDef, TableDef } from '../../../schema/table.js';
11
- import { DatabaseTable } from '../schema-types.js';
6
+ import { ColumnDiff, DatabaseColumn, DatabaseTable } from '../schema-types.js';
7
+ import { DialectName } from '../schema-dialect.js';
12
8
 
13
9
  export class SQLiteSchemaDialect extends BaseSchemaDialect {
14
10
  name: DialectName = 'sqlite';
15
11
 
12
+ private _literalFormatter = createLiteralFormatter({
13
+ booleanTrue: '1',
14
+ booleanFalse: '0',
15
+ });
16
+
17
+ get literalFormatter() {
18
+ return this._literalFormatter;
19
+ }
20
+
16
21
  quoteIdentifier(id: string): string {
17
22
  return `"${id}"`;
18
23
  }
@@ -75,7 +80,7 @@ export class SQLiteSchemaDialect extends BaseSchemaDialect {
75
80
  }
76
81
 
77
82
  renderDefault(value: unknown): string {
78
- return formatLiteral(value, this.name);
83
+ return this.literalFormatter.formatLiteral(value);
79
84
  }
80
85
 
81
86
  renderIndex(table: TableDef, index: IndexDef): string {
@@ -100,4 +105,13 @@ export class SQLiteSchemaDialect extends BaseSchemaDialect {
100
105
  const key = table.schema ? `${table.schema}.${table.name}` : table.name;
101
106
  return `Dropping columns on SQLite requires table rebuild (column ${column} on ${key}).`;
102
107
  }
108
+
109
+ alterColumnSql(_table: TableDef, _column: ColumnDef, _actual: DatabaseColumn, _diff: ColumnDiff): string[] {
110
+ return [];
111
+ }
112
+
113
+ warnAlterColumn(table: TableDef, column: ColumnDef, _actual: DatabaseColumn, _diff: ColumnDiff): string | undefined {
114
+ const key = table.schema ? `${table.schema}.${table.name}` : table.name;
115
+ return `SQLite ALTER COLUMN is not supported; rebuild table ${key} to change column ${column.name}.`;
116
+ }
103
117
  }
@@ -0,0 +1 @@
1
+ export * from './postgres.js';
@@ -0,0 +1,143 @@
1
+ import { defineTable } from '../../../../schema/table.js';
2
+ import { col } from '../../../../schema/column.js';
3
+
4
+ export const PgInformationSchemaColumns = defineTable(
5
+ 'columns',
6
+ {
7
+ table_schema: col.varchar(255),
8
+ table_name: col.varchar(255),
9
+ column_name: col.varchar(255),
10
+ data_type: col.varchar(255),
11
+ is_nullable: col.varchar(3),
12
+ column_default: col.varchar(1024),
13
+ ordinal_position: col.int()
14
+ },
15
+ {},
16
+ undefined,
17
+ { schema: 'information_schema' }
18
+ );
19
+
20
+ export const PgClass = defineTable(
21
+ 'pg_class',
22
+ {
23
+ oid: col.int(),
24
+ relname: col.varchar(255),
25
+ relnamespace: col.int(),
26
+ relkind: col.varchar(1)
27
+ },
28
+ {},
29
+ undefined,
30
+ { schema: 'pg_catalog' }
31
+ );
32
+
33
+ export const PgNamespace = defineTable(
34
+ 'pg_namespace',
35
+ {
36
+ oid: col.int(),
37
+ nspname: col.varchar(255)
38
+ },
39
+ {},
40
+ undefined,
41
+ { schema: 'pg_catalog' }
42
+ );
43
+
44
+ export const PgIndex = defineTable(
45
+ 'pg_index',
46
+ {
47
+ indrelid: col.int(),
48
+ indexrelid: col.int(),
49
+ indisprimary: col.boolean(),
50
+ indkey: col.varchar(255),
51
+ indpred: col.varchar(1024)
52
+ },
53
+ {},
54
+ undefined,
55
+ { schema: 'pg_catalog' }
56
+ );
57
+
58
+ export const PgAttribute = defineTable(
59
+ 'pg_attribute',
60
+ {
61
+ attrelid: col.int(),
62
+ attname: col.varchar(255),
63
+ attnum: col.int()
64
+ },
65
+ {},
66
+ undefined,
67
+ { schema: 'pg_catalog' }
68
+ );
69
+
70
+ export const PgTableConstraints = defineTable(
71
+ 'table_constraints',
72
+ {
73
+ constraint_catalog: col.varchar(255),
74
+ constraint_schema: col.varchar(255),
75
+ constraint_name: col.varchar(255),
76
+ table_catalog: col.varchar(255),
77
+ table_schema: col.varchar(255),
78
+ table_name: col.varchar(255),
79
+ constraint_type: col.varchar(255)
80
+ },
81
+ {},
82
+ undefined,
83
+ { schema: 'information_schema' }
84
+ );
85
+
86
+ export const PgKeyColumnUsage = defineTable(
87
+ 'key_column_usage',
88
+ {
89
+ constraint_catalog: col.varchar(255),
90
+ constraint_schema: col.varchar(255),
91
+ constraint_name: col.varchar(255),
92
+ table_catalog: col.varchar(255),
93
+ table_schema: col.varchar(255),
94
+ table_name: col.varchar(255),
95
+ column_name: col.varchar(255),
96
+ ordinal_position: col.int()
97
+ },
98
+ {},
99
+ undefined,
100
+ { schema: 'information_schema' }
101
+ );
102
+
103
+ export const PgConstraintColumnUsage = defineTable(
104
+ 'constraint_column_usage',
105
+ {
106
+ constraint_catalog: col.varchar(255),
107
+ constraint_schema: col.varchar(255),
108
+ constraint_name: col.varchar(255),
109
+ table_catalog: col.varchar(255),
110
+ table_schema: col.varchar(255),
111
+ table_name: col.varchar(255),
112
+ column_name: col.varchar(255)
113
+ },
114
+ {},
115
+ undefined,
116
+ { schema: 'information_schema' }
117
+ );
118
+
119
+ export const PgReferentialConstraints = defineTable(
120
+ 'referential_constraints',
121
+ {
122
+ constraint_catalog: col.varchar(255),
123
+ constraint_schema: col.varchar(255),
124
+ constraint_name: col.varchar(255),
125
+ unique_constraint_catalog: col.varchar(255),
126
+ unique_constraint_schema: col.varchar(255),
127
+ unique_constraint_name: col.varchar(255),
128
+ match_option: col.varchar(64),
129
+ update_rule: col.varchar(64),
130
+ delete_rule: col.varchar(64)
131
+ },
132
+ {},
133
+ undefined,
134
+ { schema: 'information_schema' }
135
+ );
136
+
137
+ export default {
138
+ PgInformationSchemaColumns,
139
+ PgClass,
140
+ PgNamespace,
141
+ PgIndex,
142
+ PgAttribute
143
+ };
@@ -0,0 +1,9 @@
1
+ import type { Dialect } from '../../dialect/abstract.js';
2
+ import type { DbExecutor } from '../../execution/db-executor.js';
3
+
4
+ export interface IntrospectContext {
5
+ dialect: Dialect;
6
+ executor: DbExecutor;
7
+ }
8
+
9
+ export default IntrospectContext;
@@ -0,0 +1,26 @@
1
+ // Small helpers to build Postgres-specific function calls as AST FunctionNodes
2
+ import { columnOperand, valueToOperand } from '../../../ast/expression-builders.js';
3
+ import type { OperandNode, FunctionNode } from '../../../ast/expression.js';
4
+
5
+ type OperandInput = OperandNode | string | number | boolean | null;
6
+
7
+ const toOperand = (v: OperandInput) => {
8
+ if (v === null) return valueToOperand(null);
9
+ if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') return valueToOperand(v);
10
+ return v as OperandNode;
11
+ };
12
+
13
+ const fn = (name: string, args: OperandInput[]): FunctionNode => ({
14
+ type: 'Function',
15
+ name,
16
+ fn: name,
17
+ args: args.map(toOperand)
18
+ });
19
+
20
+ export const pgGetExpr = (expr: OperandInput, relid: OperandInput): FunctionNode =>
21
+ fn('pg_get_expr', [expr, relid]);
22
+
23
+ export const formatType = (typeOid: OperandInput, typmod: OperandInput): FunctionNode =>
24
+ fn('format_type', [typeOid, typmod]);
25
+
26
+ export default { pgGetExpr, formatType };