uql-orm 0.96.1 → 0.97.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/migrate/builder/types.d.ts +2 -0
  2. package/dist/migrate/codegen/entityCodeGenerator.js +2 -6
  3. package/dist/migrate/codegen/fieldOptionsSource.d.ts +1 -3
  4. package/dist/migrate/codegen/fieldOptionsSource.js +0 -5
  5. package/dist/migrate/drift/driftDetector.js +16 -0
  6. package/dist/migrate/generator/definitionToNode.d.ts +2 -2
  7. package/dist/migrate/generator/definitionToNode.js +4 -3
  8. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +7 -5
  9. package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +10 -11
  10. package/dist/migrate/introspection/baseSqlIntrospector.js +4 -4
  11. package/dist/migrate/introspection/mssqlIntrospector.d.ts +2 -0
  12. package/dist/migrate/introspection/mssqlIntrospector.js +8 -0
  13. package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -0
  14. package/dist/migrate/introspection/mysqlIntrospector.js +28 -0
  15. package/dist/migrate/introspection/postgresIntrospector.d.ts +3 -0
  16. package/dist/migrate/introspection/postgresIntrospector.js +12 -0
  17. package/dist/migrate/introspection/sqliteIntrospector.d.ts +5 -0
  18. package/dist/migrate/introspection/sqliteIntrospector.js +14 -0
  19. package/dist/migrate/migrator.d.ts +5 -23
  20. package/dist/migrate/migrator.js +26 -80
  21. package/dist/migrate/schemaChange.d.ts +8 -3
  22. package/dist/migrate/schemaChange.js +24 -7
  23. package/dist/migrate/schemaGenerator.d.ts +20 -19
  24. package/dist/migrate/schemaGenerator.js +87 -76
  25. package/dist/migrate/triggerSql.d.ts +6 -8
  26. package/dist/migrate/triggerSql.js +14 -16
  27. package/dist/mongo/mongoIntrospector.d.ts +1 -3
  28. package/dist/mongo/mongoIntrospector.js +0 -4
  29. package/dist/mongo/mongoSchemaGenerator.d.ts +0 -6
  30. package/dist/mongo/mongoSchemaGenerator.js +0 -5
  31. package/dist/schema/schemaAST.js +1 -0
  32. package/dist/schema/schemaASTBuilder.d.ts +3 -1
  33. package/dist/schema/schemaASTBuilder.js +6 -4
  34. package/dist/schema/schemaASTDiffer.js +20 -5
  35. package/dist/schema/types.d.ts +23 -6
  36. package/dist/type/migration.d.ts +10 -19
  37. package/dist/util/ddlExpression.util.d.ts +11 -0
  38. package/dist/util/ddlExpression.util.js +24 -1
  39. package/dist/util/sql.util.d.ts +4 -6
  40. package/dist/util/sql.util.js +2 -6
  41. package/package.json +1 -1
  42. package/skills/uql-orm/SKILL.md +1 -1
@@ -41,12 +41,22 @@ export type ForeignKeyAction = (typeof FOREIGN_KEY_ACTIONS)[number];
41
41
  * dialect's own literal rules, so a number stays bare where a string is quoted.
42
42
  */
43
43
  export type EnumValues = readonly (string | number)[];
44
- /** A `CHECK` as the schema holds it, compared by presence only: the database reprints its expression. */
44
+ /**
45
+ * A `CHECK`, compared by name alone: one uql installs is named by `ownedCheck`, whose hash of the
46
+ * expression makes an edited one a new name. The expression is the engine's reprint when read back.
47
+ */
45
48
  export interface CheckSchema {
46
- /** Absent when nothing named it, which the generator fills in with `derivedCheckName`. */
47
- readonly name?: string;
49
+ readonly name: string;
48
50
  readonly expression: string;
49
51
  }
