shutro 0.3.4 → 0.3.6

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.
@@ -354,6 +354,27 @@ export async function createRecords(host, model, input, options = {}) {
354
354
  for (const parent of new Set(parents))
355
355
  invalidateInverseLists(host, model, key, null, parent);
356
356
  }
357
+ // A computed field with an inverse is writable on create too (Kosh request #36): its given value is cached and
358
+ // the inverse runs for the records that gave it, as on write; the compute still runs at the flush point and
359
+ // should agree with what the inverse wrote.
360
+ const inverted = new Map();
361
+ prepared.forEach((p, i) => {
362
+ for (const [key, value] of p.stored) {
363
+ const field = model.fields[key];
364
+ if (!field.computed || field.inverse === undefined)
365
+ continue;
366
+ // Only what the caller gave (a compute's own key may be prepared with nothing given).
367
+ const row = input[i];
368
+ if (row === undefined || row[key] === undefined)
369
+ continue;
370
+ host.values(model.name, ids[i], true).set(key, value);
371
+ inverted.set(key, [...(inverted.get(key) ?? []), ids[i]]);
372
+ }
373
+ });
374
+ for (const [key, withValue] of inverted) {
375
+ const inverse = model.fields[key].inverse;
376
+ await inverse(host.recordset(model, withValue), key);
377
+ }
357
378
  for (const f of internals.registry.compute.stored(model.name))
358
379
  internals.markToCompute(model.name, f.key, ids);
359
380
  // one2many `create` commands of every new parent become one child insert per field (no INSERT per parent).
@@ -734,7 +755,12 @@ async function applyCommands(host, model, parentId, key, commands, mode) {
734
755
  }
735
756
  parentValues.set(key, [...current]);
736
757
  internals.trigger(model.name, key, [parentId]);
758
+ // The junction table changed: a change event names the key, even when no column of the row was written (a write
759
+ // with scalars too merges its keys into the same event).
760
+ if (mode === 'write' && !sameIds(linked, current))
761
+ recordChange(internals, model, 'write', [parentId], [key]);
737
762
  }
763
+ const sameIds = (a, b) => a.length === b.size && a.every((id) => b.has(id));
738
764
  // -------------------------------------------------------------------------------------------------- flush
739
765
  /** Writes dirty records of `modelName` (or all models) in one `UPDATE … FROM unnest(...)` per (model, field set). */
740
766
  export async function flushDirty(host, modelName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shutro",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "The ORM built for business software: type-safe, secure-by-default, multi-company, audit-ready PostgreSQL ORM for Node.js and TypeScript.",
5
5
  "keywords": [
6
6
  "orm",