dzql 0.5.18 → 0.5.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dzql",
3
- "version": "0.5.18",
3
+ "version": "0.5.19",
4
4
  "description": "PostgreSQL-powered framework with zero boilerplate CRUD operations and real-time WebSocket synchronization",
5
5
  "type": "module",
6
6
  "main": "src/server/index.js",
@@ -487,8 +487,7 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;`;
487
487
  return `CREATE OR REPLACE FUNCTION ${this.name}_affected_documents(
488
488
  p_table_name TEXT,
489
489
  p_op TEXT,
490
- p_old JSONB,
491
- p_new JSONB
490
+ p_data JSONB
492
491
  ) RETURNS JSONB[] AS $$
493
492
  DECLARE
494
493
  v_affected JSONB[];
@@ -516,7 +515,7 @@ $$ LANGUAGE plpgsql IMMUTABLE;`;
516
515
  return `-- Root entity (${this.rootEntity}) changed
517
516
  WHEN '${this.rootEntity}' THEN
518
517
  v_affected := ARRAY[
519
- jsonb_build_object('${firstParam}', COALESCE((p_new->>'id')::int, (p_old->>'id')::int))
518
+ jsonb_build_object('${firstParam}', (p_data->>'id')::int)
520
519
  ];`;
521
520
  }
522
521
 
@@ -567,7 +566,7 @@ $$ LANGUAGE plpgsql IMMUTABLE;`;
567
566
  SELECT ARRAY_AGG(DISTINCT jsonb_build_object('${firstParam}', ${prevAlias}.${firstParam}))
568
567
  INTO v_affected
569
568
  ${joins.join('\n ')}
570
- WHERE via_0.${viaColumn} = COALESCE((p_new->>'${relFK}')::int, (p_old->>'${relFK}')::int);`;
569
+ WHERE via_0.${viaColumn} = (p_data->>'${relFK}')::int;`;
571
570
  }
572
571
 
573
572
  // Single-hop via
@@ -577,7 +576,7 @@ $$ LANGUAGE plpgsql IMMUTABLE;`;
577
576
  SELECT ARRAY_AGG(DISTINCT jsonb_build_object('${firstParam}', via_tbl.${firstParam}))
578
577
  INTO v_affected
579
578
  FROM ${viaTable} via_tbl
580
- WHERE via_tbl.${viaColumn} = COALESCE((p_new->>'${relFK}')::int, (p_old->>'${relFK}')::int);`;
579
+ WHERE via_tbl.${viaColumn} = (p_data->>'${relFK}')::int;`;
581
580
  }
582
581
 
583
582
  if (nestedIncludes) {
@@ -589,13 +588,13 @@ $$ LANGUAGE plpgsql IMMUTABLE;`;
589
588
  INTO v_affected
590
589
  FROM ${relEntity} rel
591
590
  JOIN ${Object.keys(nestedIncludes)[0]} parent ON parent.id = rel.${Object.keys(nestedIncludes)[0]}_id
592
- WHERE rel.id = COALESCE((p_new->>'id')::int, (p_old->>'id')::int);`;
591
+ WHERE rel.id = (p_data->>'id')::int;`;
593
592
  }
594
593
 
595
594
  return `-- Related entity (${relEntity}) changed
596
595
  WHEN '${relEntity}' THEN
597
596
  v_affected := ARRAY[
598
- jsonb_build_object('${firstParam}', COALESCE((p_new->>'${relFK}')::int, (p_old->>'${relFK}')::int))
597
+ jsonb_build_object('${firstParam}', (p_data->>'${relFK}')::int)
599
598
  ];`;
600
599
  }
601
600
 
@@ -18,23 +18,6 @@ export { createMCPRoute } from "./mcp.js";
18
18
  async function processSubscriptionUpdates(event, broadcast) {
19
19
  const { table, op, pk, data } = event;
20
20
 
21
- // Map data to p_old/p_new based on operation type
22
- // INSERT: new data only, UPDATE: both (we only have current), DELETE: old data only
23
- let p_old = null;
24
- let p_new = null;
25
- switch (op.toUpperCase()) {
26
- case 'INSERT':
27
- p_new = data;
28
- break;
29
- case 'UPDATE':
30
- p_old = data;
31
- p_new = data;
32
- break;
33
- case 'DELETE':
34
- p_old = data;
35
- break;
36
- }
37
-
38
21
  // Get all active subscriptions grouped by subscribable
39
22
  const subscriptionsByName = getSubscriptionsBySubscribable();
40
23
 
@@ -56,9 +39,10 @@ async function processSubscriptionUpdates(event, broadcast) {
56
39
  }
57
40
 
58
41
  // Ask PostgreSQL which subscription instances are affected
42
+ // Pass (table, op, data) - the data contains pk and fields needed to resolve affected documents
59
43
  const result = await sql.unsafe(
60
- `SELECT ${subscribableName}_affected_documents($1, $2, $3, $4) as affected`,
61
- [table, op, p_old, p_new]
44
+ `SELECT ${subscribableName}_affected_documents($1, $2, $3) as affected`,
45
+ [table, op, data]
62
46
  );
63
47
 
64
48
  const affectedParamSets = result[0]?.affected;