@warlock.js/cascade 5.25.0 → 5.27.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 (37) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/cjs/index.cjs +106 -17
  3. package/cjs/index.cjs.map +1 -1
  4. package/esm/contracts/query-builder.contract.d.mts +7 -0
  5. package/esm/contracts/query-builder.contract.d.mts.map +1 -1
  6. package/esm/drivers/mongodb/mongodb-query-builder.d.mts +5 -0
  7. package/esm/drivers/mongodb/mongodb-query-builder.d.mts.map +1 -1
  8. package/esm/drivers/mongodb/mongodb-query-builder.mjs +9 -0
  9. package/esm/drivers/mongodb/mongodb-query-builder.mjs.map +1 -1
  10. package/esm/drivers/postgres/postgres-query-builder.d.mts +5 -0
  11. package/esm/drivers/postgres/postgres-query-builder.d.mts.map +1 -1
  12. package/esm/drivers/postgres/postgres-query-builder.mjs +15 -0
  13. package/esm/drivers/postgres/postgres-query-builder.mjs.map +1 -1
  14. package/esm/index.d.mts +2 -2
  15. package/esm/migration/index.d.mts +1 -1
  16. package/esm/migration/migration-order.mjs +6 -3
  17. package/esm/migration/migration-order.mjs.map +1 -1
  18. package/esm/migration/migration-runner.d.mts +5 -3
  19. package/esm/migration/migration-runner.d.mts.map +1 -1
  20. package/esm/migration/migration-runner.mjs +3 -2
  21. package/esm/migration/migration-runner.mjs.map +1 -1
  22. package/esm/migration/migration.d.mts +11 -1
  23. package/esm/migration/migration.d.mts.map +1 -1
  24. package/esm/migration/migration.mjs +45 -2
  25. package/esm/migration/migration.mjs.map +1 -1
  26. package/esm/model/methods/query-methods.mjs +13 -9
  27. package/esm/model/methods/query-methods.mjs.map +1 -1
  28. package/esm/model/model.d.mts +2 -2
  29. package/esm/model/model.d.mts.map +1 -1
  30. package/esm/model/model.mjs +1 -1
  31. package/esm/model/model.mjs.map +1 -1
  32. package/esm/query-builder/query-builder.d.mts +5 -0
  33. package/esm/query-builder/query-builder.d.mts.map +1 -1
  34. package/esm/query-builder/query-builder.mjs +15 -0
  35. package/esm/query-builder/query-builder.mjs.map +1 -1
  36. package/llms-full.txt +11 -1
  37. package/package.json +6 -6
@@ -1,5 +1,6 @@
1
1
  import { ColumnBuilder } from "./column-builder.mjs";
2
2
  import { ForeignKeyBuilder } from "./foreign-key-builder.mjs";
3
+ import { log } from "@warlock.js/logger";
3
4
 
4
5
  //#region ../cascade/src/migration/migration.ts
5
6
  /**
@@ -244,6 +245,7 @@ var Migration = class {
244
245
  * @internal
245
246
  */
246
247
  async execute() {
248
+ this.assertNoGeneratedIndexNameCollisions();
247
249
  for (const op of this.pendingOperations) await this.executeOperation(op);
248
250
  this.pendingOperations.length = 0;
249
251
  }
@@ -280,6 +282,7 @@ var Migration = class {
280
282
  * @internal
281
283
  */
282
284
  toSteps() {
285
+ this.assertNoGeneratedIndexNameCollisions();
283
286
  const serializer = this.driver.driver.getSQLSerializer();
284
287
  const steps = [];
285
288
  let schemaRun = [];
@@ -427,6 +430,40 @@ var Migration = class {
427
430
  }
428
431
  }
429
432
  /**
433
+ * Reject two unnamed indexes that a driver would give the same name before
434
+ * either DDL command is sent. MongoDB's default names include directions;
435
+ * PostgreSQL's include the table and columns.
436
+ */
437
+ assertNoGeneratedIndexNameCollisions() {
438
+ const engine = this.databaseEngine;
439
+ if (engine !== "mongodb" && engine !== "postgres") return;
440
+ const unnamedIndexes = this.pendingOperations.flatMap((operation) => {
441
+ if (operation.type === "createIndex") {
442
+ const index = operation.payload;
443
+ return index.name ? [] : [index];
444
+ }
445
+ if (operation.type === "createUniqueIndex") {
446
+ const { columns, name } = operation.payload;
447
+ return name ? [] : [{
448
+ columns,
449
+ unique: true
450
+ }];
451
+ }
452
+ return [];
453
+ });
454
+ const seen = /* @__PURE__ */ new Map();
455
+ for (const index of unnamedIndexes) {
456
+ const generatedName = engine === "mongodb" ? index.columns.map((column, i) => `${column}_${index.directions?.[i] === "desc" ? -1 : 1}`).join("_") : `idx_${this.table}_${index.columns.join("_")}`;
457
+ const first = seen.get(generatedName);
458
+ if (first) throw new Error(`Migration queued two ${engine} indexes with the generated name "${generatedName}": ${this.describeIndex(first)} and ${this.describeIndex(index)}. Give one index an explicit name, for example { name: "..." }.`);
459
+ seen.set(generatedName, index);
460
+ }
461
+ }
462
+ describeIndex(index) {
463
+ const partial = index.where ? JSON.stringify(index.where) : "none";
464
+ return `columns [${index.columns.join(", ")}] (unique: ${!!index.unique}, partial: ${partial})`;
465
+ }
466
+ /**
430
467
  * Check if a table exists.
431
468
  *
432
469
  * Useful for conditional migrations and idempotent operations.
@@ -2133,13 +2170,18 @@ Migration.create = function createMigration(model, columns, options = {}) {
2133
2170
  */
2134
2171
  Migration.alter = function alterMigration(model, schema, options = {}) {
2135
2172
  const { order = 0, createdAt, transactional } = options;
2136
- return class AlterMigration extends Migration {
2173
+ let missingDownWarningEmitted = false;
2174
+ class AlterMigration extends Migration {
2137
2175
  static order = order;
2138
2176
  static createdAt = createdAt;
2139
2177
  static transactional = transactional;
2140
2178
  table = model.table;
2141
2179
  dataSource = model.dataSource;
2142
2180
  async up() {
2181
+ if (options.up && !options.down && !missingDownWarningEmitted) {
2182
+ missingDownWarningEmitted = true;
2183
+ log.warn("database", "migration", `Migration "${AlterMigration.migrationName ?? this.table}" defines a custom up() without a down() rollback.`);
2184
+ }
2143
2185
  if (schema.add) wireColumns(this, schema.add);
2144
2186
  if (schema.drop) for (const col of schema.drop) this.dropColumn(col);
2145
2187
  if (schema.rename) for (const [from, to] of Object.entries(schema.rename)) this.renameColumn(from, to);
@@ -2192,7 +2234,8 @@ Migration.alter = function alterMigration(model, schema, options = {}) {
2192
2234
  async down() {
2193
2235
  if (options.down) await options.down.call(this);
2194
2236
  }
2195
- };
2237
+ }
2238
+ return AlterMigration;
2196
2239
  };
2197
2240
  Migration.__declarativeFactoriesAttached = true;
2198
2241