uql-orm 0.97.0 → 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 +57 -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 +51 -168
- package/dist/migrate/codegen/fieldOptionsSource.js +3 -6
- 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 +139 -205
- package/dist/migrate/drift/index.d.ts +1 -6
- package/dist/migrate/drift/index.js +1 -6
- package/dist/migrate/generator/definitionToNode.js +3 -3
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +53 -76
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +68 -90
- package/dist/migrate/introspection/mssqlIntrospector.d.ts +8 -43
- package/dist/migrate/introspection/mssqlIntrospector.js +44 -77
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -38
- package/dist/migrate/introspection/mysqlIntrospector.js +48 -57
- package/dist/migrate/introspection/postgresIntrospector.d.ts +12 -60
- package/dist/migrate/introspection/postgresIntrospector.js +62 -77
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +14 -53
- package/dist/migrate/introspection/sqliteIntrospector.js +59 -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 +4 -21
- package/dist/migrate/migrator.js +47 -71
- package/dist/migrate/schemaChange.d.ts +0 -2
- package/dist/migrate/schemaChange.js +1 -1
- package/dist/migrate/schemaGenerator.d.ts +22 -97
- package/dist/migrate/schemaGenerator.js +113 -298
- 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 +3 -3
- package/dist/migrate/triggerSql.js +5 -4
- 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 +0 -3
- package/dist/mongo/mongoIntrospector.js +7 -14
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -20
- package/dist/mongo/mongoSchemaGenerator.js +51 -70
- 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 +4 -12
- package/dist/schema/schemaASTBuilder.d.ts +2 -5
- package/dist/schema/schemaASTBuilder.js +11 -14
- package/dist/schema/schemaASTDiffer.d.ts +4 -5
- package/dist/schema/schemaASTDiffer.js +22 -70
- package/dist/schema/types.d.ts +1 -48
- 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 +3 -54
- package/dist/util/ddlExpression.util.d.ts +2 -2
- package/dist/util/ddlExpression.util.js +3 -3
- package/dist/util/sql.util.d.ts +0 -8
- package/dist/util/sql.util.js +7 -12
- package/package.json +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
|
@@ -17,6 +17,10 @@ export type LocalSqlitePoolOptions = {
|
|
|
17
17
|
export type LocalSqliteDatabase = {
|
|
18
18
|
prepare(sql: string): SqlitePreparedStatement;
|
|
19
19
|
loadExtension(path: string): void;
|
|
20
|
+
/** Defines a SQL function, which `bun:sqlite` cannot. */
|
|
21
|
+
function?(name: string, options: {
|
|
22
|
+
deterministic: boolean;
|
|
23
|
+
}, fn: (...args: SqlValue[]) => SqlValue): unknown;
|
|
20
24
|
close(): unknown;
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
@@ -26,12 +30,22 @@ export type LocalSqliteDatabase = {
|
|
|
26
30
|
export declare function adaptSqlite<S extends Omit<SqlitePreparedStatement, 'reader'>>(db: Omit<LocalSqliteDatabase, 'prepare'> & {
|
|
27
31
|
prepare(sql: string): S;
|
|
28
32
|
}, reads: (stmt: S) => boolean): LocalSqliteDatabase;
|
|
29
|
-
/**
|
|
30
|
-
|
|
33
|
+
/**
|
|
34
|
+
* `db` extended past SQLite's own: each loadable extension installed, which `node:sqlite` refuses unless
|
|
35
|
+
* opened to allow them, and `REGEXP` defined as libSQL builds it in, which SQLite leaves to the application.
|
|
36
|
+
*/
|
|
37
|
+
export declare function extendSqlite(db: LocalSqliteDatabase, extensions?: readonly string[]): LocalSqliteDatabase;
|
|
38
|
+
/** A value SQLite hands a function, and takes back. */
|
|
39
|
+
type SqlValue = string | number | bigint | Uint8Array | null;
|
|
31
40
|
/** A pool for a database file opened in this process, configured the same way whichever driver's {@link createDb} opens it. */
|
|
32
41
|
export declare abstract class AbstractLocalSqliteQuerierPool<DB extends SqliteDatabase, D extends SqliteDialect> extends AbstractSharedHandleQuerierPool<DB, SqliteQuerier, D> {
|
|
33
42
|
/** Opens the driver's database, reading integers as `bigint`, which the querier decodes exactly past 2^53. */
|
|
34
43
|
protected abstract createDb(): Promise<DB>;
|
|
44
|
+
/**
|
|
45
|
+
* WAL, so a reader does not wait on the writer, and `foreign_keys`, which SQLite ships off per connection:
|
|
46
|
+
* without it a declared `onDelete: 'CASCADE'` does nothing and a dangling reference is accepted.
|
|
47
|
+
*/
|
|
35
48
|
protected openDb(): Promise<DB>;
|
|
36
49
|
protected buildQuerier(db: DB): SqliteQuerier;
|
|
37
50
|
}
|
|
51
|
+
export {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AbstractSharedHandleQuerierPool } from '../querier/abstractSharedHandleQuerierPool.js';
|
|
2
|
-
import { applySqlitePragmas } from './sqlitePragmas.js';
|
|
3
2
|
import { SqliteQuerier } from './sqliteQuerier.js';
|
|
4
3
|
/**
|
|
5
4
|
* A `node:sqlite` or `bun:sqlite` handle as a {@link LocalSqliteDatabase}. Neither says whether a statement
|
|
@@ -17,21 +16,38 @@ export function adaptSqlite(db, reads) {
|
|
|
17
16
|
};
|
|
18
17
|
},
|
|
19
18
|
loadExtension: (path) => db.loadExtension(path),
|
|
19
|
+
function: db.function?.bind(db),
|
|
20
20
|
close: () => db.close(),
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* `db` extended past SQLite's own: each loadable extension installed, which `node:sqlite` refuses unless
|
|
25
|
+
* opened to allow them, and `REGEXP` defined as libSQL builds it in, which SQLite leaves to the application.
|
|
26
|
+
*/
|
|
27
|
+
export function extendSqlite(db, extensions = []) {
|
|
25
28
|
for (const extension of extensions) {
|
|
26
29
|
db.loadExtension(extension);
|
|
27
30
|
}
|
|
31
|
+
db.function?.('regexp', { deterministic: true }, regexp);
|
|
28
32
|
return db;
|
|
29
33
|
}
|
|
34
|
+
/** `value REGEXP pattern`: NULL where the value is, as an operator answers. */
|
|
35
|
+
function regexp(pattern, value) {
|
|
36
|
+
return value === null ? null : Number(new RegExp(String(pattern)).test(String(value)));
|
|
37
|
+
}
|
|
30
38
|
/** A pool for a database file opened in this process, configured the same way whichever driver's {@link createDb} opens it. */
|
|
31
39
|
export class AbstractLocalSqliteQuerierPool extends AbstractSharedHandleQuerierPool {
|
|
40
|
+
/**
|
|
41
|
+
* WAL, so a reader does not wait on the writer, and `foreign_keys`, which SQLite ships off per connection:
|
|
42
|
+
* without it a declared `onDelete: 'CASCADE'` does nothing and a dangling reference is accepted.
|
|
43
|
+
*/
|
|
32
44
|
async openDb() {
|
|
33
45
|
const db = await this.createDb();
|
|
34
|
-
|
|
46
|
+
for (const pragma of ['journal_mode = WAL', 'foreign_keys = ON']) {
|
|
47
|
+
const stmt = await db.prepare(`PRAGMA ${pragma}`);
|
|
48
|
+
// `journal_mode` answers with a row and `foreign_keys` with none.
|
|
49
|
+
await (stmt.reader ? stmt.all() : stmt.run());
|
|
50
|
+
}
|
|
35
51
|
return db;
|
|
36
52
|
}
|
|
37
53
|
buildQuerier(db) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { dialectOptionsFrom } from '../dialect/abstractDialect.js';
|
|
2
|
-
import { AbstractLocalSqliteQuerierPool, adaptSqlite,
|
|
2
|
+
import { AbstractLocalSqliteQuerierPool, adaptSqlite, extendSqlite, } from './localSqliteQuerierPool.js';
|
|
3
3
|
import { SqliteDialect } from './sqliteDialect.js';
|
|
4
4
|
/**
|
|
5
5
|
* A pool over Node's built-in `node:sqlite`, needing no dependency at all. {@link Sqlite3QuerierPool} is the
|
|
@@ -22,6 +22,6 @@ export class NodeSqliteQuerierPool extends AbstractLocalSqliteQuerierPool {
|
|
|
22
22
|
// `node:sqlite` refuses `loadExtension` unless the database was opened with this on.
|
|
23
23
|
...(extensions?.length ? { allowExtension: true } : undefined),
|
|
24
24
|
});
|
|
25
|
-
return
|
|
25
|
+
return extendSqlite(adaptSqlite(nodeDb, (stmt) => stmt.columns().length > 0), extensions);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -21,7 +21,6 @@ export const SQLITE_FEATURES = {
|
|
|
21
21
|
schemas: false, // SQLite's namespaces are attached database files, not declared objects
|
|
22
22
|
dropTableCascade: false,
|
|
23
23
|
rebuildsTables: true,
|
|
24
|
-
commentSyntax: 'none',
|
|
25
24
|
vectorIndexRequiresNotNull: false,
|
|
26
25
|
vectorSupportsLength: true,
|
|
27
26
|
vectorBytes: true,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { dialectOptionsFrom } from '../dialect/abstractDialect.js';
|
|
2
|
-
import { AbstractLocalSqliteQuerierPool, adaptSqlite,
|
|
2
|
+
import { AbstractLocalSqliteQuerierPool, adaptSqlite, extendSqlite, } from './localSqliteQuerierPool.js';
|
|
3
3
|
import { SqliteDialect } from './sqliteDialect.js';
|
|
4
4
|
/**
|
|
5
5
|
* Pool for `better-sqlite3`, or `bun:sqlite` when running under Bun - the same file, through whichever
|
|
@@ -25,9 +25,9 @@ export class Sqlite3QuerierPool extends AbstractLocalSqliteQuerierPool {
|
|
|
25
25
|
const bunDb = typeof this.filename === 'string'
|
|
26
26
|
? new Database(this.filename, bunOpts)
|
|
27
27
|
: Database.deserialize(this.filename, bunOpts);
|
|
28
|
-
return
|
|
28
|
+
return extendSqlite(adaptSqlite(bunDb, (stmt) => stmt.columnNames.length > 0), extensions);
|
|
29
29
|
}
|
|
30
30
|
const { default: BetterSqlite3 } = await import('better-sqlite3');
|
|
31
|
-
return
|
|
31
|
+
return extendSqlite(new BetterSqlite3(this.filename, driverOpts).defaultSafeIntegers(true), extensions);
|
|
32
32
|
}
|
|
33
33
|
}
|
package/dist/type/dialect.d.ts
CHANGED
|
@@ -98,8 +98,6 @@ export interface DialectFeatures {
|
|
|
98
98
|
* the migration builder refuses the change by name.
|
|
99
99
|
*/
|
|
100
100
|
readonly rebuildsTables: boolean;
|
|
101
|
-
/** Where a comment goes: in the declaration (MySQL family), a `COMMENT ON` of its own (Postgres family), or nowhere. */
|
|
102
|
-
readonly commentSyntax: 'inline' | 'statement' | 'none';
|
|
103
101
|
/**
|
|
104
102
|
* Whether every column of a vector index has to be `NOT NULL`, which MariaDB 12.3 enforces ("All
|
|
105
103
|
* parts of a VECTOR index must be NOT NULL") and CockroachDB 26.3 does not - so being indexed, not
|
package/dist/type/migration.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { AnyMigrationOperation } from '../migrate/builder/types.js';
|
|
2
|
-
import type { IndexFacet } from '../schema/indexDifferences.js';
|
|
3
2
|
import type { SchemaAST } from '../schema/schemaAST.js';
|
|
4
3
|
import type { DiffOptions } from '../schema/schemaASTDiffer.js';
|
|
5
4
|
import type { CheckSchema, ColumnNode, ForeignKeyAction, IndexType, TableNode, TriggerSchema } from '../schema/types.js';
|
|
6
|
-
import type {
|
|
5
|
+
import type { Except, IndexColumnSchema, IndexedVectorField, LoggingOptions, Querier, SqlQuerier, Type, VectorIndexOptions } from './index.js';
|
|
7
6
|
/**
|
|
8
7
|
* Defines a migration using a simple object literal. `Q` is `MongoQuerier` for a MongoDB migration.
|
|
9
8
|
*/
|
|
@@ -98,7 +97,7 @@ export interface MigrationResult {
|
|
|
98
97
|
readonly error?: Error;
|
|
99
98
|
}
|
|
100
99
|
/** A column as a statement renders one: a {@link ColumnNode} with the engine's type spelling and no graph links. */
|
|
101
|
-
export interface ColumnSchema extends Except<ColumnNode, 'type' | 'table'
|
|
100
|
+
export interface ColumnSchema extends Except<ColumnNode, 'type' | 'table'> {
|
|
102
101
|
/**
|
|
103
102
|
* The engine's own type spelling, `TINYINT(1)`, compared as stored: canonical types would differ where
|
|
104
103
|
* the engine stores them alike. `sqlToCanonical` reads it.
|
|
@@ -162,8 +161,7 @@ export interface IndexSchema extends VectorIndexOptions, IndexedVectorField {
|
|
|
162
161
|
}
|
|
163
162
|
/**
|
|
164
163
|
* A foreign key constraint, wherever one is described: read back by introspection, planned into a
|
|
165
|
-
* {@link SchemaDiff}, or declared through the migration builder
|
|
166
|
-
* differed only in spelling, and the translation between them was pure overhead.
|
|
164
|
+
* {@link SchemaDiff}, or declared through the migration builder: one shape for all three.
|
|
167
165
|
*/
|
|
168
166
|
export interface ForeignKeySchema {
|
|
169
167
|
/** Absent when nothing named it, which the generator fills in with `derivedForeignKeyName`. */
|
|
@@ -218,11 +216,6 @@ export interface PrimaryKeySchema {
|
|
|
218
216
|
export interface SchemaDiff {
|
|
219
217
|
/** Qualified where the table has a schema, since it is also the key the table is found under. */
|
|
220
218
|
readonly tableName: string;
|
|
221
|
-
/**
|
|
222
|
-
* The schema {@link tableName} is in, carried separately for the identifiers that live in it
|
|
223
|
-
* rather than name it: a Postgres index is dropped as `schema.index`, never `schema.table`.
|
|
224
|
-
*/
|
|
225
|
-
readonly schema?: string;
|
|
226
219
|
readonly type: 'create' | 'alter' | 'drop';
|
|
227
220
|
readonly primaryKey?: Change<PrimaryKeySchema>;
|
|
228
221
|
readonly columns?: readonly ColumnChange[];
|
|
@@ -273,8 +266,6 @@ export interface DropSchemaOptions {
|
|
|
273
266
|
* Interface for generating DDL statements from entity metadata
|
|
274
267
|
*/
|
|
275
268
|
export interface SchemaGenerator {
|
|
276
|
-
/** Whether a column's stored default is the one the entity declares, as the engine reprints it. Absent where columns are not compared. */
|
|
277
|
-
readonly defaultsEqual?: (expected: unknown, actual: unknown) => boolean;
|
|
278
269
|
/** The whole schema for `entities`, tables then the foreign keys between them, which need every entity at once. */
|
|
279
270
|
generateCreateSchema(entities: readonly Type<object>[], options?: CreateSchemaOptions): string[];
|
|
280
271
|
/**
|
|
@@ -283,33 +274,13 @@ export interface SchemaGenerator {
|
|
|
283
274
|
* rejected once the foreign keys are really there.
|
|
284
275
|
*/
|
|
285
276
|
generateDropSchema(entities: readonly Type<object>[], options?: DropSchemaOptions): string[];
|
|
286
|
-
/** Generate DROP TABLE statement. */
|
|
287
|
-
generateDropTable(tableName: string, options?: DropSchemaOptions): string;
|
|
288
277
|
/** The statements taking a table through `diff`; its rollback is the diff reversed, see `reverseDiff`. */
|
|
289
278
|
generateAlterTable(diff: SchemaDiff): string[];
|
|
290
|
-
/**
|
|
291
|
-
* Generate CREATE INDEX statement
|
|
292
|
-
*/
|
|
293
|
-
generateCreateIndex(tableName: string, index: IndexSchema): string;
|
|
294
|
-
/**
|
|
295
|
-
* Generate DROP INDEX statement
|
|
296
|
-
*/
|
|
297
|
-
generateDropIndex(tableName: string, indexName: string): string;
|
|
298
279
|
/**
|
|
299
280
|
* The statements one migration builder operation runs as, one string each. An operation the engine
|
|
300
281
|
* has no form for throws: MongoDB has no columns, constraints or SQL.
|
|
301
282
|
*/
|
|
302
283
|
generateOperation(operation: AnyMigrationOperation): string[];
|
|
303
|
-
/**
|
|
304
|
-
* The text of SQL an entity declares - a check, a stored computed column, an index expression or
|
|
305
|
-
* predicate - rendered for this engine, which is what building an entity's schema needs from it.
|
|
306
|
-
*/
|
|
307
|
-
compileDdl(sql: EntityWhereMeta<object>, entity: Type<object>): string;
|
|
308
|
-
/**
|
|
309
|
-
* A partial index's `$where` as this engine writes it into {@link IndexSchema.where}, refused where its
|
|
310
|
-
* index takes less of a predicate than a query does: SQL Server's filter has no `OR`.
|
|
311
|
-
*/
|
|
312
|
-
compileIndexPredicate(where: EntityWhereMeta<object>, entity: Type<object>, indexName: string): string;
|
|
313
284
|
/**
|
|
314
285
|
* An entity's differences from its table. `desiredAst`, from {@link buildAST}, has to span every entity
|
|
315
286
|
* a foreign key here points at, or those keys read as matching.
|
|
@@ -320,33 +291,11 @@ export interface SchemaGenerator {
|
|
|
320
291
|
buildAST?(entities: readonly Type<object>[]): SchemaAST;
|
|
321
292
|
/** How this engine's diff compares types and defaults. Absent where {@link buildAST} is. */
|
|
322
293
|
diffOptions?(): DiffOptions;
|
|
323
|
-
/**
|
|
324
|
-
* The table's key: {@link resolveTableAlias} behind {@link resolveSchema}, which is how a
|
|
325
|
-
* `SchemaAST` stores it and how a diff finds it again.
|
|
326
|
-
*/
|
|
327
|
-
resolveTableName<E>(meta: EntityMeta<E>): string;
|
|
328
|
-
/**
|
|
329
|
-
* The table's own name, unqualified. What a derived index or constraint name is built from, since
|
|
330
|
-
* those are single identifiers.
|
|
331
|
-
*/
|
|
332
|
-
resolveTableAlias<E>(meta: EntityMeta<E>): string;
|
|
333
|
-
/** The schema the table lives in, `undefined` where nothing named one. */
|
|
334
|
-
resolveSchema<E>(meta: EntityMeta<E>): string | undefined;
|
|
335
|
-
/**
|
|
336
|
-
* Resolve column name using field options and naming strategy
|
|
337
|
-
*/
|
|
338
|
-
resolveColumnName(key: string, field: FieldOptions): string;
|
|
339
294
|
}
|
|
340
295
|
/**
|
|
341
296
|
* Interface for introspecting the current database schema
|
|
342
297
|
*/
|
|
343
298
|
export interface SchemaIntrospector {
|
|
344
|
-
/**
|
|
345
|
-
* What this introspector can read back about an index, and so all that diffing may compare.
|
|
346
|
-
* Comparing a feature it cannot read reports the same drift forever: the entity side declares it,
|
|
347
|
-
* the database side never reports it, and no migration can close the gap.
|
|
348
|
-
*/
|
|
349
|
-
readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
350
299
|
/**
|
|
351
300
|
* The whole database, or just the tables named. Names nothing matches are left out. `renames` reads
|
|
352
301
|
* each column under the name it is being renamed to, so a diff compares it as the column it becomes.
|
|
@@ -15,8 +15,8 @@ export declare function declaredIndexes<E>(meta: EntityMeta<E>): EntityIndexMeta
|
|
|
15
15
|
export declare function renderIndexColumn(entry: EntityIndexColumn, render: (sql: QueryRaw) => string): IndexColumnSchema;
|
|
16
16
|
/** What an unnamed index's name is built from: each entry's column, or `expr<n>` for an expression, which has none. */
|
|
17
17
|
export declare function indexNameParts(entries: readonly EntityIndexColumn[]): string[];
|
|
18
|
-
/** The name an index is created and read by: its own, else one derived from its table
|
|
19
|
-
export declare function declaredIndexName(name: string | undefined, table: string, entries: readonly EntityIndexColumn[]): string;
|
|
18
|
+
/** The name an index is created and read by: its own, else one derived from its table, entries' columns and uniqueness. */
|
|
19
|
+
export declare function declaredIndexName(name: string | undefined, table: string, entries: readonly EntityIndexColumn[], unique?: boolean): string;
|
|
20
20
|
/**
|
|
21
21
|
* A check as uql installs it: named for its table and `label`, the author's name or `ck`, then a hash
|
|
22
22
|
* of `expression`, so an edited check is a new name and reordering the checks renames none.
|
|
@@ -36,9 +36,9 @@ export function renderIndexColumn(entry, render) {
|
|
|
36
36
|
export function indexNameParts(entries) {
|
|
37
37
|
return entries.map((entry, at) => (typeof entry.column === 'string' ? entry.column : `expr${at}`));
|
|
38
38
|
}
|
|
39
|
-
/** The name an index is created and read by: its own, else one derived from its table
|
|
40
|
-
export function declaredIndexName(name, table, entries) {
|
|
41
|
-
return name ?? derivedIndexName(table, indexNameParts(entries));
|
|
39
|
+
/** The name an index is created and read by: its own, else one derived from its table, entries' columns and uniqueness. */
|
|
40
|
+
export function declaredIndexName(name, table, entries, unique = false) {
|
|
41
|
+
return name ?? derivedIndexName(table, indexNameParts(entries), unique);
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* A check as uql installs it: named for its table and `label`, the author's name or `ck`, then a hash
|
package/dist/util/sql.util.d.ts
CHANGED
|
@@ -17,14 +17,6 @@ export declare function splitQualifiedName(qualified: string): {
|
|
|
17
17
|
readonly name: string;
|
|
18
18
|
readonly schema?: string;
|
|
19
19
|
};
|
|
20
|
-
/**
|
|
21
|
-
* The name of a derived index or constraint, such as `Order__total_idx`, with the kind last as Postgres names
|
|
22
|
-
* its own. It leaves out the table's schema, since it is a single identifier. The AST, the DDL and a `DROP`
|
|
23
|
-
* all use this one rule.
|
|
24
|
-
*/
|
|
25
|
-
export declare function derivedConstraintName(table: string, parts: readonly string[], kind: ConstraintKind): string;
|
|
26
|
-
/** The kinds of derived name, which is also what `indexNameStem` strips to compare them. */
|
|
27
|
-
export type ConstraintKind = 'pk' | 'fk' | 'idx' | 'uk';
|
|
28
20
|
/**
|
|
29
21
|
* Whether uql installed the object called `name`. Ownership is the prefix and nothing else, since no
|
|
30
22
|
* engine records who created one - so this is the only thing standing between a hand-written trigger or
|
package/dist/util/sql.util.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { OWNED_PREFIX, UPSERT_CREATED_ALIAS } from '../dialect/aliases.js';
|
|
2
2
|
import { hasKeys, isRecord } from './object.util.js';
|
|
3
3
|
import { fnv1a } from './string.util.js';
|
|
4
|
-
/** Pre-computed regex for each SQL identifier escape character to avoid per-call allocation. */
|
|
5
|
-
const escapeIdRegexCache = { '`': /`/g, '"': /"/g };
|
|
6
4
|
/** A row with its dotted columns nested, by paths read once off its statement's first row: the row itself where none is. */
|
|
7
5
|
export function unflatObject(row, attrsPaths) {
|
|
8
6
|
if (!hasKeys(attrsPaths)) {
|
|
@@ -62,7 +60,7 @@ const NAME_HASH_LENGTH = 6;
|
|
|
62
60
|
* its own. It leaves out the table's schema, since it is a single identifier. The AST, the DDL and a `DROP`
|
|
63
61
|
* all use this one rule.
|
|
64
62
|
*/
|
|
65
|
-
|
|
63
|
+
function derivedConstraintName(table, parts, kind) {
|
|
66
64
|
const { name } = splitQualifiedName(table);
|
|
67
65
|
const body = parts.length ? `${name}${TABLE_SEPARATOR}${parts.join('_')}` : name;
|
|
68
66
|
return clampIdentifier(`${body}_${kind}`);
|
|
@@ -126,16 +124,13 @@ export function escapeSqlId(val, escapeIdChar = '`', forbidQualified, addDot) {
|
|
|
126
124
|
if (!val) {
|
|
127
125
|
return '';
|
|
128
126
|
}
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
const escaped = !forbidQualified && val.includes('.')
|
|
128
|
+
? val
|
|
131
129
|
.split('.')
|
|
132
|
-
.map((
|
|
133
|
-
.join('.')
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
const escaped = escapeIdChar + val.replace(escapeIdRegexCache[escapeIdChar], escapeIdChar + escapeIdChar) + escapeIdChar;
|
|
137
|
-
const suffix = addDot ? '.' : '';
|
|
138
|
-
return escaped + suffix;
|
|
130
|
+
.map((part) => escapeSqlId(part, escapeIdChar, true))
|
|
131
|
+
.join('.')
|
|
132
|
+
: `${escapeIdChar}${val.replaceAll(escapeIdChar, escapeIdChar + escapeIdChar)}${escapeIdChar}`;
|
|
133
|
+
return addDot ? `${escaped}.` : escaped;
|
|
139
134
|
}
|
|
140
135
|
/**
|
|
141
136
|
* A driver's result as a {@link QueryUpdateResult}: `RETURNING` rows name their id `id`; a MySQL header's
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"homepage": "https://uql-orm.dev",
|
|
4
4
|
"description": "JSON-native TypeScript ORM for Bun, Browsers, Edge, Deno, Node, Workers. Supports PostgreSQL, PGlite, MySQL, MariaDB, SQLite, CockroachDB, SQL Server, Turso, Neon, Cloudflare D1 and MongoDB. Queries are plain JSON, typed to the leaf.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.98.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=24"
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { AbstractSqlDialect } from '../../dialect/index.js';
|
|
2
|
-
import type { IndexFacet } from '../../schema/indexDifferences.js';
|
|
3
|
-
import { SchemaAST } from '../../schema/schemaAST.js';
|
|
4
|
-
import type { ColumnRenames, TableSchema } from '../../type/migration.js';
|
|
5
|
-
/**
|
|
6
|
-
* Base class for SQL introspectors with shared AST building logic.
|
|
7
|
-
*/
|
|
8
|
-
export declare abstract class BaseSqlIntrospector {
|
|
9
|
-
protected readonly dialect: AbstractSqlDialect;
|
|
10
|
-
readonly schema?: string | undefined;
|
|
11
|
-
/** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
|
|
12
|
-
readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
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: AbstractSqlDialect, schema?: string | undefined);
|
|
19
|
-
/**
|
|
20
|
-
* The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
|
|
21
|
-
* rather than raised: the point of naming them is to read a database other things are still
|
|
22
|
-
* changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
|
|
23
|
-
*/
|
|
24
|
-
introspect(tables?: readonly string[], renames?: ColumnRenames): Promise<SchemaAST>;
|
|
25
|
-
/** `table` with each column `renames` names under its new name, wherever the table names it. */
|
|
26
|
-
private renamed;
|
|
27
|
-
abstract getTableNames(): Promise<string[]>;
|
|
28
|
-
abstract getTableSchema(tableName: string): Promise<TableSchema | undefined>;
|
|
29
|
-
/**
|
|
30
|
-
* Build SchemaAST from table schemas.
|
|
31
|
-
*/
|
|
32
|
-
protected buildAST(tableSchemas: TableSchema[]): SchemaAST;
|
|
33
|
-
private buildTable;
|
|
34
|
-
private buildRelationships;
|
|
35
|
-
private buildIndexes;
|
|
36
|
-
}
|
|
@@ -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
|
-
import { renameIndexEntries } from '../schemaChange.js';
|
|
6
|
-
/**
|
|
7
|
-
* Base class for SQL introspectors with shared AST building logic.
|
|
8
|
-
*/
|
|
9
|
-
export class BaseSqlIntrospector {
|
|
10
|
-
dialect;
|
|
11
|
-
schema;
|
|
12
|
-
/** Columns and uniqueness only; each introspector opts in to what its catalogue queries report. */
|
|
13
|
-
indexFacets = new Set();
|
|
14
|
-
/**
|
|
15
|
-
* The one schema these queries read, `undefined` for the connection's own default. Every table
|
|
16
|
-
* reported is stamped with it, so a diff compares like with like: entity and database both say
|
|
17
|
-
* `undefined` for "wherever the connection points", and name a schema only when one was asked for.
|
|
18
|
-
*/
|
|
19
|
-
constructor(dialect, schema) {
|
|
20
|
-
this.dialect = dialect;
|
|
21
|
-
this.schema = schema;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
|
|
25
|
-
* rather than raised: the point of naming them is to read a database other things are still
|
|
26
|
-
* changing, where scanning every table is both wasted work and a relation that can vanish mid-scan.
|
|
27
|
-
*/
|
|
28
|
-
async introspect(tables, renames) {
|
|
29
|
-
const tableNames = tables ?? (await this.getTableNames());
|
|
30
|
-
const tableSchemas = [];
|
|
31
|
-
for (const tableName of tableNames) {
|
|
32
|
-
const schema = await this.getTableSchema(tableName);
|
|
33
|
-
if (schema) {
|
|
34
|
-
tableSchemas.push(renames ? this.renamed(schema, renames) : schema);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return this.buildAST(tableSchemas);
|
|
38
|
-
}
|
|
39
|
-
/** `table` with each column `renames` names under its new name, wherever the table names it. */
|
|
40
|
-
renamed(table, renames) {
|
|
41
|
-
const nameIn = (tableName) => (column) => renames.get(qualifyName(tableName, this.schema))?.find((rename) => rename.from === column)?.to ?? column;
|
|
42
|
-
const own = nameIn(table.name);
|
|
43
|
-
return {
|
|
44
|
-
...table,
|
|
45
|
-
columns: table.columns.map((column) => ({ ...column, name: own(column.name) })),
|
|
46
|
-
primaryKey: table.primaryKey && { ...table.primaryKey, columns: table.primaryKey.columns.map(own) },
|
|
47
|
-
indexes: table.indexes?.map((index) => ({ ...index, entries: renameIndexEntries(index.entries, own) })),
|
|
48
|
-
foreignKeys: table.foreignKeys?.map((foreignKey) => ({
|
|
49
|
-
...foreignKey,
|
|
50
|
-
columns: foreignKey.columns.map(own),
|
|
51
|
-
references: {
|
|
52
|
-
...foreignKey.references,
|
|
53
|
-
columns: foreignKey.references.columns.map(nameIn(foreignKey.references.table)),
|
|
54
|
-
},
|
|
55
|
-
})),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Build SchemaAST from table schemas.
|
|
60
|
-
*/
|
|
61
|
-
buildAST(tableSchemas) {
|
|
62
|
-
const ast = new SchemaAST();
|
|
63
|
-
// Every table first, so a foreign key reaches a table introspected after the one it leaves from.
|
|
64
|
-
const tables = tableSchemas.map((schema) => ({ schema, table: this.buildTable(schema) }));
|
|
65
|
-
const tableNodes = new Map(tables.map(({ schema, table }) => [schema.name, table]));
|
|
66
|
-
for (const { table } of tables) {
|
|
67
|
-
ast.addTable(table);
|
|
68
|
-
}
|
|
69
|
-
for (const { schema, table } of tables) {
|
|
70
|
-
this.buildRelationships(ast, tableNodes, schema, table);
|
|
71
|
-
this.buildIndexes(ast, schema, table);
|
|
72
|
-
}
|
|
73
|
-
return ast;
|
|
74
|
-
}
|
|
75
|
-
buildTable(schema) {
|
|
76
|
-
const table = createTableNode(schema.name, this.schema, this.indexFacets);
|
|
77
|
-
const { columns } = table;
|
|
78
|
-
for (const col of schema.columns) {
|
|
79
|
-
// Spread, not field by field: a `ColumnSchema` is a `ColumnNode` minus the graph links, so
|
|
80
|
-
// everything but the type crosses unchanged and a field either shape gains cannot be dropped
|
|
81
|
-
// here. Listed by hand this had already lost `enum` and `generatedAs`.
|
|
82
|
-
const { type, length: _length, precision: _precision, scale: _scale, ...rest } = col;
|
|
83
|
-
const column = {
|
|
84
|
-
...rest,
|
|
85
|
-
// A timestamp with no stated precision has the engine's default; otherwise an entity would assume 3.
|
|
86
|
-
type: withTimestampPrecision(canonicalColumnType(type, col), defaultTimestampPrecision(this.dialect.dialectName)),
|
|
87
|
-
table,
|
|
88
|
-
referencedBy: [],
|
|
89
|
-
};
|
|
90
|
-
columns.set(col.name, column);
|
|
91
|
-
}
|
|
92
|
-
// From the ordered key the query returned, not from the per-column flags: `(a, b)` is a different
|
|
93
|
-
// key from `(b, a)`, and a flag says only that a column is *in* the key. Falls back to the flags
|
|
94
|
-
// for an introspector that reports no key of its own.
|
|
95
|
-
table.primaryKey = schema.primaryKey ?? keyOfColumns(schema.columns);
|
|
96
|
-
table.checks.push(...(schema.checks ?? []));
|
|
97
|
-
table.triggers.push(...(schema.triggers ?? []));
|
|
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
|
-
}
|
package/dist/schema/index.d.ts
DELETED
package/dist/schema/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { SchemaAST } from './schemaAST.js';
|
|
@@ -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
|
-
}
|