uql-orm 0.99.0 → 0.100.1

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 (174) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +1 -1
  3. package/dist/browser/uql-browser.min.js +2 -2
  4. package/dist/browser/uql-browser.min.js.map +5 -5
  5. package/dist/bunSql/bunSql.util.js +2 -2
  6. package/dist/bunSql/bunSqlQuerier.js +0 -1
  7. package/dist/bunSql/bunSqlQuerierPool.js +1 -1
  8. package/dist/cockroachdb/cockroachDialect.js +1 -0
  9. package/dist/context/context.browser.d.ts +5 -0
  10. package/dist/context/context.browser.js +21 -11
  11. package/dist/context/context.d.ts +5 -0
  12. package/dist/context/context.js +8 -3
  13. package/dist/dialect/abstractDialect.js +4 -2
  14. package/dist/dialect/abstractSqlDialect.d.ts +5 -5
  15. package/dist/dialect/abstractSqlDialect.js +26 -18
  16. package/dist/dialect/aliases.d.ts +0 -2
  17. package/dist/dialect/aliases.js +0 -2
  18. package/dist/dialect/hydrateColumn.d.ts +3 -3
  19. package/dist/dialect/hydrateColumn.js +1 -0
  20. package/dist/dialect/index.js +0 -2
  21. package/dist/dialect/jsonSql.d.ts +2 -3
  22. package/dist/dialect/jsonSql.js +4 -6
  23. package/dist/dialect/mysqlLikeSqlDialect.d.ts +1 -1
  24. package/dist/dialect/mysqlLikeSqlDialect.js +5 -3
  25. package/dist/dialect/operators.d.ts +2 -3
  26. package/dist/dialect/operators.js +3 -4
  27. package/dist/dialect/pgLikeSqlDialect.js +12 -12
  28. package/dist/entity/decorator/members.d.ts +4 -12
  29. package/dist/entity/metadata/definition.js +8 -5
  30. package/dist/http/handler.d.ts +6 -6
  31. package/dist/http/handler.js +20 -5
  32. package/dist/index.d.ts +6 -5
  33. package/dist/index.js +6 -5
  34. package/dist/{maria → mariadb}/mariadbQuerier.js +1 -1
  35. package/dist/migrate/builder/columnBuilder.d.ts +3 -3
  36. package/dist/migrate/builder/columnBuilder.js +1 -1
  37. package/dist/migrate/builder/index.d.ts +0 -3
  38. package/dist/migrate/builder/index.js +0 -3
  39. package/dist/migrate/builder/migrationBuilder.d.ts +6 -6
  40. package/dist/migrate/builder/migrationBuilder.js +7 -7
  41. package/dist/migrate/builder/tableBuilder.d.ts +25 -25
  42. package/dist/migrate/builder/tableBuilder.js +5 -5
  43. package/dist/migrate/builder/types.d.ts +37 -37
  44. package/dist/migrate/cli-config.d.ts +11 -1
  45. package/dist/migrate/cli-config.js +20 -6
  46. package/dist/migrate/cli.js +9 -18
  47. package/dist/migrate/codegen/entityCodeGenerator.js +1 -1
  48. package/dist/migrate/codegen/migrationFile.d.ts +7 -7
  49. package/dist/migrate/codegen/migrationFile.js +11 -12
  50. package/dist/migrate/ddl/indexCapabilities.d.ts +2 -0
  51. package/dist/migrate/ddl/indexCapabilities.js +6 -0
  52. package/dist/migrate/ddl/indexDdl.d.ts +8 -0
  53. package/dist/migrate/ddl/indexDdl.js +22 -6
  54. package/dist/migrate/generator/definitionToNode.d.ts +1 -1
  55. package/dist/migrate/generator/definitionToNode.js +1 -1
  56. package/dist/migrate/index.d.ts +1 -3
  57. package/dist/migrate/index.js +0 -3
  58. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +11 -15
  59. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +13 -15
  60. package/dist/migrate/introspection/mssqlIntrospector.d.ts +5 -5
  61. package/dist/migrate/introspection/mssqlIntrospector.js +24 -28
  62. package/dist/migrate/introspection/mysqlIntrospector.d.ts +5 -5
  63. package/dist/migrate/introspection/mysqlIntrospector.js +31 -37
  64. package/dist/migrate/introspection/postgresIntrospector.d.ts +13 -13
  65. package/dist/migrate/introspection/postgresIntrospector.js +35 -39
  66. package/dist/migrate/introspection/registry.js +1 -1
  67. package/dist/migrate/introspection/sqliteIntrospector.d.ts +6 -4
  68. package/dist/migrate/introspection/sqliteIntrospector.js +15 -12
  69. package/dist/migrate/migrationLock.d.ts +38 -0
  70. package/dist/migrate/migrationLock.js +103 -0
  71. package/dist/migrate/migrationTarget.d.ts +6 -2
  72. package/dist/migrate/migrationTarget.js +30 -14
  73. package/dist/migrate/migrator.d.ts +39 -23
  74. package/dist/migrate/migrator.js +110 -74
  75. package/dist/migrate/schemaGenerator.d.ts +3 -1
  76. package/dist/migrate/schemaGenerator.js +14 -5
  77. package/dist/migrate/storage/databaseStorage.d.ts +28 -20
  78. package/dist/migrate/storage/databaseStorage.js +36 -54
  79. package/dist/migrate/storage/index.d.ts +0 -1
  80. package/dist/migrate/storage/index.js +0 -1
  81. package/dist/{mongo → mongodb}/index.d.ts +0 -1
  82. package/dist/{mongo → mongodb}/index.js +0 -1
  83. package/dist/{mongo → mongodb}/mongoCommand.d.ts +17 -1
  84. package/dist/{mongo → mongodb}/mongoCommand.js +15 -2
  85. package/dist/{mongo → mongodb}/mongoDialect.js +8 -12
  86. package/dist/{mongo → mongodb}/mongoIntrospector.js +12 -3
  87. package/dist/{mongo → mongodb}/mongoMigrationStorage.d.ts +5 -5
  88. package/dist/{mongo → mongodb}/mongoMigrationStorage.js +8 -12
  89. package/dist/{mongo → mongodb}/mongoQuerier.d.ts +3 -1
  90. package/dist/{mongo → mongodb}/mongoSchemaGenerator.d.ts +11 -1
  91. package/dist/{mongo → mongodb}/mongoSchemaGenerator.js +56 -11
  92. package/dist/{mongo → mongodb}/mongodbQuerier.d.ts +7 -18
  93. package/dist/{mongo → mongodb}/mongodbQuerier.js +44 -78
  94. package/dist/mssql/mssqlDialect.d.ts +6 -4
  95. package/dist/mssql/mssqlDialect.js +20 -10
  96. package/dist/mssql/mssqlQuerier.d.ts +2 -2
  97. package/dist/mssql/mssqlQuerier.js +3 -3
  98. package/dist/mssql/mssqlQuerierPool.d.ts +1 -1
  99. package/dist/mssql/mssqlQuerierPool.js +2 -2
  100. package/dist/mysql/mysql2Querier.js +1 -5
  101. package/dist/namingStrategy/defaultNamingStrategy.d.ts +0 -1
  102. package/dist/namingStrategy/defaultNamingStrategy.js +0 -3
  103. package/dist/pglite/pgliteDialect.d.ts +3 -0
  104. package/dist/pglite/pgliteDialect.js +3 -1
  105. package/dist/pglite/pgliteQuerier.d.ts +2 -0
  106. package/dist/pglite/pgliteQuerier.js +4 -0
  107. package/dist/postgres/postgresDialect.d.ts +3 -4
  108. package/dist/postgres/postgresDialect.js +5 -6
  109. package/dist/querier/abstractQuerier.d.ts +119 -36
  110. package/dist/querier/abstractQuerier.js +465 -161
  111. package/dist/querier/abstractQuerierPool.d.ts +5 -6
  112. package/dist/querier/abstractQuerierPool.js +20 -6
  113. package/dist/querier/abstractSqlQuerier.d.ts +15 -10
  114. package/dist/querier/abstractSqlQuerier.js +54 -47
  115. package/dist/querier/abstractSqlQuerierPool.d.ts +5 -3
  116. package/dist/querier/abstractSqlQuerierPool.js +7 -4
  117. package/dist/querier/transaction.d.ts +46 -0
  118. package/dist/querier/transaction.js +56 -0
  119. package/dist/schema/canonicalType.js +2 -2
  120. package/dist/schema/schemaASTDiffer.d.ts +11 -3
  121. package/dist/schema/schemaASTDiffer.js +20 -8
  122. package/dist/sqlite/nodeSqliteQuerierPool.d.ts +1 -1
  123. package/dist/sqlite/nodeSqliteQuerierPool.js +1 -1
  124. package/dist/sqlite/sqliteDialect.d.ts +1 -1
  125. package/dist/sqlite/sqliteDialect.js +3 -1
  126. package/dist/sqlite/sqliteQuerier.d.ts +2 -0
  127. package/dist/sqlite/sqliteQuerier.js +4 -0
  128. package/dist/sqlite/sqliteQuerierPool.d.ts +4 -4
  129. package/dist/sqlite/sqliteQuerierPool.js +1 -1
  130. package/dist/type/dialect.d.ts +16 -2
  131. package/dist/type/entity.d.ts +32 -32
  132. package/dist/type/migration.d.ts +10 -27
  133. package/dist/type/namingStrategy.d.ts +0 -4
  134. package/dist/type/querier.d.ts +18 -10
  135. package/dist/type/query.d.ts +7 -22
  136. package/dist/type/queryRaw.d.ts +11 -6
  137. package/dist/type/queryRaw.js +2 -3
  138. package/dist/type/universalQuerier.d.ts +26 -28
  139. package/dist/type/utility.d.ts +7 -2
  140. package/dist/util/dialect.util.d.ts +2 -2
  141. package/dist/util/dialect.util.js +4 -1
  142. package/dist/util/field.util.d.ts +12 -0
  143. package/dist/util/field.util.js +24 -0
  144. package/dist/util/hook.util.d.ts +3 -2
  145. package/dist/util/query.util.d.ts +5 -0
  146. package/dist/util/query.util.js +34 -1
  147. package/dist/util/raw.d.ts +8 -2
  148. package/dist/util/raw.js +22 -0
  149. package/dist/util/relationQuery.util.d.ts +2 -0
  150. package/dist/util/relationQuery.util.js +4 -0
  151. package/dist/util/sql.util.d.ts +0 -5
  152. package/dist/util/sql.util.js +3 -9
  153. package/dist/util/uqlError.d.ts +0 -6
  154. package/dist/util/uqlError.js +0 -5
  155. package/package.json +20 -26
  156. package/skills/uql-orm/SKILL.md +13 -10
  157. package/dist/migrate/storage/jsonStorage.d.ts +0 -14
  158. package/dist/migrate/storage/jsonStorage.js +0 -33
  159. /package/dist/{maria → mariadb}/index.d.ts +0 -0
  160. /package/dist/{maria → mariadb}/index.js +0 -0
  161. /package/dist/{maria → mariadb}/mariaDialect.d.ts +0 -0
  162. /package/dist/{maria → mariadb}/mariaDialect.js +0 -0
  163. /package/dist/{maria → mariadb}/mariadbQuerier.d.ts +0 -0
  164. /package/dist/{maria → mariadb}/mariadbQuerierPool.d.ts +0 -0
  165. /package/dist/{maria → mariadb}/mariadbQuerierPool.js +0 -0
  166. /package/dist/{mongo → mongodb}/mongoDialect.d.ts +0 -0
  167. /package/dist/{mongo → mongodb}/mongoIntrospector.d.ts +0 -0
  168. /package/dist/{mongo → mongodb}/mongoQuerier.js +0 -0
  169. /package/dist/{mongo → mongodb}/mongodbQuerierPool.d.ts +0 -0
  170. /package/dist/{mongo → mongodb}/mongodbQuerierPool.js +0 -0
  171. /package/dist/{mongo → mongodb}/textLanguage.d.ts +0 -0
  172. /package/dist/{mongo → mongodb}/textLanguage.js +0 -0
  173. /package/dist/{mongo → mongodb}/vectorDistance.d.ts +0 -0
  174. /package/dist/{mongo → mongodb}/vectorDistance.js +0 -0
