uql-orm 0.96.1 → 0.98.0

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 (117) hide show
  1. package/dist/dialect/abstractSqlDialect.d.ts +1 -7
  2. package/dist/dialect/abstractSqlDialect.js +1 -9
  3. package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
  4. package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
  5. package/dist/dialect/pgLikeSqlDialect.js +0 -1
  6. package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
  7. package/dist/migrate/acquireQuerierForMigrations.js +10 -18
  8. package/dist/migrate/builder/columnBuilder.d.ts +1 -2
  9. package/dist/migrate/builder/columnBuilder.js +3 -10
  10. package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
  11. package/dist/migrate/builder/migrationBuilder.js +21 -78
  12. package/dist/migrate/builder/tableBuilder.js +2 -3
  13. package/dist/migrate/builder/types.d.ts +59 -120
  14. package/dist/migrate/cli-config.d.ts +1 -0
  15. package/dist/migrate/cli-config.js +11 -26
  16. package/dist/migrate/cli.d.ts +0 -13
  17. package/dist/migrate/cli.js +55 -118
  18. package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
  19. package/dist/migrate/codegen/entityCodeGenerator.js +53 -174
  20. package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
  21. package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
  22. package/dist/migrate/codegen/index.d.ts +2 -7
  23. package/dist/migrate/codegen/index.js +2 -8
  24. package/dist/migrate/codegen/migrationFile.d.ts +1 -8
  25. package/dist/migrate/codegen/migrationFile.js +9 -13
  26. package/dist/migrate/ddl/defaultSql.d.ts +2 -1
  27. package/dist/migrate/ddl/defaultSql.js +1 -3
  28. package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
  29. package/dist/migrate/ddl/indexCapabilities.js +60 -0
  30. package/dist/migrate/ddl/indexDdl.d.ts +11 -24
  31. package/dist/migrate/ddl/indexDdl.js +22 -28
  32. package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
  33. package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
  34. package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
  35. package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
  36. package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
  37. package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
  38. package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
  39. package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
  40. package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
  41. package/dist/migrate/ddl/pgIndexDdl.js +5 -44
  42. package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
  43. package/dist/migrate/ddl/pgTableDdl.js +17 -1
  44. package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
  45. package/dist/migrate/ddl/tableDdl.d.ts +43 -24
  46. package/dist/migrate/ddl/tableDdl.js +56 -37
  47. package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
  48. package/dist/migrate/drift/driftDetector.d.ts +29 -32
  49. package/dist/migrate/drift/driftDetector.js +155 -205
  50. package/dist/migrate/drift/index.d.ts +1 -6
  51. package/dist/migrate/drift/index.js +1 -6
  52. package/dist/migrate/generator/definitionToNode.d.ts +2 -2
  53. package/dist/migrate/generator/definitionToNode.js +6 -5
  54. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
  55. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
  56. package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
  57. package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
  58. package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
  59. package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
  60. package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
  61. package/dist/migrate/introspection/postgresIntrospector.js +74 -77
  62. package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
  63. package/dist/migrate/introspection/sqliteIntrospector.js +73 -119
  64. package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
  65. package/dist/migrate/introspection/tableSchemaAST.js +85 -0
  66. package/dist/migrate/migrationTarget.js +10 -2
  67. package/dist/migrate/migrator.d.ts +9 -44
  68. package/dist/migrate/migrator.js +72 -150
  69. package/dist/migrate/schemaChange.d.ts +8 -5
  70. package/dist/migrate/schemaChange.js +25 -8
  71. package/dist/migrate/schemaGenerator.d.ts +35 -109
  72. package/dist/migrate/schemaGenerator.js +179 -353
  73. package/dist/migrate/storage/databaseStorage.d.ts +2 -1
  74. package/dist/migrate/storage/databaseStorage.js +3 -2
  75. package/dist/migrate/storage/jsonStorage.d.ts +1 -2
  76. package/dist/migrate/storage/jsonStorage.js +11 -20
  77. package/dist/migrate/triggerSql.d.ts +7 -9
  78. package/dist/migrate/triggerSql.js +17 -18
  79. package/dist/mongo/index.d.ts +0 -1
  80. package/dist/mongo/index.js +0 -1
  81. package/dist/mongo/mongoDialect.js +0 -1
  82. package/dist/mongo/mongoIntrospector.d.ts +1 -6
  83. package/dist/mongo/mongoIntrospector.js +7 -18
  84. package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
  85. package/dist/mongo/mongoSchemaGenerator.js +51 -75
  86. package/dist/mssql/mssqlDialect.js +0 -1
  87. package/dist/schema/canonicalType.d.ts +15 -19
  88. package/dist/schema/canonicalType.js +20 -43
  89. package/dist/schema/dependencyGraph.d.ts +2 -3
  90. package/dist/schema/dependencyGraph.js +0 -4
  91. package/dist/schema/indexDifferences.js +2 -2
  92. package/dist/schema/schemaAST.d.ts +1 -1
  93. package/dist/schema/schemaAST.js +5 -12
  94. package/dist/schema/schemaASTBuilder.d.ts +5 -6
  95. package/dist/schema/schemaASTBuilder.js +17 -18
  96. package/dist/schema/schemaASTDiffer.d.ts +4 -5
  97. package/dist/schema/schemaASTDiffer.js +40 -73
  98. package/dist/schema/types.d.ts +24 -54
  99. package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
  100. package/dist/sqlite/localSqliteQuerierPool.js +20 -4
  101. package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
  102. package/dist/sqlite/sqliteDialect.js +0 -1
  103. package/dist/sqlite/sqliteQuerierPool.js +3 -3
  104. package/dist/type/dialect.d.ts +0 -2
  105. package/dist/type/migration.d.ts +13 -73
  106. package/dist/util/ddlExpression.util.d.ts +13 -2
  107. package/dist/util/ddlExpression.util.js +27 -4
  108. package/dist/util/sql.util.d.ts +2 -12
  109. package/dist/util/sql.util.js +9 -18
  110. package/package.json +1 -1
  111. package/skills/uql-orm/SKILL.md +1 -1
  112. package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
  113. package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
  114. package/dist/schema/index.d.ts +0 -2
  115. package/dist/schema/index.js +0 -1
  116. package/dist/sqlite/sqlitePragmas.d.ts +0 -3
  117. package/dist/sqlite/sqlitePragmas.js +0 -15
