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.
- package/dist/dialect/abstractSqlDialect.d.ts +1 -7
- package/dist/dialect/abstractSqlDialect.js +1 -9
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -0
- package/dist/dialect/mysqlLikeSqlDialect.js +4 -1
- package/dist/dialect/pgLikeSqlDialect.js +0 -1
- package/dist/migrate/acquireQuerierForMigrations.d.ts +4 -8
- package/dist/migrate/acquireQuerierForMigrations.js +10 -18
- package/dist/migrate/builder/columnBuilder.d.ts +1 -2
- package/dist/migrate/builder/columnBuilder.js +3 -10
- package/dist/migrate/builder/migrationBuilder.d.ts +6 -26
- package/dist/migrate/builder/migrationBuilder.js +21 -78
- package/dist/migrate/builder/tableBuilder.js +2 -3
- package/dist/migrate/builder/types.d.ts +59 -120
- package/dist/migrate/cli-config.d.ts +1 -0
- package/dist/migrate/cli-config.js +11 -26
- package/dist/migrate/cli.d.ts +0 -13
- package/dist/migrate/cli.js +55 -118
- package/dist/migrate/codegen/entityCodeGenerator.d.ts +7 -61
- package/dist/migrate/codegen/entityCodeGenerator.js +53 -174
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -11
- package/dist/migrate/codegen/index.d.ts +2 -7
- package/dist/migrate/codegen/index.js +2 -8
- package/dist/migrate/codegen/migrationFile.d.ts +1 -8
- package/dist/migrate/codegen/migrationFile.js +9 -13
- package/dist/migrate/ddl/defaultSql.d.ts +2 -1
- package/dist/migrate/ddl/defaultSql.js +1 -3
- package/dist/migrate/ddl/indexCapabilities.d.ts +18 -0
- package/dist/migrate/ddl/indexCapabilities.js +60 -0
- package/dist/migrate/ddl/indexDdl.d.ts +11 -24
- package/dist/migrate/ddl/indexDdl.js +22 -28
- package/dist/migrate/ddl/mssqlIndexDdl.d.ts +1 -7
- package/dist/migrate/ddl/mssqlIndexDdl.js +1 -7
- package/dist/migrate/ddl/mssqlTableDdl.d.ts +6 -9
- package/dist/migrate/ddl/mssqlTableDdl.js +14 -17
- package/dist/migrate/ddl/mysqlIndexDdl.d.ts +0 -21
- package/dist/migrate/ddl/mysqlIndexDdl.js +4 -48
- package/dist/migrate/ddl/mysqlTableDdl.d.ts +6 -5
- package/dist/migrate/ddl/mysqlTableDdl.js +15 -10
- package/dist/migrate/ddl/pgIndexDdl.d.ts +0 -10
- package/dist/migrate/ddl/pgIndexDdl.js +5 -44
- package/dist/migrate/ddl/pgTableDdl.d.ts +6 -2
- package/dist/migrate/ddl/pgTableDdl.js +17 -1
- package/dist/migrate/ddl/sqliteIndexDdl.js +2 -7
- package/dist/migrate/ddl/tableDdl.d.ts +43 -24
- package/dist/migrate/ddl/tableDdl.js +56 -37
- package/dist/migrate/ddl/tableRebuild.d.ts +2 -1
- package/dist/migrate/drift/driftDetector.d.ts +29 -32
- package/dist/migrate/drift/driftDetector.js +155 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.d.ts +2 -2
- package/dist/migrate/generator/definitionToNode.js +6 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +56 -77
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +75 -98
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +10 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +52 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +21 -37
- package/dist/migrate/introspection/mysqlIntrospector.js +75 -56
- package/dist/migrate/introspection/postgresIntrospector.d.ts +15 -60
- package/dist/migrate/introspection/postgresIntrospector.js +74 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +19 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +73 -119
- package/dist/migrate/introspection/tableSchemaAST.d.ts +13 -0
- package/dist/migrate/introspection/tableSchemaAST.js +85 -0
- package/dist/migrate/migrationTarget.js +10 -2
- package/dist/migrate/migrator.d.ts +9 -44
- package/dist/migrate/migrator.js +72 -150
- package/dist/migrate/schemaChange.d.ts +8 -5
- package/dist/migrate/schemaChange.js +25 -8
- package/dist/migrate/schemaGenerator.d.ts +35 -109
- package/dist/migrate/schemaGenerator.js +179 -353
- package/dist/migrate/storage/databaseStorage.d.ts +2 -1
- package/dist/migrate/storage/databaseStorage.js +3 -2
- package/dist/migrate/storage/jsonStorage.d.ts +1 -2
- package/dist/migrate/storage/jsonStorage.js +11 -20
- package/dist/migrate/triggerSql.d.ts +7 -9
- package/dist/migrate/triggerSql.js +17 -18
- package/dist/mongo/index.d.ts +0 -1
- package/dist/mongo/index.js +0 -1
- package/dist/mongo/mongoDialect.js +0 -1
- package/dist/mongo/mongoIntrospector.d.ts +1 -6
- package/dist/mongo/mongoIntrospector.js +7 -18
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -26
- package/dist/mongo/mongoSchemaGenerator.js +51 -75
- package/dist/mssql/mssqlDialect.js +0 -1
- package/dist/schema/canonicalType.d.ts +15 -19
- package/dist/schema/canonicalType.js +20 -43
- package/dist/schema/dependencyGraph.d.ts +2 -3
- package/dist/schema/dependencyGraph.js +0 -4
- package/dist/schema/indexDifferences.js +2 -2
- package/dist/schema/schemaAST.d.ts +1 -1
- package/dist/schema/schemaAST.js +5 -12
- package/dist/schema/schemaASTBuilder.d.ts +5 -6
- package/dist/schema/schemaASTBuilder.js +17 -18
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +40 -73
- package/dist/schema/types.d.ts +24 -54
- package/dist/sqlite/localSqliteQuerierPool.d.ts +16 -2
- package/dist/sqlite/localSqliteQuerierPool.js +20 -4
- package/dist/sqlite/nodeSqliteQuerierPool.js +2 -2
- package/dist/sqlite/sqliteDialect.js +0 -1
- package/dist/sqlite/sqliteQuerierPool.js +3 -3
- package/dist/type/dialect.d.ts +0 -2
- package/dist/type/migration.d.ts +13 -73
- package/dist/util/ddlExpression.util.d.ts +13 -2
- package/dist/util/ddlExpression.util.js +27 -4
- package/dist/util/sql.util.d.ts +2 -12
- package/dist/util/sql.util.js +9 -18
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +1 -1
- package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -36
- package/dist/migrate/introspection/baseSqlIntrospector.js +0 -133
- package/dist/schema/index.d.ts +0 -2
- package/dist/schema/index.js +0 -1
- package/dist/sqlite/sqlitePragmas.d.ts +0 -3
- package/dist/sqlite/sqlitePragmas.js +0 -15
|
@@ -737,12 +737,7 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
737
737
|
private vectorNearCondition;
|
|
738
738
|
/** ANSI-style single-quote escaping. */
|
|
739
739
|
escape(value: unknown): string;
|
|
740
|
-
/**
|
|
741
|
-
* The `$regex` predicate. An infix operator on the MySQL family (`REGEXP`) and the Postgres one
|
|
742
|
-
* (`~`), but a function on Oracle and SQL Server 2025 (`REGEXP_LIKE(col, ?)`) - which is why this
|
|
743
|
-
* is a method rather than the operator token alone. An engine with no regex at all overrides it to
|
|
744
|
-
* throw, the way {@link appendTextSearch} already does.
|
|
745
|
-
*/
|
|
740
|
+
/** The `$regex` predicate, case-sensitive on every engine. */
|
|
746
741
|
protected regexCondition(operand: string, placeholder: string): string;
|
|
747
742
|
/**
|
|
748
743
|
* Two fragments compared null-safely: true where they differ, and where one side alone is NULL. What a
|
|
@@ -754,5 +749,4 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
754
749
|
protected formatIn(_ctx: QueryContext, operand: string, values: unknown[], negate: boolean, bind: (value: unknown) => string): string;
|
|
755
750
|
/** Reads extracted JSON text as a number, which every engine spells its own way. */
|
|
756
751
|
protected abstract numericCast(expr: string): string;
|
|
757
|
-
toString(): string;
|
|
758
752
|
}
|
|
@@ -2081,12 +2081,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
2081
2081
|
escape(value) {
|
|
2082
2082
|
return escapeAnsiSqlLiteral(value);
|
|
2083
2083
|
}
|
|
2084
|
-
/**
|
|
2085
|
-
* The `$regex` predicate. An infix operator on the MySQL family (`REGEXP`) and the Postgres one
|
|
2086
|
-
* (`~`), but a function on Oracle and SQL Server 2025 (`REGEXP_LIKE(col, ?)`) - which is why this
|
|
2087
|
-
* is a method rather than the operator token alone. An engine with no regex at all overrides it to
|
|
2088
|
-
* throw, the way {@link appendTextSearch} already does.
|
|
2089
|
-
*/
|
|
2084
|
+
/** The `$regex` predicate, case-sensitive on every engine. */
|
|
2090
2085
|
regexCondition(operand, placeholder) {
|
|
2091
2086
|
return `${operand} REGEXP ${placeholder}`;
|
|
2092
2087
|
}
|
|
@@ -2098,7 +2093,4 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
2098
2093
|
const phs = values.map(bind).join(', ');
|
|
2099
2094
|
return `${operand} ${negate ? 'NOT IN' : 'IN'} (${phs})`;
|
|
2100
2095
|
}
|
|
2101
|
-
toString() {
|
|
2102
|
-
return this.dialectName;
|
|
2103
|
-
}
|
|
2104
2096
|
}
|
|
@@ -19,6 +19,8 @@ export declare abstract class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
19
19
|
*/
|
|
20
20
|
estimatedCount<E>(ctx: QueryContext, entity: Type<E>): void;
|
|
21
21
|
protected readonly unboundedLimit: string;
|
|
22
|
+
/** Case-sensitive, as on every other engine, where the column's collation would fold it. */
|
|
23
|
+
protected regexCondition(operand: string, placeholder: string): string;
|
|
22
24
|
/** A signed key, so a foreign key taking its type from it matches, as MySQL refuses an `UNSIGNED` mismatch. */
|
|
23
25
|
readonly autoIncrementSuffix = "AUTO_INCREMENT";
|
|
24
26
|
readonly escapeIdChar = "`";
|
|
@@ -19,7 +19,6 @@ export const MYSQL_FEATURES = {
|
|
|
19
19
|
schemas: true,
|
|
20
20
|
dropTableCascade: false,
|
|
21
21
|
rebuildsTables: false,
|
|
22
|
-
commentSyntax: 'inline',
|
|
23
22
|
vectorIndexRequiresNotNull: false,
|
|
24
23
|
vectorSupportsLength: false,
|
|
25
24
|
vectorBytes: false,
|
|
@@ -81,6 +80,10 @@ export class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
81
80
|
ctx.addValue(this.resolveTableAlias(meta));
|
|
82
81
|
}
|
|
83
82
|
unboundedLimit = String(MAX_LIMIT);
|
|
83
|
+
/** Case-sensitive, as on every other engine, where the column's collation would fold it. */
|
|
84
|
+
regexCondition(operand, placeholder) {
|
|
85
|
+
return `${operand} REGEXP CONCAT('(?-i)', ${placeholder})`;
|
|
86
|
+
}
|
|
84
87
|
/** A signed key, so a foreign key taking its type from it matches, as MySQL refuses an `UNSIGNED` mismatch. */
|
|
85
88
|
autoIncrementSuffix = 'AUTO_INCREMENT';
|
|
86
89
|
escapeIdChar = '`';
|
|
@@ -6,14 +6,10 @@ import { type Querier, type QuerierPool, type SqlQuerier } from '../type/index.j
|
|
|
6
6
|
* app traffic (e.g. LibSQL embedded replica: local `file:` + remote `syncUrl`).
|
|
7
7
|
*/
|
|
8
8
|
export declare function acquireQuerierForMigrations(pool: QuerierPool): Promise<Querier>;
|
|
9
|
+
/** Runs `task` on a migration querier over SQL, and releases it. `requiredBy` names the caller in the error. */
|
|
10
|
+
export declare function withSqlQuerierForMigrations<T>(pool: QuerierPool, requiredBy: string, task: (querier: SqlQuerier) => Promise<T>): Promise<T>;
|
|
9
11
|
/**
|
|
10
|
-
* Runs `task` on a migration querier and releases it
|
|
11
|
-
*
|
|
12
|
-
* `pool.withQuerier` cannot serve here because migrations may run on a different connection than app
|
|
13
|
-
* traffic, but the ownership rule is the same one: whoever acquires, releases.
|
|
12
|
+
* Runs `task` on a migration querier `isKind` accepts, refusing any other with `error`, and releases it whatever
|
|
13
|
+
* happens. Not `pool.withQuerier`: migrations may run on another connection than app traffic.
|
|
14
14
|
*/
|
|
15
|
-
export declare function withQuerierForMigrations<T>(pool: QuerierPool, task: (querier: Querier) => Promise<T>): Promise<T>;
|
|
16
|
-
/** Same, for the paths that only work against SQL. `requiredBy` names the caller in the error. */
|
|
17
|
-
export declare function withSqlQuerierForMigrations<T>(pool: QuerierPool, requiredBy: string, task: (querier: SqlQuerier) => Promise<T>): Promise<T>;
|
|
18
|
-
/** Runs `task` on a migration querier `isKind` accepts, refusing any other with `error`. */
|
|
19
15
|
export declare function withQuerierOfKind<Q extends Querier, T>(pool: QuerierPool, isKind: (querier: Querier) => querier is Q, error: string, task: (querier: Q) => Promise<T>): Promise<T>;
|
|
@@ -9,31 +9,23 @@ import { UqlUsageError } from '../util/uqlError.js';
|
|
|
9
9
|
export async function acquireQuerierForMigrations(pool) {
|
|
10
10
|
return (await pool.getMigrationQuerier?.()) ?? (await pool.getQuerier());
|
|
11
11
|
}
|
|
12
|
+
/** Runs `task` on a migration querier over SQL, and releases it. `requiredBy` names the caller in the error. */
|
|
13
|
+
export function withSqlQuerierForMigrations(pool, requiredBy, task) {
|
|
14
|
+
return withQuerierOfKind(pool, isSqlQuerier, `${requiredBy} requires a SQL-based querier`, task);
|
|
15
|
+
}
|
|
12
16
|
/**
|
|
13
|
-
* Runs `task` on a migration querier and releases it
|
|
14
|
-
*
|
|
15
|
-
* `pool.withQuerier` cannot serve here because migrations may run on a different connection than app
|
|
16
|
-
* traffic, but the ownership rule is the same one: whoever acquires, releases.
|
|
17
|
+
* Runs `task` on a migration querier `isKind` accepts, refusing any other with `error`, and releases it whatever
|
|
18
|
+
* happens. Not `pool.withQuerier`: migrations may run on another connection than app traffic.
|
|
17
19
|
*/
|
|
18
|
-
export async function
|
|
20
|
+
export async function withQuerierOfKind(pool, isKind, error, task) {
|
|
19
21
|
const querier = await acquireQuerierForMigrations(pool);
|
|
20
22
|
try {
|
|
23
|
+
if (!isKind(querier)) {
|
|
24
|
+
throw new UqlUsageError(error);
|
|
25
|
+
}
|
|
21
26
|
return await task(querier);
|
|
22
27
|
}
|
|
23
28
|
finally {
|
|
24
29
|
await querier.release();
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
|
-
/** Same, for the paths that only work against SQL. `requiredBy` names the caller in the error. */
|
|
28
|
-
export function withSqlQuerierForMigrations(pool, requiredBy, task) {
|
|
29
|
-
return withQuerierOfKind(pool, isSqlQuerier, `${requiredBy} requires a SQL-based querier`, task);
|
|
30
|
-
}
|
|
31
|
-
/** Runs `task` on a migration querier `isKind` accepts, refusing any other with `error`. */
|
|
32
|
-
export function withQuerierOfKind(pool, isKind, error, task) {
|
|
33
|
-
return withQuerierForMigrations(pool, (querier) => {
|
|
34
|
-
if (!isKind(querier)) {
|
|
35
|
-
throw new UqlUsageError(error);
|
|
36
|
-
}
|
|
37
|
-
return task(querier);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Fluent API for defining columns in migrations.
|
|
5
5
|
*/
|
|
6
|
-
import type { EnumValues } from '../../schema/types.js';
|
|
7
|
-
import { type CanonicalType, type ForeignKeyAction } from '../../schema/types.js';
|
|
6
|
+
import type { CanonicalType, EnumValues, ForeignKeyAction } from '../../schema/types.js';
|
|
8
7
|
import type { BaseColumnOptions, FullColumnDefinition, IColumnBuilder, IForeignKeyBuilder } from './types.js';
|
|
9
8
|
/**
|
|
10
9
|
* Builder for column definitions with a fluent API. Each call replaces the definition it holds, and `build`
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Fluent API for defining columns in migrations.
|
|
5
5
|
*/
|
|
6
|
-
import { DEFAULT_FOREIGN_KEY_ACTION } from '../../schema/types.js';
|
|
7
6
|
/**
|
|
8
7
|
* Builder for column definitions with a fluent API. Each call replaces the definition it holds, and `build`
|
|
9
8
|
* returns it. Columns are NOT NULL by default (safer).
|
|
@@ -26,8 +25,8 @@ export class ColumnBuilder {
|
|
|
26
25
|
index: options.index,
|
|
27
26
|
foreignKey: references && {
|
|
28
27
|
references: { table: references.table, columns: [references.column ?? 'id'] },
|
|
29
|
-
onDelete: references.onDelete
|
|
30
|
-
onUpdate: references.onUpdate
|
|
28
|
+
onDelete: references.onDelete,
|
|
29
|
+
onUpdate: references.onUpdate,
|
|
31
30
|
},
|
|
32
31
|
};
|
|
33
32
|
}
|
|
@@ -110,13 +109,7 @@ export class ColumnBuilder {
|
|
|
110
109
|
* Returns a ForeignKeyBuilder for additional options.
|
|
111
110
|
*/
|
|
112
111
|
references(table, column = 'id') {
|
|
113
|
-
return this.set({
|
|
114
|
-
foreignKey: {
|
|
115
|
-
references: { table, columns: [column] },
|
|
116
|
-
onDelete: DEFAULT_FOREIGN_KEY_ACTION,
|
|
117
|
-
onUpdate: DEFAULT_FOREIGN_KEY_ACTION,
|
|
118
|
-
},
|
|
119
|
-
});
|
|
112
|
+
return this.set({ foreignKey: { references: { table, columns: [column] } } });
|
|
120
113
|
}
|
|
121
114
|
/**
|
|
122
115
|
* Set ON DELETE action for foreign key.
|
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import type { IndexColumnInput, IndexOptions } from '../../type/index.js';
|
|
2
|
-
import type {
|
|
3
|
-
import type { AnyMigrationOperation, IAlterTableBuilder, IColumnBuilder, IColumnFactory, IMigrationBuilder, ITableBuilder } from './types.js';
|
|
4
|
-
type ForeignKeyTarget = ForeignKeySchema['references'];
|
|
5
|
-
type ForeignKeyOptions = Pick<ForeignKeySchema, 'name' | 'onDelete' | 'onUpdate'>;
|
|
2
|
+
import type { AnyMigrationOperation, ForeignKeyOptions, ForeignKeyTarget, IAlterTableBuilder, IColumnBuilder, IColumnFactory, IMigrationBuilder, ITableBuilder } from './types.js';
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* The type-safe migration builder: each change is declared once, as an operation handed to `apply`.
|
|
5
|
+
* `migrationBuilderFor` builds one running the statements its querier's generator writes for each.
|
|
9
6
|
*/
|
|
10
|
-
export declare class
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* Where every operation this class builds lands, and the one thing {@link MigrationBuilder}
|
|
14
|
-
* overrides: it records and then runs. Each operation is spelled once, here, rather than once per
|
|
15
|
-
* class, which is what let the two drift into recording different shapes of the same change.
|
|
16
|
-
*/
|
|
17
|
-
protected record(operation: AnyMigrationOperation): Promise<void>;
|
|
7
|
+
export declare class MigrationBuilder implements IMigrationBuilder {
|
|
8
|
+
private readonly apply;
|
|
9
|
+
constructor(apply: (operation: AnyMigrationOperation) => Promise<void>);
|
|
18
10
|
createTable(name: string, callback: (table: ITableBuilder) => void): Promise<void>;
|
|
19
11
|
dropTable(name: string, options?: {
|
|
20
12
|
ifExists?: boolean;
|
|
@@ -31,16 +23,4 @@ export declare class OperationRecorder implements IMigrationBuilder {
|
|
|
31
23
|
addForeignKey(tableName: string, columns: string[], target: ForeignKeyTarget, options?: ForeignKeyOptions): Promise<void>;
|
|
32
24
|
dropForeignKey(tableName: string, constraintName: string): Promise<void>;
|
|
33
25
|
raw(sql: string): Promise<void>;
|
|
34
|
-
getOperations(): AnyMigrationOperation[];
|
|
35
26
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Records each operation, then runs the statements `generator` writes for it through `run`. Build one
|
|
38
|
-
* for a querier with `migrationBuilderFor`.
|
|
39
|
-
*/
|
|
40
|
-
export declare class MigrationBuilder extends OperationRecorder {
|
|
41
|
-
private readonly generator;
|
|
42
|
-
private readonly run;
|
|
43
|
-
constructor(generator: SchemaGenerator, run: (statement: string) => Promise<unknown>);
|
|
44
|
-
protected record(operation: AnyMigrationOperation): Promise<void>;
|
|
45
|
-
}
|
|
46
|
-
export {};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_FOREIGN_KEY_ACTION } from '../../schema/types.js';
|
|
2
1
|
import { UqlUsageError } from '../../util/uqlError.js';
|
|
3
2
|
import { indexDefinition } from '../generator/definitionToNode.js';
|
|
4
3
|
import { TableBuilder } from './tableBuilder.js';
|
|
@@ -44,12 +43,7 @@ class AlterTableBuilder {
|
|
|
44
43
|
if (column.index || column.foreignKey) {
|
|
45
44
|
throw new UqlUsageError(`alterColumn changes '${column.name}' alone: add its index with createIndex, its foreign key with addForeignKey`);
|
|
46
45
|
}
|
|
47
|
-
this.operations.push({
|
|
48
|
-
type: 'alterColumn',
|
|
49
|
-
tableName: this.tableName,
|
|
50
|
-
columnName: column.name,
|
|
51
|
-
changes: column,
|
|
52
|
-
});
|
|
46
|
+
this.operations.push({ type: 'alterColumn', tableName: this.tableName, changes: column });
|
|
53
47
|
return this;
|
|
54
48
|
}
|
|
55
49
|
addIndex(columns, options) {
|
|
@@ -72,13 +66,7 @@ class AlterTableBuilder {
|
|
|
72
66
|
this.operations.push({
|
|
73
67
|
type: 'addForeignKey',
|
|
74
68
|
tableName: this.tableName,
|
|
75
|
-
foreignKey: {
|
|
76
|
-
name: options.name,
|
|
77
|
-
columns,
|
|
78
|
-
references: { table: target.table, columns: target.columns },
|
|
79
|
-
onDelete: options.onDelete ?? DEFAULT_FOREIGN_KEY_ACTION,
|
|
80
|
-
onUpdate: options.onUpdate ?? DEFAULT_FOREIGN_KEY_ACTION,
|
|
81
|
-
},
|
|
69
|
+
foreignKey: { ...options, columns, references: target },
|
|
82
70
|
});
|
|
83
71
|
return this;
|
|
84
72
|
}
|
|
@@ -91,51 +79,31 @@ class AlterTableBuilder {
|
|
|
91
79
|
return this;
|
|
92
80
|
}
|
|
93
81
|
}
|
|
94
|
-
function collectAlterOperations(tableName, callback) {
|
|
95
|
-
const builder = new AlterTableBuilder(tableName);
|
|
96
|
-
callback(builder);
|
|
97
|
-
return builder.operations;
|
|
98
|
-
}
|
|
99
82
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
83
|
+
* The type-safe migration builder: each change is declared once, as an operation handed to `apply`.
|
|
84
|
+
* `migrationBuilderFor` builds one running the statements its querier's generator writes for each.
|
|
102
85
|
*/
|
|
103
|
-
export class
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
async record(operation) {
|
|
111
|
-
this.operations.push(operation);
|
|
112
|
-
}
|
|
113
|
-
async createTable(name, callback) {
|
|
86
|
+
export class MigrationBuilder {
|
|
87
|
+
apply;
|
|
88
|
+
constructor(apply) {
|
|
89
|
+
this.apply = apply;
|
|
90
|
+
}
|
|
91
|
+
createTable(name, callback) {
|
|
114
92
|
const builder = new TableBuilder(name);
|
|
115
93
|
callback(builder);
|
|
116
|
-
|
|
117
|
-
type: 'createTable',
|
|
118
|
-
table: builder.build(),
|
|
119
|
-
});
|
|
94
|
+
return this.apply({ type: 'createTable', table: builder.build() });
|
|
120
95
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
type: 'dropTable',
|
|
124
|
-
tableName: name,
|
|
125
|
-
ifExists: options.ifExists,
|
|
126
|
-
cascade: options.cascade,
|
|
127
|
-
});
|
|
96
|
+
dropTable(name, options = {}) {
|
|
97
|
+
return this.apply({ type: 'dropTable', tableName: name, ifExists: options.ifExists, cascade: options.cascade });
|
|
128
98
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
type: 'renameTable',
|
|
132
|
-
oldName,
|
|
133
|
-
newName,
|
|
134
|
-
});
|
|
99
|
+
renameTable(oldName, newName) {
|
|
100
|
+
return this.apply({ type: 'renameTable', oldName, newName });
|
|
135
101
|
}
|
|
136
102
|
async alterTable(name, callback) {
|
|
137
|
-
|
|
138
|
-
|
|
103
|
+
const table = new AlterTableBuilder(name);
|
|
104
|
+
callback(table);
|
|
105
|
+
for (const operation of table.operations) {
|
|
106
|
+
await this.apply(operation);
|
|
139
107
|
}
|
|
140
108
|
}
|
|
141
109
|
// Each single change delegates to `alterTable`, so each operation is implemented only once.
|
|
@@ -163,32 +131,7 @@ export class OperationRecorder {
|
|
|
163
131
|
dropForeignKey(tableName, constraintName) {
|
|
164
132
|
return this.alterTable(tableName, (table) => table.dropForeignKey(constraintName));
|
|
165
133
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
type: 'raw',
|
|
169
|
-
sql,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
getOperations() {
|
|
173
|
-
return [...this.operations];
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Records each operation, then runs the statements `generator` writes for it through `run`. Build one
|
|
178
|
-
* for a querier with `migrationBuilderFor`.
|
|
179
|
-
*/
|
|
180
|
-
export class MigrationBuilder extends OperationRecorder {
|
|
181
|
-
generator;
|
|
182
|
-
run;
|
|
183
|
-
constructor(generator, run) {
|
|
184
|
-
super();
|
|
185
|
-
this.generator = generator;
|
|
186
|
-
this.run = run;
|
|
187
|
-
}
|
|
188
|
-
async record(operation) {
|
|
189
|
-
await super.record(operation);
|
|
190
|
-
for (const statement of this.generator.generateOperation(operation)) {
|
|
191
|
-
await this.run(statement);
|
|
192
|
-
}
|
|
134
|
+
raw(sql) {
|
|
135
|
+
return this.apply({ type: 'raw', sql });
|
|
193
136
|
}
|
|
194
137
|
}
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Fluent API for defining tables in migrations.
|
|
5
5
|
*/
|
|
6
|
-
import { DEFAULT_FOREIGN_KEY_ACTION } from '../../schema/types.js';
|
|
7
6
|
import { DATE_PRECISION } from '../../util/date.js';
|
|
8
7
|
import { currentTimestamp } from '../../util/raw.js';
|
|
9
8
|
import { columnForeignKey, columnIndex, indexDefinition } from '../generator/definitionToNode.js';
|
|
@@ -19,8 +18,8 @@ class TableForeignKeyBuilder {
|
|
|
19
18
|
_columns;
|
|
20
19
|
_referencedTable;
|
|
21
20
|
_referencedColumns = [];
|
|
22
|
-
_onDelete
|
|
23
|
-
_onUpdate
|
|
21
|
+
_onDelete;
|
|
22
|
+
_onUpdate;
|
|
24
23
|
_name;
|
|
25
24
|
constructor(_columns) {
|
|
26
25
|
this._columns = _columns;
|
|
@@ -68,7 +68,7 @@ export interface VectorColumnOptions extends BaseColumnOptions {
|
|
|
68
68
|
dimensions?: number;
|
|
69
69
|
}
|
|
70
70
|
/** A column as the builder describes one: a {@link ColumnNode} without its graph links. */
|
|
71
|
-
export type ColumnDefinition = Except<ColumnNode, 'table'
|
|
71
|
+
export type ColumnDefinition = Except<ColumnNode, 'table'>;
|
|
72
72
|
/**
|
|
73
73
|
* The foreign key a single column declares: {@link ForeignKeySchema} without the local columns, which
|
|
74
74
|
* are the column itself. Derived for the reason {@link ColumnDefinition} is - restated, the two spelled
|
|
@@ -79,6 +79,8 @@ export type ForeignKeyDefinition = Except<ForeignKeySchema, 'columns'>;
|
|
|
79
79
|
* Full column definition including foreign key.
|
|
80
80
|
*/
|
|
81
81
|
export interface FullColumnDefinition extends ColumnDefinition {
|
|
82
|
+
/** The values the column accepts, lifted onto the table as its check. */
|
|
83
|
+
enum?: EnumValues;
|
|
82
84
|
/** Foreign key reference (if any) */
|
|
83
85
|
foreignKey?: ForeignKeyDefinition;
|
|
84
86
|
/** Index name (if indexed) */
|
|
@@ -109,110 +111,60 @@ export interface TableDefinition {
|
|
|
109
111
|
/** Table comment */
|
|
110
112
|
comment?: string;
|
|
111
113
|
}
|
|
112
|
-
/**
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
|
|
164
|
-
*/
|
|
165
|
-
export
|
|
166
|
-
type: 'renameColumn';
|
|
167
|
-
tableName: string;
|
|
168
|
-
oldName: string;
|
|
169
|
-
newName: string;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Create index operation.
|
|
173
|
-
*/
|
|
174
|
-
export interface CreateIndexOperation {
|
|
175
|
-
type: 'createIndex';
|
|
176
|
-
tableName: string;
|
|
177
|
-
index: IndexDefinition;
|
|
178
|
-
ifNotExists?: boolean;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Drop index operation.
|
|
182
|
-
*/
|
|
183
|
-
export interface DropIndexOperation {
|
|
184
|
-
type: 'dropIndex';
|
|
185
|
-
tableName: string;
|
|
186
|
-
indexName: string;
|
|
187
|
-
ifExists?: boolean;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Add foreign key operation.
|
|
191
|
-
*/
|
|
192
|
-
export interface AddForeignKeyOperation {
|
|
193
|
-
type: 'addForeignKey';
|
|
194
|
-
tableName: string;
|
|
195
|
-
foreignKey: ForeignKeySchema;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Drop foreign key operation.
|
|
199
|
-
*/
|
|
200
|
-
export interface DropForeignKeyOperation {
|
|
201
|
-
type: 'dropForeignKey';
|
|
202
|
-
tableName: string;
|
|
203
|
-
constraintName: string;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Raw SQL operation (escape hatch).
|
|
207
|
-
*/
|
|
208
|
-
export interface RawSqlOperation {
|
|
209
|
-
type: 'raw';
|
|
210
|
-
sql: string;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Union of all operation types.
|
|
214
|
-
*/
|
|
215
|
-
export type AnyMigrationOperation = CreateTableOperation | DropTableOperation | RenameTableOperation | AddColumnOperation | DropColumnOperation | AlterColumnOperation | RenameColumnOperation | CreateIndexOperation | DropIndexOperation | AddForeignKeyOperation | DropForeignKeyOperation | RawSqlOperation;
|
|
114
|
+
/** One change the migration builder declares, which a generator turns into statements. */
|
|
115
|
+
export type AnyMigrationOperation = {
|
|
116
|
+
readonly type: 'createTable';
|
|
117
|
+
readonly table: TableDefinition;
|
|
118
|
+
} | {
|
|
119
|
+
readonly type: 'dropTable';
|
|
120
|
+
readonly tableName: string;
|
|
121
|
+
readonly ifExists?: boolean;
|
|
122
|
+
readonly cascade?: boolean;
|
|
123
|
+
} | {
|
|
124
|
+
readonly type: 'renameTable';
|
|
125
|
+
readonly oldName: string;
|
|
126
|
+
readonly newName: string;
|
|
127
|
+
} | {
|
|
128
|
+
readonly type: 'addColumn';
|
|
129
|
+
readonly tableName: string;
|
|
130
|
+
readonly column: FullColumnDefinition;
|
|
131
|
+
} | {
|
|
132
|
+
readonly type: 'dropColumn';
|
|
133
|
+
readonly tableName: string;
|
|
134
|
+
readonly columnName: string;
|
|
135
|
+
} | {
|
|
136
|
+
readonly type: 'alterColumn';
|
|
137
|
+
readonly tableName: string;
|
|
138
|
+
readonly changes: FullColumnDefinition;
|
|
139
|
+
} | {
|
|
140
|
+
readonly type: 'renameColumn';
|
|
141
|
+
readonly tableName: string;
|
|
142
|
+
readonly oldName: string;
|
|
143
|
+
readonly newName: string;
|
|
144
|
+
} | {
|
|
145
|
+
readonly type: 'createIndex';
|
|
146
|
+
readonly tableName: string;
|
|
147
|
+
readonly index: IndexDefinition;
|
|
148
|
+
} | {
|
|
149
|
+
readonly type: 'dropIndex';
|
|
150
|
+
readonly tableName: string;
|
|
151
|
+
readonly indexName: string;
|
|
152
|
+
} | {
|
|
153
|
+
readonly type: 'addForeignKey';
|
|
154
|
+
readonly tableName: string;
|
|
155
|
+
readonly foreignKey: ForeignKeySchema;
|
|
156
|
+
} | {
|
|
157
|
+
readonly type: 'dropForeignKey';
|
|
158
|
+
readonly tableName: string;
|
|
159
|
+
readonly constraintName: string;
|
|
160
|
+
} | {
|
|
161
|
+
readonly type: 'raw';
|
|
162
|
+
readonly sql: string;
|
|
163
|
+
};
|
|
164
|
+
/** The table and columns a foreign key points at. */
|
|
165
|
+
export type ForeignKeyTarget = ForeignKeySchema['references'];
|
|
166
|
+
/** A foreign key's constraint name and actions, each the engine's default where unstated. */
|
|
167
|
+
export type ForeignKeyOptions = Pick<ForeignKeySchema, 'name' | 'onDelete' | 'onUpdate'>;
|
|
216
168
|
/**
|
|
217
169
|
* Interface for column builder (fluent API).
|
|
218
170
|
*/
|
|
@@ -349,14 +301,7 @@ export interface IAlterTableBuilder {
|
|
|
349
301
|
/** Drop an index from the table */
|
|
350
302
|
dropIndex(name: string): this;
|
|
351
303
|
/** Add a foreign key to the table */
|
|
352
|
-
addForeignKey(columns: string[], target:
|
|
353
|
-
table: string;
|
|
354
|
-
columns: string[];
|
|
355
|
-
}, options?: {
|
|
356
|
-
name?: string;
|
|
357
|
-
onDelete?: ForeignKeyAction;
|
|
358
|
-
onUpdate?: ForeignKeyAction;
|
|
359
|
-
}): this;
|
|
304
|
+
addForeignKey(columns: string[], target: ForeignKeyTarget, options?: ForeignKeyOptions): this;
|
|
360
305
|
/** Drop a foreign key from the table */
|
|
361
306
|
dropForeignKey(name: string): this;
|
|
362
307
|
}
|
|
@@ -388,13 +333,7 @@ export interface IMigrationBuilder {
|
|
|
388
333
|
/** Drop an index */
|
|
389
334
|
dropIndex(tableName: string, indexName: string): Promise<void>;
|
|
390
335
|
/** Add a foreign key */
|
|
391
|
-
addForeignKey(tableName: string, columns: string[], target:
|
|
392
|
-
table: string;
|
|
393
|
-
columns: string[];
|
|
394
|
-
}, options?: {
|
|
395
|
-
onDelete?: ForeignKeyAction;
|
|
396
|
-
onUpdate?: ForeignKeyAction;
|
|
397
|
-
}): Promise<void>;
|
|
336
|
+
addForeignKey(tableName: string, columns: string[], target: ForeignKeyTarget, options?: ForeignKeyOptions): Promise<void>;
|
|
398
337
|
/** Drop a foreign key */
|
|
399
338
|
dropForeignKey(tableName: string, constraintName: string): Promise<void>;
|
|
400
339
|
/** Execute raw SQL, the escape hatch for anything the builder does not model. */
|
|
@@ -8,4 +8,5 @@ export declare function tsxApiFor(path: string, versions: {
|
|
|
8
8
|
bun?: string;
|
|
9
9
|
deno?: string;
|
|
10
10
|
}): string | undefined;
|
|
11
|
+
/** The config at `customPath`, or the first of {@link CONFIG_FILES} in the working directory; the CLI validates it. */
|
|
11
12
|
export declare function loadConfig(customPath?: string): Promise<Config>;
|
|
@@ -33,33 +33,18 @@ async function importConfig(path) {
|
|
|
33
33
|
});
|
|
34
34
|
return mod.default ?? mod;
|
|
35
35
|
}
|
|
36
|
+
/** Where the CLI looks for its config, in order, when it is given no path. */
|
|
37
|
+
const CONFIG_FILES = ['uql.config.ts', 'uql.config.js', 'uql.config.mjs', '.uqlrc.ts', '.uqlrc.js'];
|
|
38
|
+
/** The config at `customPath`, or the first of {@link CONFIG_FILES} in the working directory; the CLI validates it. */
|
|
36
39
|
export async function loadConfig(customPath) {
|
|
37
|
-
|
|
38
|
-
const fullPath = resolve(process.cwd(),
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (!exists) {
|
|
43
|
-
throw new UqlUsageError(`Could not find uql configuration file at ${customPath}`);
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
const config = await importConfig(fullPath);
|
|
47
|
-
return config;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
throw new UqlUsageError(`Could not load configuration file at ${customPath}: ${error.message}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const configPaths = ['uql.config.ts', 'uql.config.js', 'uql.config.mjs', '.uqlrc.ts', '.uqlrc.js'];
|
|
54
|
-
for (const configPath of configPaths) {
|
|
55
|
-
const fullPath = resolve(process.cwd(), configPath);
|
|
56
|
-
const exists = await stat(fullPath)
|
|
57
|
-
.then(() => true)
|
|
58
|
-
.catch(() => false);
|
|
59
|
-
if (exists) {
|
|
60
|
-
const config = await importConfig(fullPath);
|
|
61
|
-
return config;
|
|
40
|
+
for (const candidate of customPath ? [customPath] : CONFIG_FILES) {
|
|
41
|
+
const fullPath = resolve(process.cwd(), candidate);
|
|
42
|
+
const found = await stat(fullPath).then(() => true, () => false);
|
|
43
|
+
if (found) {
|
|
44
|
+
return (await importConfig(fullPath));
|
|
62
45
|
}
|
|
63
46
|
}
|
|
64
|
-
throw new UqlUsageError(
|
|
47
|
+
throw new UqlUsageError(customPath
|
|
48
|
+
? `Could not find uql configuration file at ${customPath}`
|
|
49
|
+
: 'Could not find uql configuration file. Create a uql.config.ts or uql.config.js file in your project root.');
|
|
65
50
|
}
|