@@ -1,12 +1,12 @@
1
- import { mongoCommandSource } from '../../mongo/mongoCommand.js';
1
+ import { mongoCommandSource } from '../../mongodb/mongoCommand.js';
2
2
  /**
3
- * Emit one `await querier.run(...)` line for entity-generated migrations.
4
- * Uses `JSON.stringify` so SQL with backticks (SQLite/LibSQL), quotes, `${`, etc. stays valid TS source.
3
+ * One `await querier.run`...`` line, the SQL a template binding nothing, so its lines and quotes read as
4
+ * written; a backslash, a backtick and a `${` are escaped.
5
5
  */
6
6
  export function emitSqlRunCall(sql) {
7
- return /*ts*/ ` await querier.run(${JSON.stringify(sql)});`;
7
+ return /*ts*/ ` await querier.run\`${sql.replace(/[\\`]|\$\{/g, (char) => `\\${char}`)}\`;`;
8
8
  }
9
- /** Indented `up`/`down` body: one `await querier.run(...)` per SQL string. */
9
+ /** Indented `up`/`down` body: one `await querier.run`...`` per SQL string. */
10
10
  export function emitSqlRunCalls(statements) {
11
11
  return statements.map(emitSqlRunCall).join('\n');
12
12
  }
@@ -17,20 +17,19 @@ export function emitMongoCommandCalls(statements) {
17
17
  export const migrationSource = {
18
18
  SqlQuerier: {
19
19
  querier: 'SqlQuerier',
20
- module: 'uql-orm/migrate',
21
- emptyUp: ` // Add your migration logic here.
22
- // Use one await querier.run("...") per SQL statement when possible (same style as generate:entities).
20
+ entry: 'uql-orm',
21
+ emptyUp: ` // Add your migration logic here: one await querier.run\`...\` per SQL statement.
23
22
  // Example (Postgres):
24
- // await querier.run("CREATE TABLE \\"users\\" (\\"id\\" SERIAL PRIMARY KEY);");
23
+ // await querier.run\`CREATE TABLE "users" ("id" SERIAL PRIMARY KEY)\`;
25
24
  `,
26
25
  emptyDown: ` // Add your rollback logic here.
27
- // await querier.run("DROP TABLE IF EXISTS \\"users\\";");
26
+ // await querier.run\`DROP TABLE IF EXISTS "users"\`;
28
27
  `,
29
28
  emit: emitSqlRunCalls,
30
29
  },
31
30
  MongoQuerier: {
32
31
  querier: 'MongoQuerier',
33
- module: 'uql-orm/mongo',
32
+ entry: 'uql-orm/mongodb',
34
33
  emptyUp: ` // Add your migration logic here, through the database handle.
35
34
  // await querier.db.collection('users').updateMany({}, { $set: { active: true } });
36
35
  `,
@@ -47,7 +46,7 @@ export function buildMigrationModule(options) {
47
46
  const querier = options.querier ?? 'SqlQuerier';
48
47
  const iso = options.createdAt.toISOString();
49
48
  const extra = options.docExtraLines?.map((line) => `\n * ${line}`).join('') ?? '';
50
- return /*ts*/ `import type { ${querier} } from '${migrationSource[querier].module}';
49
+ return /*ts*/ `import type { ${querier} } from '${migrationSource[querier].entry}';
51
50
 
52
51
  /**
53
52
  * Migration: ${options.migrationName}
@@ -13,6 +13,8 @@ export type IndexCapabilities = {
13
13
  readonly keywords: ReadonlyMap<IndexType, string>;
14
14
  /** What to declare instead of a type it lacks, appended to the refusal. */
15
15
  readonly hints: ReadonlyMap<IndexType, string>;
16
+ /** Whether `CREATE TABLE` takes a plain index among its definitions, built with the table. */
17
+ readonly inline: boolean;
16
18
  };
17
19
  /** Each engine's {@link IndexCapabilities}, total over the SQL engines. */
18
20
  export declare const INDEX_CAPABILITIES: Readonly<Record<SqlDialectName, IndexCapabilities>>;
@@ -12,6 +12,7 @@ export const INDEX_CAPABILITIES = {
12
12
  features: new Set(['expression', 'partial', 'nullsOrder', 'opsClass', 'include', 'jsonPath']),
13
13
  keywords: NONE,
14
14
  hints: NONE,
15
+ inline: false,
15
16
  },
16
17
  /** v26.3 answers `hash` and `brin` "unimplemented", `ivfflat` "unrecognized"; no nulls order nor operator class. */
17
18
  cockroachdb: {
@@ -19,6 +20,7 @@ export const INDEX_CAPABILITIES = {
19
20
  features: new Set(['expression', 'partial', 'include', 'jsonPath']),
20
21
  keywords: new Map([['vector', 'VECTOR INDEX']]),
21
22
  hints: new Map([['ivfflat', DECLARE_VECTOR]]),
23
+ inline: true,
22
24
  },
23
25
  /** No vector index: MySQL 26.7 has no distance function outside HeatWave, and `VECTOR INDEX` is MariaDB's. */
24
26
  mysql: {
@@ -26,6 +28,7 @@ export const INDEX_CAPABILITIES = {
26
28
  features: new Set(['expression', 'prefixLength', 'jsonPath', 'jsonArray']),
27
29
  keywords: MYSQL_LIKE_KEYWORDS,
28
30
  hints: new Map(VECTOR_INDEX_TYPES.map((type) => [type, '. Vector search on MySQL needs HeatWave'])),
31
+ inline: true,
29
32
  },
30
33
  /**
31
34
  * No functional index, even on 12.3, where a generated column is the documented workaround: so no JSON index
@@ -39,6 +42,7 @@ export const INDEX_CAPABILITIES = {
39
42
  ['hnsw', DECLARE_VECTOR],
40
43
  ['ivfflat', DECLARE_VECTOR],
41
44
  ]),
45
+ inline: true,
42
46
  },
43
47
  /**
44
48
  * 2025 rejects an expression (Msg 16216), the subquery a JSON path compiles to (Msg 1046), and any type but
@@ -49,6 +53,7 @@ export const INDEX_CAPABILITIES = {
49
53
  features: new Set(['partial']),
50
54
  keywords: NONE,
51
55
  hints: NONE,
56
+ inline: false,
52
57
  },
53
58
  /** Every type builds a plain index, so an entity written for Postgres migrates; libSQL's vector is DiskANN. */
54
59
  sqlite: {
@@ -56,5 +61,6 @@ export const INDEX_CAPABILITIES = {
56
61
  features: new Set(['expression', 'partial', 'jsonPath']),
57
62
  keywords: NONE,
58
63
  hints: NONE,
64
+ inline: false,
59
65
  },
60
66
  };
@@ -18,6 +18,14 @@ export declare class IndexDdl {
18
18
  getCreateIndexStatement(tableName: string, index: IndexSchema, opts?: {
19
19
  ifNotExists?: boolean;
20
20
  }): string;
21
+ /**
22
+ * A plain index as a definition of its `CREATE TABLE`, where the engine takes one; `undefined` for its own
23
+ * `CREATE INDEX`. Never a unique one: CockroachDB makes that a constraint, which `DROP INDEX` refuses.
24
+ */
25
+ inlineDefinition(index: IndexSchema): string | undefined;
26
+ private assertExpressible;
27
+ /** What follows the name and access method, in either form: the columns, then what qualifies them. */
28
+ private indexBody;
21
29
  /** What an index added to a table that has rows needs run after it to serve queries; nothing, mostly. */
22
30
  settleStatements(_tableName: string, _index: IndexSchema): string[];
23
31
  /** The keyword an index type replaces `INDEX` with, or `INDEX` for the types that do not. */
@@ -46,15 +46,31 @@ export class IndexDdl {
46
46
  this.capabilities = INDEX_CAPABILITIES[dialect.dialectName];
47
47
  }
48
48
  getCreateIndexStatement(tableName, index, opts = {}) {
49
- const { types, features, hints } = this.capabilities;
50
- assertIndexType(index, types, this.dialect.dialectName, hints);
51
- assertIndexFeatures(index, features, this.dialect.dialectName);
49
+ this.assertExpressible(index);
52
50
  const unique = index.unique ? 'UNIQUE ' : '';
53
51
  const ifNotExists = (opts.ifNotExists ?? this.dialect.features.indexIfNotExists) ? 'IF NOT EXISTS ' : '';
54
- const columns = this.indexTarget(index);
55
52
  return (`CREATE ${unique}${this.indexKeyword(index)} ${ifNotExists}${this.dialect.escapeId(index.name)} ` +
56
- `ON ${this.dialect.escapeId(tableName)}${this.indexAccessMethod(index)} (${columns})` +
57
- `${this.indexInclude(index)}${this.indexTuning(index)}${this.indexPredicate(index)};`);
53
+ `ON ${this.dialect.escapeId(tableName)}${this.indexAccessMethod(index)} ${this.indexBody(index)};`);
54
+ }
55
+ /**
56
+ * A plain index as a definition of its `CREATE TABLE`, where the engine takes one; `undefined` for its own
57
+ * `CREATE INDEX`. Never a unique one: CockroachDB makes that a constraint, which `DROP INDEX` refuses.
58
+ */
59
+ inlineDefinition(index) {
60
+ if (!this.capabilities.inline || index.type || index.unique) {
61
+ return undefined;
62
+ }
63
+ this.assertExpressible(index);
64
+ return `INDEX ${this.dialect.escapeId(index.name)} ${this.indexBody(index)}`;
65
+ }
66
+ assertExpressible(index) {
67
+ const { types, features, hints } = this.capabilities;
68
+ assertIndexType(index, types, this.dialect.dialectName, hints);
69
+ assertIndexFeatures(index, features, this.dialect.dialectName);
70
+ }
71
+ /** What follows the name and access method, in either form: the columns, then what qualifies them. */
72
+ indexBody(index) {
73
+ return `(${this.indexTarget(index)})${this.indexInclude(index)}${this.indexTuning(index)}${this.indexPredicate(index)}`;
58
74
  }
59
75
  /** What an index added to a table that has rows needs run after it to serve queries; nothing, mostly. */
60
76
  settleStatements(_tableName, _index) {
@@ -14,7 +14,7 @@ export declare function bareColumn({ index: _index, foreignKey: _foreignKey, enu
14
14
  /**
15
15
  * The index a column-level `index` or `unique` declares, or nothing: a unique column is a unique index.
16
16
  *
17
- * Shared with `TableBuilder.build`, which lifts these into the table it is creating: written twice,
17
+ * Shared with `TableDefinitionBuilder.build`, which lifts these into the table it is creating: written twice,
18
18
  * `addColumn` had no lift at all and silently emitted a column with no index.
19
19
  */
20
20
  export declare function columnIndex(tableName: string, col: FullColumnDefinition): Pick<IndexSchema, 'name' | 'entries' | 'unique'> | undefined;
@@ -35,7 +35,7 @@ export function bareColumn({ index: _index, foreignKey: _foreignKey, enum: _enum
35
35
  /**
36
36
  * The index a column-level `index` or `unique` declares, or nothing: a unique column is a unique index.
37
37
  *
38
- * Shared with `TableBuilder.build`, which lifts these into the table it is creating: written twice,
38
+ * Shared with `TableDefinitionBuilder.build`, which lifts these into the table it is creating: written twice,
39
39
  * `addColumn` had no lift at all and silently emitted a column with no index.
40
40
  */
41
41
  export function columnIndex(tableName, col) {
@@ -1,4 +1,4 @@
1
- export type { Change, ColumnSchema, DialectName, ForeignKeySchema, IndexSchema, Migration, MigrationDefinition, MigrationResult, MigrationStorage, MigratorOptions, PrimaryKeySchema, SchemaDiff, SchemaGenerator, SchemaIntrospector, SqlDialectName, SqlQuerier, SqlQueryDialect, SyncOptions, TableSchema, } from '../type/index.js';
1
+ export type { Change, ColumnSchema, DialectName, ForeignKeySchema, IndexSchema, Migration, MigrationDefinition, MigrationResult, MigratorOptions, PrimaryKeySchema, SchemaDiff, SchemaGenerator, SchemaIntrospector, SqlDialectName, SqlQuerier, SqlQueryDialect, SyncOptions, TableSchema, } from '../type/index.js';
2
2
  export { type Config, isSqlQuerier } from '../type/index.js';
3
3
  export { acquireQuerierForMigrations } from './acquireQuerierForMigrations.js';
4
4
  export { assertCliConfig } from './assertCliConfig.js';
@@ -12,5 +12,3 @@ export { migrationBuilderFor } from './migrationTarget.js';
12
12
  export { type BuilderMigrationDefinition, defineBuilderMigration, defineMigration, Migrator } from './migrator.js';
13
13
  export { reverseDiff } from './schemaChange.js';
14
14
  export { SqlSchemaGenerator } from './schemaGenerator.js';
15
- export { DatabaseMigrationStorage } from './storage/databaseStorage.js';
16
- export { JsonMigrationStorage } from './storage/jsonStorage.js';
@@ -18,6 +18,3 @@ export { defineBuilderMigration, defineMigration, Migrator } from './migrator.js
18
18
  // Schema generators
19
19
  export { reverseDiff } from './schemaChange.js';
20
20
  export { SqlSchemaGenerator } from './schemaGenerator.js';
21
- // Storage implementations
22
- export { DatabaseMigrationStorage } from './storage/databaseStorage.js';
23
- export { JsonMigrationStorage } from './storage/jsonStorage.js';
@@ -4,12 +4,12 @@ import type { SchemaAST } from '../../schema/schemaAST.js';
4
4
  import { SqlExpression } from '../../schema/sqlExpression.js';
5
5
  import type { CheckSchema, TriggerSchema } from '../../schema/types.js';
6
6
  import { type ForeignKeyAction } from '../../schema/types.js';
7
- import type { ColumnRenames, ColumnSchema, Except, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
7
+ import type { ColumnRenames, ColumnSchema, Except, ForeignKeySchema, IndexSchema, PrimaryKeySchema, QuerierPool, QueryRaw, RawRow, SchemaIntrospector, SqlQuerier, StoredDefinition, TableSchema } from '../../type/index.js';
8
8
  /**
9
9
  * Reads the rows of one statement while introspecting a table, an identical statement sent once: SQLite's
10
10
  * `table_info` is both the column list and the key, and on D1 and Turso every PRAGMA is a round trip.
11
11
  */
12
- export type TableRowReader = <T extends RawRow>(sql: string, params?: unknown[]) => Promise<T[]>;
12
+ export type TableRowReader = <T extends RawRow>(sql: QueryRaw) => Promise<T[]>;
13
13
  /** A column as its engine's catalogue reads it; the key and the indexes say which it belongs to. */
14
14
  export type ReadColumn = Except<ColumnSchema, 'isPrimaryKey' | 'isUnique'>;
15
15
  /** A foreign key as MySQL and SQL Server list one: a row, its column lists comma-joined. */
@@ -34,17 +34,13 @@ export declare abstract class AbstractSqlSchemaIntrospector implements SchemaInt
34
34
  * `undefined` for "wherever the connection points", and name a schema only when one was asked for.
35
35
  */
36
36
  constructor(pool: QuerierPool, schema?: string | undefined);
37
- /**
38
- * The schema every catalogue query filters on, as SQL: the one that was asked for, as the engine's
39
- * own literal, or its expression for the connection's default. A literal rather than a bind
40
- * parameter because these queries are assembled as text and several use it more than once.
41
- */
42
- protected get schemaExpr(): string;
37
+ /** The schema every catalogue query filters on: the one that was asked for, bound, or the connection's default. */
38
+ protected get schemaExpr(): QueryRaw;
43
39
  /**
44
40
  * How this engine names the connection's current schema (Postgres) or database (MySQL). Empty on
45
41
  * an engine with no schemas, whose catalogue queries never reference one.
46
42
  */
47
- protected readonly defaultSchemaExpr: string;
43
+ protected readonly defaultSchemaExpr: QueryRaw;
48
44
  /**
49
45
  * The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
50
46
  * rather than raised: the point of naming them is to read a database other things are still
@@ -59,9 +55,9 @@ export declare abstract class AbstractSqlSchemaIntrospector implements SchemaInt
59
55
  /** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
60
56
  protected tableExistsInternal(read: TableRowReader, tableName: string): Promise<boolean>;
61
57
  /** SQL listing the base tables' names, as `table_name`. */
62
- protected abstract getTableNamesQuery(): string;
63
- /** SQL answering a row where the base table its one parameter names exists. */
64
- protected abstract tableExistsQuery(): string;
58
+ protected abstract getTableNamesQuery(): QueryRaw;
59
+ /** SQL answering a row where the base table named exists. */
60
+ protected abstract tableExistsQuery(tableName: string): QueryRaw;
65
61
  /** The table's columns, in their order. */
66
62
  protected abstract getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
67
63
  /** The table's indexes, but its key's own. */
@@ -77,18 +73,18 @@ export declare abstract class AbstractSqlSchemaIntrospector implements SchemaInt
77
73
  */
78
74
  protected getTriggers(read: TableRowReader, tableName: string): Promise<TriggerSchema[]>;
79
75
  /**
80
- * SQL listing the triggers on the table named by its single parameter: each one's `name`, its `definition`
76
+ * SQL listing the triggers on the table named: each one's `name`, its `definition`
81
77
  * as the engine reprints it, and what it `requires` to be recreated first. It reads what is installed, not
82
78
  * what uql wrote, which is exactly what a rollback restores.
83
79
  */
84
- protected abstract triggersQuery(): string;
80
+ protected abstract triggersQuery(tableName: string): QueryRaw;
85
81
  /** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
86
82
  protected getDefinition(_read: TableRowReader, _tableName: string): Promise<StoredDefinition[] | undefined>;
87
83
  /**
88
84
  * The key `sql` lists a row of for each column, in key order: its `column_name`, and the `constraint_name`
89
85
  * where the engine names the key's constraint. Only a `DROP` needs that name, and only the reported one will do.
90
86
  */
91
- protected readPrimaryKey(read: TableRowReader, sql: string, tableName: string): Promise<PrimaryKeySchema | undefined>;
87
+ protected readPrimaryKey(read: TableRowReader, sql: QueryRaw): Promise<PrimaryKeySchema | undefined>;
92
88
  /** The {@link ForeignKeyAction} a catalogue names, whatever its case, and `SET_NULL` as SQL Server spells it. */
93
89
  protected normalizeReferentialAction(action: string): ForeignKeyAction | undefined;
94
90
  /** Foreign keys read one row each, as {@link JoinedForeignKeyRow} lists them. */
@@ -1,5 +1,6 @@
1
1
  import { SqlExpression } from '../../schema/sqlExpression.js';
2
2
  import { FOREIGN_KEY_ACTIONS } from '../../schema/types.js';
3
+ import { raw } from '../../util/raw.js';
3
4
  import { isOwnedName } from '../../util/sql.util.js';
4
5
  import { withSqlQuerierForMigrations } from '../acquireQuerierForMigrations.js';
5
6
  import { knownDefault } from '../ddl/defaultSql.js';
@@ -21,19 +22,15 @@ export class AbstractSqlSchemaIntrospector {
21
22
  this.schema = schema;
22
23
  this.dialect = pool.dialect;
23
24
  }
24
- /**
25
- * The schema every catalogue query filters on, as SQL: the one that was asked for, as the engine's
26
- * own literal, or its expression for the connection's default. A literal rather than a bind
27
- * parameter because these queries are assembled as text and several use it more than once.
28
- */
25
+ /** The schema every catalogue query filters on: the one that was asked for, bound, or the connection's default. */
29
26
  get schemaExpr() {
30
- return this.schema === undefined ? this.defaultSchemaExpr : this.dialect.escape(this.schema);
27
+ return this.schema === undefined ? this.defaultSchemaExpr : raw `${this.schema}`;
31
28
  }
32
29
  /**
33
30
  * How this engine names the connection's current schema (Postgres) or database (MySQL). Empty on
34
31
  * an engine with no schemas, whose catalogue queries never reference one.
35
32
  */
36
- defaultSchemaExpr = '';
33
+ defaultSchemaExpr = raw ``;
37
34
  /**
38
35
  * The database as a {@link SchemaAST}, or just the tables named. A name nothing matches is left out
39
36
  * rather than raised: the point of naming them is to read a database other things are still
@@ -101,14 +98,14 @@ export class AbstractSqlSchemaIntrospector {
101
98
  }
102
99
  /** Whether the base table exists: its query answers a row for it, none for a view or nothing. */
103
100
  async tableExistsInternal(read, tableName) {
104
- return (await read(this.tableExistsQuery(), [tableName])).length > 0;
101
+ return (await read(this.tableExistsQuery(tableName))).length > 0;
105
102
  }
106
103
  /**
107
104
  * The triggers uql installed on the table, each with the statements recreating it as the engine keeps it.
108
105
  * Ownership is matched here rather than with `LIKE`, whose `_` wildcard would take in a hand-written one.
109
106
  */
110
107
  async getTriggers(read, tableName) {
111
- const rows = await read(this.triggersQuery(), [tableName]);
108
+ const rows = await read(this.triggersQuery(tableName));
112
109
  return rows.flatMap(({ name, requires, definition }) => isOwnedName(name) ? [{ name, statements: [requires, definition].filter((sql) => typeof sql === 'string') }] : []);
113
110
  }
114
111
  /** See {@link TableSchema.definition}: none, but where the engine keeps the statements themselves. */
@@ -119,8 +116,8 @@ export class AbstractSqlSchemaIntrospector {
119
116
  * The key `sql` lists a row of for each column, in key order: its `column_name`, and the `constraint_name`
120
117
  * where the engine names the key's constraint. Only a `DROP` needs that name, and only the reported one will do.
121
118
  */
122
- async readPrimaryKey(read, sql, tableName) {
123
- const rows = await read(sql, [tableName]);
119
+ async readPrimaryKey(read, sql) {
120
+ const rows = await read(sql);
124
121
  return rows.length
125
122
  ? { columns: rows.map((row) => row.column_name), name: rows[0].constraint_name ?? undefined }
126
123
  : undefined;
@@ -162,12 +159,13 @@ function uniqueColumns(indexes) {
162
159
  /** A {@link TableRowReader} over one querier: the same statement is only ever sent once. */
163
160
  function createTableRowReader(querier) {
164
161
  const sent = new Map();
165
- return (sql, params) => {
166
- const key = params?.length ? `${sql}\u0000${JSON.stringify(params)}` : sql;
162
+ return (sql) => {
163
+ const ctx = querier.dialect.createContext();
164
+ querier.dialect.getRawValue(ctx, { value: sql });
165
+ const key = `${ctx.sql}\u0000${JSON.stringify(ctx.values)}`;
167
166
  let rows = sent.get(key);
168
167
  if (!rows) {
169
- // PRAGMA statements take no parameters at all, so they are sent as a bare statement.
170
- rows = params?.length ? querier.all(sql, params) : querier.all(sql);
168
+ rows = querier.all(sql);
171
169
  sent.set(key, rows);
172
170
  }
173
171
  return rows;
@@ -1,13 +1,13 @@
1
1
  import type { CheckSchema } from '../../schema/types.js';
2
- import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema } from '../../type/index.js';
2
+ import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, QueryRaw } from '../../type/index.js';
3
3
  import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
4
4
  /** SQL Server schema introspector: `INFORMATION_SCHEMA` has no view of indexes, which come from `sys.indexes`. */
5
5
  export declare class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
6
- protected readonly defaultSchemaExpr = "SCHEMA_NAME()";
7
- protected triggersQuery(): string;
6
+ protected readonly defaultSchemaExpr: QueryRaw;
7
+ protected triggersQuery(tableName: string): QueryRaw;
8
8
  protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
9
- protected getTableNamesQuery(): string;
10
- protected tableExistsQuery(): string;
9
+ protected getTableNamesQuery(): QueryRaw;
10
+ protected tableExistsQuery(tableName: string): QueryRaw;
11
11
  /** From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag. */
12
12
  protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
13
13
  /** `is_primary_key` is excluded: the key is read separately, the way every other engine reads it. */
@@ -1,26 +1,26 @@
1
+ import { raw } from '../../util/raw.js';
1
2
  import { AbstractSqlSchemaIntrospector, } from './abstractSqlSchemaIntrospector.js';
2
3
  /** SQL Server schema introspector: `INFORMATION_SCHEMA` has no view of indexes, which come from `sys.indexes`. */
3
4
  export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
4
- defaultSchemaExpr = 'SCHEMA_NAME()';
5
- triggersQuery() {
6
- return /*sql*/ `
5
+ defaultSchemaExpr = raw `SCHEMA_NAME()`;
6
+ triggersQuery(tableName) {
7
+ return raw `
7
8
  SELECT t.name AS name, m.definition AS definition
8
9
  FROM sys.triggers t
9
10
  JOIN sys.sql_modules m ON m.object_id = t.object_id
10
11
  WHERE t.parent_id <> 0 AND OBJECT_SCHEMA_NAME(t.parent_id) = ${this.schemaExpr}
11
- AND OBJECT_NAME(t.parent_id) = ${this.dialect.placeholder(1)}
12
+ AND OBJECT_NAME(t.parent_id) = ${tableName}
12
13
  `;
13
14
  }
14
15
  async getChecks(read, tableName) {
15
- return read(
16
- /*sql*/ `
16
+ return read(raw `
17
17
  SELECT k.name AS name, k.definition AS expression
18
18
  FROM sys.check_constraints k
19
- WHERE OBJECT_SCHEMA_NAME(k.parent_object_id) = ${this.schemaExpr} AND OBJECT_NAME(k.parent_object_id) = @p1
20
- `, [tableName]);
19
+ WHERE OBJECT_SCHEMA_NAME(k.parent_object_id) = ${this.schemaExpr} AND OBJECT_NAME(k.parent_object_id) = ${tableName}
20
+ `);
21
21
  }
22
22
  getTableNamesQuery() {
23
- return /*sql*/ `
23
+ return raw `
24
24
  SELECT TABLE_NAME as table_name
25
25
  FROM INFORMATION_SCHEMA.TABLES
26
26
  WHERE TABLE_SCHEMA = ${this.schemaExpr}
@@ -28,16 +28,15 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
28
28
  ORDER BY TABLE_NAME
29
29
  `;
30
30
  }
31
- tableExistsQuery() {
32
- return /*sql*/ `
31
+ tableExistsQuery(tableName) {
32
+ return raw `
33
33
  SELECT 1 FROM INFORMATION_SCHEMA.TABLES
34
- WHERE TABLE_SCHEMA = ${this.schemaExpr} AND TABLE_NAME = @p1 AND TABLE_TYPE = 'BASE TABLE'
34
+ WHERE TABLE_SCHEMA = ${this.schemaExpr} AND TABLE_NAME = ${tableName} AND TABLE_TYPE = 'BASE TABLE'
35
35
  `;
36
36
  }
37
37
  /** From `sys` rather than `INFORMATION_SCHEMA`, which has no identity flag. */
38
38
  async getColumns(read, tableName) {
39
- const rows = await read(
40
- /*sql*/ `
39
+ const rows = await read(raw `
41
40
  SELECT
42
41
  c.name as column_name,
43
42
  t.name as data_type,
@@ -54,9 +53,9 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
54
53
  JOIN sys.types t ON t.user_type_id = c.user_type_id
55
54
  LEFT JOIN sys.default_constraints d ON d.object_id = c.default_object_id
56
55
  LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
57
- WHERE s.name = ${this.schemaExpr} AND o.name = @p1
56
+ WHERE s.name = ${this.schemaExpr} AND o.name = ${tableName}
58
57
  ORDER BY c.column_id
59
- `, [tableName]);
58
+ `);
60
59
  return rows.map((row) => {
61
60
  const type = row.data_type.toUpperCase();
62
61
  const bytes = this.toNumber(row.max_length);
@@ -75,8 +74,7 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
75
74
  }
76
75
  /** `is_primary_key` is excluded: the key is read separately, the way every other engine reads it. */
77
76
  async getIndexes(read, tableName) {
78
- const rows = await read(
79
- /*sql*/ `
77
+ const rows = await read(raw `
80
78
  SELECT
81
79
  i.name as index_name,
82
80
  i.is_unique as is_unique,
@@ -87,11 +85,11 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
87
85
  JOIN sys.schemas s ON s.schema_id = o.schema_id
88
86
  JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
89
87
  JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
90
- WHERE s.name = ${this.schemaExpr} AND o.name = @p1
88
+ WHERE s.name = ${this.schemaExpr} AND o.name = ${tableName}
91
89
  AND i.is_primary_key = 0 AND i.name IS NOT NULL AND ic.is_included_column = 0
92
90
  GROUP BY i.name, i.is_unique, i.filter_definition
93
91
  ORDER BY i.name
94
- `, [tableName]);
92
+ `);
95
93
  return rows.map((row) => ({
96
94
  name: row.index_name,
97
95
  entries: row.columns.split(',').map((column) => ({ column })),
@@ -100,8 +98,7 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
100
98
  }));
101
99
  }
102
100
  async getForeignKeys(read, tableName) {
103
- const rows = await read(
104
- /*sql*/ `
101
+ const rows = await read(raw `
105
102
  SELECT
106
103
  fk.name as constraint_name,
107
104
  STRING_AGG(pc.name, ',') WITHIN GROUP (ORDER BY fkc.constraint_column_id) as columns,
@@ -116,24 +113,23 @@ export class MsSqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
116
113
  JOIN sys.columns pc ON pc.object_id = fkc.parent_object_id AND pc.column_id = fkc.parent_column_id
117
114
  JOIN sys.objects rt ON rt.object_id = fk.referenced_object_id
118
115
  JOIN sys.columns rc ON rc.object_id = fkc.referenced_object_id AND rc.column_id = fkc.referenced_column_id
119
- WHERE s.name = ${this.schemaExpr} AND o.name = @p1
116
+ WHERE s.name = ${this.schemaExpr} AND o.name = ${tableName}
120
117
  GROUP BY fk.name, rt.name, fk.delete_referential_action_desc, fk.update_referential_action_desc
121
118
  ORDER BY fk.name
122
- `, [tableName]);
119
+ `);
123
120
  return this.joinedForeignKeys(rows);
124
121
  }
125
122
  getPrimaryKey(read, tableName) {
126
- return this.readPrimaryKey(read,
127
- /*sql*/ `
123
+ return this.readPrimaryKey(read, raw `
128
124
  SELECT c.name as column_name, i.name as constraint_name
129
125
  FROM sys.indexes i
130
126
  JOIN sys.objects o ON o.object_id = i.object_id
131
127
  JOIN sys.schemas s ON s.schema_id = o.schema_id
132
128
  JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
133
129
  JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
134
- WHERE s.name = ${this.schemaExpr} AND o.name = @p1 AND i.is_primary_key = 1
130
+ WHERE s.name = ${this.schemaExpr} AND o.name = ${tableName} AND i.is_primary_key = 1
135
131
  ORDER BY ic.key_ordinal
136
- `, tableName);
132
+ `);
137
133
  }
138
134
  /**
139
135
  * The engine reprints a default from its own parse tree: all of it in one pair of parentheses, and a
@@ -1,21 +1,21 @@
1
1
  import type { IndexFacet } from '../../schema/indexDifferences.js';
2
2
  import type { CheckSchema } from '../../schema/types.js';
3
- import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema } from '../../type/index.js';
3
+ import type { ForeignKeySchema, IndexSchema, PrimaryKeySchema, QueryRaw } from '../../type/index.js';
4
4
  import { AbstractSqlSchemaIntrospector, type ReadColumn, type TableRowReader } from './abstractSqlSchemaIntrospector.js';
5
5
  /**
6
6
  * MySQL/MariaDB schema introspector.
7
7
  * Works with both MySQL and MariaDB as they share the same information_schema structure.
8
8
  */
9
9
  export declare class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
10
- protected readonly defaultSchemaExpr = "DATABASE()";
11
- protected triggersQuery(): string;
10
+ protected readonly defaultSchemaExpr: QueryRaw;
11
+ protected triggersQuery(tableName: string): QueryRaw;
12
12
  /**
13
13
  * `CHECK_CONSTRAINTS` names no table on MySQL, where a check's name is unique in its schema, so the
14
14
  * table comes from `TABLE_CONSTRAINTS`. It reports the clause escaped as a literal's body (`\'a\'`).
15
15
  */
16
16
  protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
17
- protected getTableNamesQuery(): string;
18
- protected tableExistsQuery(): string;
17
+ protected getTableNamesQuery(): QueryRaw;
18
+ protected tableExistsQuery(tableName: string): QueryRaw;
19
19
  protected getColumns(read: TableRowReader, tableName: string): Promise<ReadColumn[]>;
20
20
  protected getIndexes(read: TableRowReader, tableName: string): Promise<IndexSchema[]>;
21
21
  protected getForeignKeys(read: TableRowReader, tableName: string): Promise<ForeignKeySchema[]>;