@guren/orm 2.13.0 → 2.14.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/index.d.ts CHANGED
@@ -279,11 +279,14 @@ declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult ex
279
279
  perPage?: number;
280
280
  }): Promise<PaginatedResult<TResult>>;
281
281
  /**
282
- * Bulk update. Mass-assignment protection and persistence preparation
283
- * (mutators, casts, `preparePersistencePayload`) apply exactly as in
284
- * `Model.update()`; per-record hooks and observers are skipped by design.
285
- */
286
- update(data: PlainObject): Promise<TRecord>;
282
+ * Bulk update. Mass-assignment protection and persistence preparation apply
283
+ * exactly as in `Model.update()`; per-record hooks and observers are skipped.
284
+ * `set` holds the columns the server chooses (RFC 0031); `data` is untyped
285
+ * here, so a key in both is refused at runtime only, by `filterFillable()`.
286
+ */
287
+ update(data: PlainObject, options?: {
288
+ set?: PlainObject;
289
+ }): Promise<TRecord>;
287
290
  /**
288
291
  * Bulk update bypassing mass-assignment protection. Trusted server-side data
289
292
  * only, never raw request input.
package/dist/index.js CHANGED
@@ -1554,12 +1554,13 @@ var QueryBuilder = class QueryBuilder {
1554
1554
  };
1555
1555
  }
1556
1556
  /**
1557
- * Bulk update. Mass-assignment protection and persistence preparation
1558
- * (mutators, casts, `preparePersistencePayload`) apply exactly as in
1559
- * `Model.update()`; per-record hooks and observers are skipped by design.
1557
+ * Bulk update. Mass-assignment protection and persistence preparation apply
1558
+ * exactly as in `Model.update()`; per-record hooks and observers are skipped.
1559
+ * `set` holds the columns the server chooses (RFC 0031); `data` is untyped
1560
+ * here, so a key in both is refused at runtime only, by `filterFillable()`.
1560
1561
  */
1561
- async update(data) {
1562
- return this.runBulkUpdate(data, true);
1562
+ async update(data, options) {
1563
+ return this.runBulkUpdate(data, true, options?.set);
1563
1564
  }
1564
1565
  /**
1565
1566
  * Bulk update bypassing mass-assignment protection. Trusted server-side data
@@ -1568,9 +1569,9 @@ var QueryBuilder = class QueryBuilder {
1568
1569
  async forceUpdate(data) {
1569
1570
  return this.runBulkUpdate(data, false);
1570
1571
  }
1571
- async runBulkUpdate(data, applyFillable) {
1572
+ async runBulkUpdate(data, applyFillable, set) {
1572
1573
  const model = this.modelClass;
1573
- const filtered = applyFillable ? model.filterFillable(data) : { ...data };
1574
+ const filtered = applyFillable ? model.filterFillable(data, set) : { ...data };
1574
1575
  const payload = await model.prepareBulkPersistencePayload(filtered);
1575
1576
  return this[PREPARED_UPDATE](payload);
1576
1577
  }
@@ -3610,6 +3611,22 @@ function assertNotConnectionUri(dbPath, source) {
3610
3611
  if (!CONNECTION_URI.test(dbPath)) return;
3611
3612
  throw new Error(`createSqliteDatabase() received a connection URI where it expects a file path: ${dbPath} (from ${source}). Left alone it would be created as a directory tree and migrated into silently. Pass a path such as "./data/guren.db", or ":memory:".`);
3612
3613
  }
3614
+ /**
3615
+ * Migrations run with enforcement off, so a rebuild's copy step or a data
3616
+ * migration can leave rows whose parent is gone. drizzle owns the COMMIT, so
3617
+ * this can only report them afterwards, as SQLite's own ALTER procedure would.
3618
+ */
3619
+ function reportForeignKeyViolations(handle) {
3620
+ const rows = handle.all(sql.raw("PRAGMA foreign_key_check;"));
3621
+ if (rows.length === 0) return;
3622
+ const counts = /* @__PURE__ */ new Map();
3623
+ for (const { table, parent } of rows) {
3624
+ const key = `${table} -> ${parent}`;
3625
+ counts.set(key, (counts.get(key) ?? 0) + 1);
3626
+ }
3627
+ const summary = [...counts].map(([key, count]) => `${key} (${count})`).join(", ");
3628
+ console.warn(`[guren/orm] The migrations just applied left ${rows.length} row(s) whose foreign key names no parent row: ${summary}.\n[guren/orm] Foreign keys are not enforced while migrations run. \`PRAGMA foreign_key_check\` lists the rows.`);
3629
+ }
3613
3630
  function createSqliteDatabase(options) {
3614
3631
  const { migrationsFolder, filename, seedersFolder, relations } = options;
3615
3632
  const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath(migrationsFolder) : resolve(String(migrationsFolder));
@@ -3646,6 +3663,7 @@ function createSqliteDatabase(options) {
3646
3663
  const { drizzle } = await import("drizzle-orm/bun-sqlite");
3647
3664
  const sqlite = new Database(target.kind === "memory" ? ":memory:" : dbFile ?? dbPath);
3648
3665
  sqlite.exec("PRAGMA journal_mode = WAL;");
3666
+ sqlite.exec("PRAGMA foreign_keys = ON;");
3649
3667
  sqliteClient = sqlite;
3650
3668
  const handle = drizzle({
3651
3669
  client: sqlite,
@@ -3690,7 +3708,17 @@ function createSqliteDatabase(options) {
3690
3708
  const { migrate } = await import("drizzle-orm/bun-sqlite/migrator");
3691
3709
  await migrateAndReport(resolvedMigrationsFolder, {
3692
3710
  readApplied: report ? readAppliedMigrations : void 0,
3693
- migrate: () => migrate(db, { migrationsFolder: resolvedMigrationsFolder })
3711
+ migrate: () => {
3712
+ const handle = db;
3713
+ const appliedBefore = readAppliedMigrations().length;
3714
+ handle.run(sql.raw("PRAGMA foreign_keys = OFF;"));
3715
+ try {
3716
+ migrate(db, { migrationsFolder: resolvedMigrationsFolder });
3717
+ } finally {
3718
+ handle.run(sql.raw("PRAGMA foreign_keys = ON;"));
3719
+ }
3720
+ if (readAppliedMigrations().length > appliedBefore) reportForeignKeyViolations(handle);
3721
+ }
3694
3722
  });
3695
3723
  return summary;
3696
3724
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/orm",
3
- "version": "2.13.0",
3
+ "version": "2.14.0",
4
4
  "description": "Eloquent-style models for the Guren framework, on Drizzle ORM: PostgreSQL, MySQL, SQLite and Cloudflare D1.",
5
5
  "keywords": [
6
6
  "guren",