@@ -1,133 +0,0 @@
1
- import { canonicalColumnType, defaultTimestampPrecision, withTimestampPrecision } from '../../schema/canonicalType.js';
2
- import { createTableNode, keyOfColumns, SchemaAST } from '../../schema/schemaAST.js';
3
- import { DEFAULT_FOREIGN_KEY_ACTION, } from '../../schema/types.js';
4
- import { derivedForeignKeyName, qualifyName } from '../../util/sql.util.js';
5
- /**
6
- * Base class for SQL introspectors with shared AST building logic.
7
- */
8
- export class BaseSqlIntrospector {
9
- dialect;
10
- schema;
11
- /** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
12
- indexFacets = new Set();
13
- /**
14
- * The one schema these queries read, `undefined` for the connection's own default. Every table
15
- * reported is stamped with it, so a diff compares like with like: entity and database both say
16
- * `undefined` for "wherever the connection points", and name a schema only when one was asked for.
17
- */
18
- constructor(dialect, schema) {
19
- this.dialect = dialect;
20
- this.schema = schema;
21
- }
22
- /**
23
- * The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
24
- * rather than raised: the point of naming them is to read a database other things are still
25
- * changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
26
- */
27
- async introspect(tables, renames) {
28
- const tableNames = tables ?? (await this.getTableNames());
29
- const tableSchemas = [];
30
- for (const tableName of tableNames) {
31
- const schema = await this.getTableSchema(tableName);
32
- if (schema) {
33
- tableSchemas.push(renames ? this.renamed(schema, renames) : schema);
34
- }
35
- }
36
- return this.buildAST(tableSchemas);
37
- }
38
- /** `table` with each column `renames` names under its new name, wherever the table names it. */
39
- renamed(table, renames) {
40
- const nameIn = (tableName) => (column) => renames.get(qualifyName(tableName, this.schema))?.find((rename) => rename.from === column)?.to ?? column;
41
- const own = nameIn(table.name);
42
- return {
43
- ...table,
44
- columns: table.columns.map((column) => ({ ...column, name: own(column.name) })),
45
- primaryKey: table.primaryKey && { ...table.primaryKey, columns: table.primaryKey.columns.map(own) },
46
- indexes: table.indexes?.map((index) => ({
47
- ...index,
48
- entries: index.entries.map((entry) => (entry.expression ? entry : { ...entry, column: own(entry.column) })),
49
- })),
50
- foreignKeys: table.foreignKeys?.map((foreignKey) => ({
51
- ...foreignKey,
52
- columns: foreignKey.columns.map(own),
53
- references: {
54
- ...foreignKey.references,
55
- columns: foreignKey.references.columns.map(nameIn(foreignKey.references.table)),
56
- },
57
- })),
58
- };
59
- }
60
- /**
61
- * Build SchemaAST from table schemas.
62
- */
63
- buildAST(tableSchemas) {
64
- const ast = new SchemaAST();
65
- // Every table first, so a foreign key reaches a table introspected after the one it leaves from.
66
- const tables = tableSchemas.map((schema) => ({ schema, table: this.buildTable(schema) }));
67
- const tableNodes = new Map(tables.map(({ schema, table }) => [schema.name, table]));
68
- for (const { table } of tables) {
69
- ast.addTable(table);
70
- }
71
- for (const { schema, table } of tables) {
72
- this.buildRelationships(ast, tableNodes, schema, table);
73
- this.buildIndexes(ast, schema, table);
74
- }
75
- return ast;
76
- }
77
- buildTable(schema) {
78
- const table = createTableNode(schema.name, this.schema, this.indexFacets);
79
- const { columns } = table;
80
- for (const col of schema.columns) {
81
- // Spread, not field by field: a `ColumnSchema` is a `ColumnNode` minus the graph links, so
82
- // everything but the type crosses unchanged and a field either shape gains cannot be dropped
83
- // here. Listed by hand this had already lost `enum` and `generatedAs`.
84
- const { type, length: _length, precision: _precision, scale: _scale, ...rest } = col;
85
- const column = {
86
- ...rest,
87
- // A timestamp with no stated precision has the engine's default; otherwise an entity would assume 3.
88
- type: withTimestampPrecision(canonicalColumnType(type, col), defaultTimestampPrecision(this.dialect.dialectName)),
89
- table,
90
- referencedBy: [],
91
- };
92
- columns.set(col.name, column);
93
- }
94
- // From the ordered key the query returned, not from the per-column flags: `(a, b)` is a different
95
- // key from `(b, a)`, and a flag says only that a column is *in* the key. Falls back to the flags
96
- // for an introspector that reports no key of its own.
97
- table.primaryKey = schema.primaryKey ?? keyOfColumns(schema.columns);
98
- table.definition = schema.definition;
99
- return table;
100
- }
101
- buildRelationships(ast, tableNodes, schema, fromTable) {
102
- for (const fk of schema.foreignKeys ?? []) {
103
- const toTable = tableNodes.get(fk.references.table);
104
- if (!toTable) {
105
- fromTable.externalForeignKeys.push(fk);
106
- continue;
107
- }
108
- const fromColumns = fk.columns.flatMap((name) => fromTable.columns.get(name) ?? []);
109
- const toColumns = fk.references.columns.flatMap((name) => toTable.columns.get(name) ?? []);
110
- if (fromColumns.length > 0 && toColumns.length > 0) {
111
- const rel = {
112
- name: fk.name ?? derivedForeignKeyName(schema.name, fk.columns),
113
- type: fromColumns[0].isUnique ? 'OneToOne' : 'ManyToOne',
114
- from: { table: fromTable, columns: fromColumns },
115
- to: { table: toTable, columns: toColumns },
116
- onDelete: fk.onDelete || DEFAULT_FOREIGN_KEY_ACTION,
117
- onUpdate: fk.onUpdate || DEFAULT_FOREIGN_KEY_ACTION,
118
- };
119
- ast.addRelationship(rel);
120
- }
121
- }
122
- }
123
- buildIndexes(ast, schema, table) {
124
- for (const idx of schema.indexes ?? []) {
125
- // An expression has no column to resolve. Dropping the entries that name a column this table
126
- // does not have, and the index if that leaves none, is what the entity side does too.
127
- const entries = idx.entries.filter((entry) => entry.expression || table.columns.has(entry.column));
128
- if (entries.length > 0) {
129
- ast.addIndex({ ...idx, table, entries });
130
- }
131
- }
132
- }
133
- }
@@ -1,2 +0,0 @@
1
- export { SchemaAST } from './schemaAST.js';
2
- export type { Drift, DriftReport } from './types.js';
@@ -1 +0,0 @@
1
- export { SchemaAST } from './schemaAST.js';
@@ -1,3 +0,0 @@
1
- import type { SqliteDatabase } from './sqliteQuerier.js';
2
- /** Runs {@link SQLITE_PRAGMAS} through a driver's own statements, whether it answers now or later. */
3
- export declare function applySqlitePragmas(db: Pick<SqliteDatabase, 'prepare'>): Promise<void>;
@@ -1,15 +0,0 @@
1
- /**
2
- * How every local SQLite connection opens. WAL, so a reader does not wait on the writer, and
3
- * `foreign_keys`, which SQLite ships off per connection for backward compatibility: without it the
4
- * constraints uql's own DDL declares are decorative - a declared `onDelete: 'CASCADE'` silently does
5
- * nothing and a dangling reference is accepted.
6
- */
7
- const SQLITE_PRAGMAS = ['journal_mode = WAL', 'foreign_keys = ON'];
8
- /** Runs {@link SQLITE_PRAGMAS} through a driver's own statements, whether it answers now or later. */
9
- export async function applySqlitePragmas(db) {
10
- for (const pragma of SQLITE_PRAGMAS) {
11
- const stmt = await db.prepare(`PRAGMA ${pragma}`);
12
- // `journal_mode` answers with a row and `foreign_keys` with none; `reader` picks the call for each.
13
- await (stmt.reader ? stmt.all() : stmt.run());
14
- }
15
- }