@warlock.js/cascade 5.24.0 → 5.26.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 (93) hide show
  1. package/CHANGELOG.md +30 -5
  2. package/README.md +101 -101
  3. package/bin/cascade.js +0 -0
  4. package/cjs/index.cjs +307 -88
  5. package/cjs/index.cjs.map +1 -1
  6. package/esm/contracts/database-driver.contract.d.mts +22 -1
  7. package/esm/contracts/database-driver.contract.d.mts.map +1 -1
  8. package/esm/contracts/index.d.mts +1 -1
  9. package/esm/contracts/query-builder.contract.d.mts +7 -0
  10. package/esm/contracts/query-builder.contract.d.mts.map +1 -1
  11. package/esm/data-source/data-source-registry.d.mts +8 -6
  12. package/esm/data-source/data-source-registry.d.mts.map +1 -1
  13. package/esm/data-source/data-source-registry.mjs +7 -1
  14. package/esm/data-source/data-source-registry.mjs.map +1 -1
  15. package/esm/drivers/mongodb/mongodb-driver.d.mts +13 -0
  16. package/esm/drivers/mongodb/mongodb-driver.d.mts.map +1 -1
  17. package/esm/drivers/mongodb/mongodb-driver.mjs +71 -2
  18. package/esm/drivers/mongodb/mongodb-driver.mjs.map +1 -1
  19. package/esm/drivers/mongodb/mongodb-query-builder.d.mts +5 -0
  20. package/esm/drivers/mongodb/mongodb-query-builder.d.mts.map +1 -1
  21. package/esm/drivers/mongodb/mongodb-query-builder.mjs +11 -1
  22. package/esm/drivers/mongodb/mongodb-query-builder.mjs.map +1 -1
  23. package/esm/drivers/postgres/postgres-driver.d.mts +9 -0
  24. package/esm/drivers/postgres/postgres-driver.d.mts.map +1 -1
  25. package/esm/drivers/postgres/postgres-driver.mjs +55 -7
  26. package/esm/drivers/postgres/postgres-driver.mjs.map +1 -1
  27. package/esm/drivers/postgres/postgres-query-builder.d.mts +5 -0
  28. package/esm/drivers/postgres/postgres-query-builder.d.mts.map +1 -1
  29. package/esm/drivers/postgres/postgres-query-builder.mjs +15 -0
  30. package/esm/drivers/postgres/postgres-query-builder.mjs.map +1 -1
  31. package/esm/drivers/postgres/postgres-query-parser.d.mts +1 -4
  32. package/esm/drivers/postgres/postgres-query-parser.d.mts.map +1 -1
  33. package/esm/drivers/postgres/postgres-query-parser.mjs +4 -5
  34. package/esm/drivers/postgres/postgres-query-parser.mjs.map +1 -1
  35. package/esm/errors/unsafe-filter.error.mjs.map +1 -1
  36. package/esm/errors/unsafe-raw-expression.error.mjs.map +1 -1
  37. package/esm/errors/unsupported-lock-for-update.error.d.mts +11 -0
  38. package/esm/errors/unsupported-lock-for-update.error.d.mts.map +1 -0
  39. package/esm/errors/unsupported-lock-for-update.error.mjs +16 -0
  40. package/esm/errors/unsupported-lock-for-update.error.mjs.map +1 -0
  41. package/esm/events/model-events.mjs.map +1 -1
  42. package/esm/index.d.mts +4 -3
  43. package/esm/index.mjs +2 -1
  44. package/esm/migration/index.d.mts +1 -1
  45. package/esm/migration/migration-order.mjs +6 -3
  46. package/esm/migration/migration-order.mjs.map +1 -1
  47. package/esm/migration/migration-runner.d.mts +5 -3
  48. package/esm/migration/migration-runner.d.mts.map +1 -1
  49. package/esm/migration/migration-runner.mjs +3 -2
  50. package/esm/migration/migration-runner.mjs.map +1 -1
  51. package/esm/migration/migration.d.mts +11 -1
  52. package/esm/migration/migration.d.mts.map +1 -1
  53. package/esm/migration/migration.mjs +45 -2
  54. package/esm/migration/migration.mjs.map +1 -1
  55. package/esm/migration/sql-grammar.mjs.map +1 -1
  56. package/esm/model/generate-model-id.mjs +23 -0
  57. package/esm/model/generate-model-id.mjs.map +1 -0
  58. package/esm/model/methods/delete-methods.mjs.map +1 -1
  59. package/esm/model/methods/meta-methods.mjs.map +1 -1
  60. package/esm/model/methods/query-methods.mjs +38 -11
  61. package/esm/model/methods/query-methods.mjs.map +1 -1
  62. package/esm/model/methods/write-methods.mjs.map +1 -1
  63. package/esm/model/model.d.mts +2 -2
  64. package/esm/model/model.d.mts.map +1 -1
  65. package/esm/model/model.mjs +1 -1
  66. package/esm/model/model.mjs.map +1 -1
  67. package/esm/query-builder/query-builder.d.mts +5 -0
  68. package/esm/query-builder/query-builder.d.mts.map +1 -1
  69. package/esm/query-builder/query-builder.mjs +16 -1
  70. package/esm/query-builder/query-builder.mjs.map +1 -1
  71. package/esm/utils/sanitize-filter.mjs.map +1 -1
  72. package/esm/validation/plugins/database-rules-plugin.mjs.map +1 -1
  73. package/esm/writer/database-writer.d.mts +0 -33
  74. package/esm/writer/database-writer.d.mts.map +1 -1
  75. package/esm/writer/database-writer.mjs +3 -52
  76. package/esm/writer/database-writer.mjs.map +1 -1
  77. package/llms-full.txt +2911 -2864
  78. package/package.json +5 -5
  79. package/skills/aggregate-data/SKILL.md +165 -165
  80. package/skills/alter-migration/SKILL.md +531 -531
  81. package/skills/configure-delete-strategy/SKILL.md +130 -130
  82. package/skills/define-model/SKILL.md +205 -205
  83. package/skills/define-relations/SKILL.md +171 -171
  84. package/skills/manage-data-sources/SKILL.md +177 -140
  85. package/skills/manage-transactions/SKILL.md +215 -205
  86. package/skills/paginate-results/SKILL.md +122 -122
  87. package/skills/perform-atomic-ops/SKILL.md +156 -156
  88. package/skills/query-data/SKILL.md +213 -213
  89. package/skills/run-cascade-cli/SKILL.md +125 -125
  90. package/skills/search-by-vector/SKILL.md +127 -127
  91. package/skills/subscribe-to-model-events/SKILL.md +148 -148
  92. package/skills/track-changes/SKILL.md +127 -127
  93. package/skills/write-migration/SKILL.md +206 -206
@@ -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