@secondlayer/subgraphs 3.14.2 → 3.14.3

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.
@@ -520,21 +520,22 @@ class SubgraphContext {
520
520
  "_tx_id",
521
521
  "_created_at"
522
522
  ];
523
- const valuesList = Array.from(batch.rows.values()).map((r) => {
524
- const vals = [
523
+ if (this.journal) {
524
+ statements.push(this.journalCaptureSQL(batch.table, batch.keyCols, Array.from(batch.rows.values()).map((r) => batch.keyCols.map((k) => escapeLiteral(r.keys[k])))));
525
+ }
526
+ for (const r of batch.rows.values()) {
527
+ const keyPred = batch.keyCols.map((k) => `"${k}" = ${escapeLiteral(r.keys[k])}`).join(" AND ");
528
+ const updSet = batch.deltaCols.map((c) => `"${c}" = COALESCE("${c}", 0) + (${String(r.deltas[c] ?? 0n)})`).join(", ");
529
+ statements.push(`UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`);
530
+ const insertVals = [
525
531
  ...batch.keyCols.map((k) => escapeLiteral(r.keys[k])),
526
532
  ...batch.deltaCols.map((c) => String(r.deltas[c] ?? 0n)),
527
533
  escapeLiteral(r.meta.blockHeight),
528
534
  escapeLiteral(r.meta.txId),
529
535
  "NOW()"
530
536
  ];
531
- return `(${vals.join(", ")})`;
532
- }).join(", ");
533
- const setClauses = batch.deltaCols.map((c) => `"${c}" = COALESCE("${batch.table}"."${c}", 0) + EXCLUDED."${c}"`);
534
- if (this.journal) {
535
- statements.push(this.journalCaptureSQL(batch.table, batch.keyCols, Array.from(batch.rows.values()).map((r) => batch.keyCols.map((k) => escapeLiteral(r.keys[k])))));
537
+ statements.push(`INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`);
536
538
  }
537
- statements.push(`INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) VALUES ${valuesList} ` + `ON CONFLICT (${batch.keyCols.map((k) => `"${k}"`).join(", ")}) DO UPDATE SET ${setClauses.join(", ")}`);
538
539
  incBatch = null;
539
540
  incBatchKey = "";
540
541
  };
@@ -3519,5 +3520,5 @@ var shutdown = async () => {
3519
3520
  process.on("SIGINT", shutdown);
3520
3521
  process.on("SIGTERM", shutdown);
3521
3522
 
3522
- //# debugId=86CD0D48124A797164756E2164756E21
3523
+ //# debugId=9435991A9E2313C564756E2164756E21
3523
3524
  //# sourceMappingURL=service.js.map