@secondlayer/subgraphs 3.14.1 → 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
  };
@@ -1413,6 +1414,7 @@ import { resolveTraitContractIds } from "@secondlayer/shared/db/queries/contract
1413
1414
  import { advanceOperationCursor } from "@secondlayer/shared/db/queries/subgraph-operations";
1414
1415
  import {
1415
1416
  isByoSubgraph,
1417
+ recordLiveProgress,
1416
1418
  recordSubgraphProcessed,
1417
1419
  resolveSubgraphDb,
1418
1420
  updateSubgraphStatus
@@ -2107,7 +2109,7 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2107
2109
  result.matched = matched.length;
2108
2110
  if (matched.length === 0) {
2109
2111
  if (!opts?.skipProgressUpdate) {
2110
- await updateSubgraphStatus(targetDb, subgraphName, "active", blockHeight);
2112
+ await recordLiveProgress(targetDb, subgraphName, blockHeight);
2111
2113
  }
2112
2114
  return result;
2113
2115
  }
@@ -2130,8 +2132,11 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
2130
2132
  const applyProgress = async (tx, rr) => {
2131
2133
  if (opts?.skipProgressUpdate)
2132
2134
  return;
2133
- const status = rr.errors > 0 && rr.processed === 0 ? "error" : "active";
2134
- await updateSubgraphStatus(tx, subgraphName, status, blockHeight);
2135
+ if (rr.errors > 0 && rr.processed === 0) {
2136
+ await updateSubgraphStatus(tx, subgraphName, "error", blockHeight);
2137
+ } else {
2138
+ await recordLiveProgress(tx, subgraphName, blockHeight);
2139
+ }
2135
2140
  if (rr.processed > 0 || rr.errors > 0) {
2136
2141
  const lastError = rr.errors > 0 ? `${rr.errors} error(s) at block ${blockHeight}` : undefined;
2137
2142
  await recordSubgraphProcessed(tx, subgraphName, rr.processed, rr.errors, lastError);
@@ -4023,5 +4028,5 @@ export {
4023
4028
  ByoBreakingChangeError
4024
4029
  };
4025
4030
 
4026
- //# debugId=9ABB4400810C6A0464756E2164756E21
4031
+ //# debugId=43A9769D4DEED50064756E2164756E21
4027
4032
  //# sourceMappingURL=index.js.map