@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.
package/dist/src/index.js CHANGED
@@ -598,21 +598,22 @@ class SubgraphContext {
598
598
  "_tx_id",
599
599
  "_created_at"
600
600
  ];
601
- const valuesList = Array.from(batch.rows.values()).map((r) => {
602
- const vals = [
601
+ if (this.journal) {
602
+ statements.push(this.journalCaptureSQL(batch.table, batch.keyCols, Array.from(batch.rows.values()).map((r) => batch.keyCols.map((k) => escapeLiteral(r.keys[k])))));
603
+ }
604
+ for (const r of batch.rows.values()) {
605
+ const keyPred = batch.keyCols.map((k) => `"${k}" = ${escapeLiteral(r.keys[k])}`).join(" AND ");
606
+ const updSet = batch.deltaCols.map((c) => `"${c}" = COALESCE("${c}", 0) + (${String(r.deltas[c] ?? 0n)})`).join(", ");
607
+ statements.push(`UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`);
608
+ const insertVals = [
603
609
  ...batch.keyCols.map((k) => escapeLiteral(r.keys[k])),
604
610
  ...batch.deltaCols.map((c) => String(r.deltas[c] ?? 0n)),
605
611
  escapeLiteral(r.meta.blockHeight),
606
612
  escapeLiteral(r.meta.txId),
607
613
  "NOW()"
608
614
  ];
609
- return `(${vals.join(", ")})`;
610
- }).join(", ");
611
- const setClauses = batch.deltaCols.map((c) => `"${c}" = COALESCE("${batch.table}"."${c}", 0) + EXCLUDED."${c}"`);
612
- if (this.journal) {
613
- statements.push(this.journalCaptureSQL(batch.table, batch.keyCols, Array.from(batch.rows.values()).map((r) => batch.keyCols.map((k) => escapeLiteral(r.keys[k])))));
615
+ statements.push(`INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`);
614
616
  }
615
- 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(", ")}`);
616
617
  incBatch = null;
617
618
  incBatchKey = "";
618
619
  };
@@ -4027,5 +4028,5 @@ export {
4027
4028
  ByoBreakingChangeError
4028
4029
  };
4029
4030
 
4030
- //# debugId=8B1D7AE53DBBA74F64756E2164756E21
4031
+ //# debugId=43A9769D4DEED50064756E2164756E21
4031
4032
  //# sourceMappingURL=index.js.map