52
+ /**
53
+ * A trigger, compared by name alone as a check is: its name ends in a hash of its SQL. The statements create
54
+ * it; read back, they are the engine's own, which is what a rollback restores.
55
+ */
56
+ export interface TriggerSchema {
57
+ readonly name: string;
58
+ readonly statements: readonly string[];
59
+ }
50
60
  /**
51
61
  * Default action for foreign key ON DELETE and ON UPDATE clauses.
52
62
  */
@@ -79,8 +89,6 @@ export interface ColumnNode {
79
89
  readonly isAutoIncrement: boolean;
80
90
  /** Whether this column has a unique constraint */
81
91
  readonly isUnique: boolean;
82
- /** The values the column accepts. See {@link EnumValues}. */
83
- readonly enum?: EnumValues;
84
92
  /** The SQL an engine-generated column is computed from, as `GENERATED ALWAYS AS (...) STORED`. */
85
93
  readonly generatedAs?: string;
86
94
  /** Column comment/description */
@@ -121,6 +129,8 @@ export interface TableNode {
121
129
  readonly indexFacets: ReadonlySet<IndexFacet>;
122
130
  /** `CHECK` constraints on this table. */
123
131
  readonly checks: CheckSchema[];
132
+ /** The triggers uql installs on this table. */
133
+ readonly triggers: TriggerSchema[];
124
134
  /** Optional table comment */
125
135
  readonly comment?: string;
126
136
  /** The statements the engine keeps for the table, where it keeps them; none on a table built from entities. */
@@ -204,13 +214,18 @@ export type RelationshipDiff = NodeChange<RelationshipNode> & {
204
214
  * `(a, b)` differs from `(b, a)`, while `Member_pkey` and `Member__userId_pk` over the same columns are equal.
205
215
  * `from` keeps the name the database reported, the only name a `DROP` can use.
206
216
  */
207
- export type PrimaryKeyDiff = Change<PrimaryKeySchema> & {
217
+ export type PrimaryKeyDiff = TableChange<PrimaryKeySchema>;
218
+ /** One object of a table changed, named by the table. */
219
+ export type TableChange<T> = Change<T> & {
208
220
  readonly table: string;
209
221
  };
210
222
  /** The differences within a table that exists on both sides. */
211
223
  export interface TableDiff {
212
224
  readonly columns: ColumnDiff[];
213
225
  readonly indexes: IndexDiff[];
226
+ /** A check or trigger only one side has. Never both: a changed one is a new name, one dropped and one added. */
227
+ readonly checks: TableChange<CheckSchema>[];
228
+ readonly triggers: TableChange<TriggerSchema>[];
214
229
  /** Set only when the two primary keys have different columns. */
215
230
  readonly primaryKey?: PrimaryKeyDiff;
216
231
  }
@@ -220,6 +235,8 @@ export interface SchemaDiffResult {
220
235
  readonly tables: NodeChange<TableNode>[];
221
236
  readonly columns: ColumnDiff[];
222
237
  readonly indexes: IndexDiff[];
238
+ readonly checks: TableChange<CheckSchema>[];
239
+ readonly triggers: TableChange<TriggerSchema>[];
223
240
  readonly primaryKeys: PrimaryKeyDiff[];
224
241
  readonly relationships: RelationshipDiff[];
225
242
  }
@@ -2,7 +2,7 @@ import type { AnyMigrationOperation } from '../migrate/builder/types.js';
2
2
  import type { IndexFacet } from '../schema/indexDifferences.js';
3
3
  import type { SchemaAST } from '../schema/schemaAST.js';
4
4
  import type { DiffOptions } from '../schema/schemaASTDiffer.js';
5
- import type { ColumnNode, ForeignKeyAction, IndexType, TableNode } from '../schema/types.js';
5
+ import type { CheckSchema, ColumnNode, ForeignKeyAction, IndexType, TableNode, TriggerSchema } from '../schema/types.js';
6
6
  import type { EntityMeta, EntityWhereMeta, Except, FieldOptions, IndexColumnSchema, IndexedVectorField, LoggingOptions, Querier, SqlQuerier, Type, VectorIndexOptions } from './index.js';
7
7
  /**
8
8
  * Defines a migration using a simple object literal. `Q` is `MongoQuerier` for a MongoDB migration.
@@ -118,6 +118,9 @@ export interface TableSchema {
118
118
  readonly primaryKey?: PrimaryKeySchema;
119
119
  readonly indexes?: IndexSchema[];
120
120
  readonly foreignKeys?: ForeignKeySchema[];
121
+ readonly checks?: CheckSchema[];
122
+ /** The triggers uql installed, each with the statements the engine would recreate it from. */
123
+ readonly triggers?: TriggerSchema[];
121
124
  /** The statements the engine keeps for the table, where it keeps them: SQLite's `sqlite_master`. */
122
125
  readonly definition?: readonly StoredDefinition[];
123
126
  }
@@ -225,6 +228,12 @@ export interface SchemaDiff {
225
228
  readonly columns?: readonly ColumnChange[];
226
229
  readonly indexes?: readonly Change<IndexSchema>[];
227
230
  readonly foreignKeys?: readonly Change<ForeignKeySchema>[];
231
+ readonly checks?: readonly Change<CheckSchema>[];
232
+ /**
233
+ * The triggers installed or declared, a kept one as both sides under one name: Postgres refuses to alter
234
+ * a column a trigger names, so a table whose columns change takes every trigger off and puts it back.
235
+ */
236
+ readonly triggers?: readonly Change<TriggerSchema>[];
228
237
  /** Columns renamed in place, `from` the database's name `to` the entity's, which the other changes already use. */
229
238
  readonly renamedColumns?: readonly Rename[];
230
239
  /**
@@ -260,8 +269,6 @@ export interface DropSchemaOptions {
260
269
  /** The schema as it stands, whose foreign keys are dropped before any table, since a cycle of them has no drop order. */
261
270
  readonly existing?: SchemaAST;
262
271
  }
263
- /** The triggers uql installed on one table, by name, each with the statements that recreate it as it stands. */
264
- export type InstalledTriggers = ReadonlyMap<string, readonly string[]>;
265
272
  /**
266
273
  * Interface for generating DDL statements from entity metadata
267
274
  */
@@ -278,15 +285,6 @@ export interface SchemaGenerator {
278
285
  generateDropSchema(entities: readonly Type<object>[], options?: DropSchemaOptions): string[];
279
286
  /** Generate DROP TABLE statement. */
280
287
  generateDropTable(tableName: string, options?: DropSchemaOptions): string;
281
- /**
282
- * What takes the triggers on `entity`'s table from `installed` - each by name, with the statements that
283
- * recreate it - to what it declares: nothing where the two agree, which is always on MongoDB.
284
- */
285
- generateTriggers(entity: Type<object>, installed?: InstalledTriggers): string[];
286
- /** The inverse of {@link generateTriggers} from the same `installed`: its triggers dropped, and the ones it dropped restored. */
287
- generateTriggersDown(entity: Type<object>, installed?: InstalledTriggers): string[];
288
- /** A `DROP` for each trigger uql owns among `names` on `entity`'s table, whatever the entity declares. */
289
- generateTriggerDrops(entity: Type<object>, names: readonly string[]): string[];
290
288
  /** The statements taking a table through `diff`; its rollback is the diff reversed, see `reverseDiff`. */
291
289
  generateAlterTable(diff: SchemaDiff): string[];
292
290
  /**
@@ -343,13 +341,6 @@ export interface SchemaGenerator {
343
341
  * Interface for introspecting the current database schema
344
342
  */
345
343
  export interface SchemaIntrospector {
346
- /**
347
- * Every trigger uql installed on `table`, by name, each with the statements that recreate it as it
348
- * stands. The names say which to drop once an entity no longer declares them; the statements are what
349
- * a rollback puts back, read off the engine rather than recorded anywhere by uql. One table's alone,
350
- * so reading it never meets a trigger another writer is dropping from some other table.
351
- */
352
- ownedTriggers(table: string): Promise<InstalledTriggers>;
353
344
  /**
354
345
  * What this introspector can read back about an index, and so all that diffing may compare.
355
346
  * Comparing a feature it cannot read reports the same drift forever: the entity side declares it,
@@ -1,3 +1,4 @@
1
+ import type { CheckSchema, EnumValues } from '../schema/types.js';
1
2
  import { type EntityIndexColumn, type EntityIndexMeta, type EntityMeta, type IndexColumnInput, type IndexColumnSchema, QueryRaw } from '../type/index.js';
2
3
  /**
3
4
  * Reduces an authored index entry to the form metadata keeps, so a column, an expression and an options
@@ -16,3 +17,13 @@ export declare function renderIndexColumn(entry: EntityIndexColumn, render: (sql
16
17
  export declare function indexNameParts(entries: readonly EntityIndexColumn[]): string[];
17
18
  /** The name an index is created and read by: its own, else one derived from its table and its entries' columns. */
18
19
  export declare function declaredIndexName(name: string | undefined, table: string, entries: readonly EntityIndexColumn[]): string;
20
+ /**
21
+ * A check as uql installs it: named for its table and `label`, the author's name or `ck`, then a hash
22
+ * of `expression`, so an edited check is a new name and reordering the checks renames none.
23
+ */
24
+ export declare function ownedCheck(table: string, label: string | undefined, expression: string): CheckSchema;
25
+ /** The check a column's `enum` is, `column IN (...)`, each value the dialect's own literal; none without one. */
26
+ export declare function enumCheck(table: string, { name, enum: values }: {
27
+ readonly name: string;
28
+ readonly enum?: EnumValues;
29
+ }, render: (sql: QueryRaw) => string): CheckSchema[];
@@ -1,6 +1,7 @@
1
1
  import { ColumnRef, QueryRaw, } from '../type/index.js';
2
2
  import { definedEntries } from './object.util.js';
3
- import { derivedIndexName } from './sql.util.js';
3
+ import { raw } from './raw.js';
4
+ import { derivedIndexName, ownedName } from './sql.util.js';
4
5
  /**
5
6
  * Reduces an authored index entry to the form metadata keeps, so a column, an expression and an options
6
7
  * object reach the schema as one: a column read off the refs as its key, any other `raw` as it is.
@@ -39,3 +40,25 @@ export function indexNameParts(entries) {
39
40
  export function declaredIndexName(name, table, entries) {
40
41
  return name ?? derivedIndexName(table, indexNameParts(entries));
41
42
  }
43
+ /**
44
+ * A check as uql installs it: named for its table and `label`, the author's name or `ck`, then a hash
45
+ * of `expression`, so an edited check is a new name and reordering the checks renames none.
46
+ */
47
+ export function ownedCheck(table, label, expression) {
48
+ return { name: ownedName(table, label ?? 'ck', expression), expression };
49
+ }
50
+ /** The check a column's `enum` is, `column IN (...)`, each value the dialect's own literal; none without one. */
51
+ export function enumCheck(table, { name, enum: values }, render) {
52
+ if (!values) {
53
+ return [];
54
+ }
55
+ const sql = raw(({ ctx, dialect }) => {
56
+ ctx.append(`${dialect.escapeId(name)} IN (`);
57
+ values.forEach((value, i) => {
58
+ ctx.append(i ? ', ' : '');
59
+ ctx.addValue(value);
60
+ });
61
+ ctx.append(')');
62
+ });
63
+ return [ownedCheck(table, name, render(sql))];
64
+ }
@@ -22,13 +22,13 @@ export declare function splitQualifiedName(qualified: string): {
22
22
  * its own. It leaves out the table's schema, since it is a single identifier. The AST, the DDL and a `DROP`
23
23
  * all use this one rule.
24
24
  */
25
- export declare function derivedConstraintName(table: string, parts: readonly (string | number)[], kind: ConstraintKind): string;
25
+ export declare function derivedConstraintName(table: string, parts: readonly string[], kind: ConstraintKind): string;
26
26
  /** The kinds of derived name, which is also what `indexNameStem` strips to compare them. */
27
- export type ConstraintKind = 'pk' | 'fk' | 'idx' | 'ck' | 'uk';
27
+ export type ConstraintKind = 'pk' | 'fk' | 'idx' | 'uk';
28
28
  /**
29
29
  * Whether uql installed the object called `name`. Ownership is the prefix and nothing else, since no
30
- * engine records who created one - so this is the only thing standing between a hand-written trigger and
31
- * a `DROP`, and it is asked on both sides: when reading the catalogue, and again before emitting.
30
+ * engine records who created one - so this is the only thing standing between a hand-written trigger or
31
+ * check and a `DROP`, and it is asked on both sides: when reading the catalogue, and again before emitting.
32
32
  */
33
33
  export declare function isOwnedName(name: string): boolean;
34
34
  /**
@@ -49,8 +49,6 @@ export declare function derivedIndexName(table: string, columns: readonly string
49
49
  * same, so an existing constraint keeps whatever the engine called it - see `SchemaDiff.primaryKey`.
50
50
  */
51
51
  export declare function derivedPrimaryKeyName(table: string, columns: readonly string[]): string;
52
- /** The constraint name a check gets when nothing named it: `Order__1_ck`, by declaration order. */
53
- export declare function derivedCheckName(table: string, position: number): string;
54
52
  /** The constraint name a foreign key gets when nothing named it: `Order__customerId_fk`. */
55
53
  export declare function derivedForeignKeyName(table: string, columns: readonly string[]): string;
56
54
  /** Escapes an identifier with `escapeIdChar`, refusing a dotted one where `forbidQualified`, with a trailing dot where `addDot`. */
@@ -73,8 +73,8 @@ const TABLE_SEPARATOR = '__';
73
73
  const OWNED_START = `${OWNED_PREFIX}_`;
74
74
  /**
75
75
  * Whether uql installed the object called `name`. Ownership is the prefix and nothing else, since no
76
- * engine records who created one - so this is the only thing standing between a hand-written trigger and
77
- * a `DROP`, and it is asked on both sides: when reading the catalogue, and again before emitting.
76
+ * engine records who created one - so this is the only thing standing between a hand-written trigger or
77
+ * check and a `DROP`, and it is asked on both sides: when reading the catalogue, and again before emitting.
78
78
  */
79
79
  export function isOwnedName(name) {
80
80
  return name.startsWith(OWNED_START);
@@ -117,10 +117,6 @@ export function derivedIndexName(table, columns, unique = false) {
117
117
  export function derivedPrimaryKeyName(table, columns) {
118
118
  return derivedConstraintName(table, columns, 'pk');
119
119
  }
120
- /** The constraint name a check gets when nothing named it: `Order__1_ck`, by declaration order. */
121
- export function derivedCheckName(table, position) {
122
- return derivedConstraintName(table, [position], 'ck');
123
- }
124
120
  /** The constraint name a foreign key gets when nothing named it: `Order__customerId_fk`. */
125
121
  export function derivedForeignKeyName(table, columns) {
126
122
  return derivedConstraintName(table, columns, 'fk');
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.96.1",
6
+ "version": "0.97.0",
7
7
  "type": "module",
8
8
  "engines": {
9
9
  "node": ">=24"
@@ -143,7 +143,7 @@ A `findManyStream` holds its querier until the loop ends, which refuses any othe
143
143
  - `drift:check` fails when the database no longer matches.
144
144
  - `sync --dry-run` prints only SQL on stdout, to append to a migration file another tool applies: on Cloudflare D1, wrangler's (https://uql-orm.dev/cloudflare-d1.md).
145
145
 
146
- Triggers are part of the diff: uql owns the `_uql_`-prefixed ones and never touches another.
146
+ Checks, an enum's included, and triggers are part of the diff: uql owns the `_uql_`-prefixed ones and never touches another.
147
147
 
148
148
  ## Where to read more
149
149