@secondlayer/subgraphs 3.25.1 → 3.26.1

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.
@@ -605,14 +605,10 @@ class SubgraphContext {
605
605
  this.ops.length = 0;
606
606
  const statements = this.buildStatements(opsToFlush);
607
607
  if ("isTransaction" in this.db) {
608
- for (const stmt of statements) {
609
- await sql.raw(stmt).execute(this.db);
610
- }
608
+ await this.execStatements(this.db, statements);
611
609
  } else {
612
610
  await this.db.transaction().execute(async (tx) => {
613
- for (const stmt of statements) {
614
- await sql.raw(stmt).execute(tx);
615
- }
611
+ await this.execStatements(tx, statements);
616
612
  });
617
613
  }
618
614
  const writes = opsToFlush.map((op, rowIndex) => {
@@ -631,6 +627,25 @@ class SubgraphContext {
631
627
  });
632
628
  return { count: opsToFlush.length, writes };
633
629
  }
630
+ async execStatements(executor, statements) {
631
+ const affected = (r) => r.numAffectedRows ?? 0n;
632
+ for (const stmt of statements) {
633
+ if (typeof stmt === "string") {
634
+ await sql.raw(stmt).execute(executor);
635
+ continue;
636
+ }
637
+ const updated = await sql.raw(stmt.update).execute(executor);
638
+ if (affected(updated) > 0n)
639
+ continue;
640
+ const inserted = await sql.raw(stmt.insert).execute(executor);
641
+ if (affected(inserted) > 0n)
642
+ continue;
643
+ const retried = await sql.raw(stmt.update).execute(executor);
644
+ if (affected(retried) === 0n) {
645
+ throw new Error(`increment could not be applied to ${this.pgSchemaName}.${stmt.table} where ${stmt.keyPred}: row neither updated nor inserted`);
646
+ }
647
+ }
648
+ }
634
649
  prepareInsert(op) {
635
650
  const upsertKeys = op.data._upsert_keys;
636
651
  const data = { ...op.data };
@@ -710,7 +725,6 @@ class SubgraphContext {
710
725
  for (const r of batch.rows.values()) {
711
726
  const keyPred = batch.keyCols.map((k) => `"${k}" = ${escapeLiteral(r.keys[k])}`).join(" AND ");
712
727
  const updSet = batch.deltaCols.map((c) => `"${c}" = COALESCE("${c}", 0) + (${String(r.deltas[c] ?? 0n)})`).join(", ");
713
- statements.push(`UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`);
714
728
  const insertVals = [
715
729
  ...batch.keyCols.map((k) => escapeLiteral(r.keys[k])),
716
730
  ...batch.deltaCols.map((c) => String(r.deltas[c] ?? 0n)),
@@ -718,7 +732,13 @@ class SubgraphContext {
718
732
  escapeLiteral(r.meta.txId),
719
733
  "NOW()"
720
734
  ];
721
- statements.push(`INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`);
735
+ statements.push({
736
+ kind: "increment",
737
+ table: batch.table,
738
+ keyPred,
739
+ update: `UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`,
740
+ insert: `INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`
741
+ });
722
742
  }
723
743
  incBatch = null;
724
744
  incBatchKey = "";
@@ -3290,5 +3310,5 @@ export {
3290
3310
  bumpReorgEpoch
3291
3311
  };
3292
3312
 
3293
- //# debugId=8CB91249FBEDCD6B64756E2164756E21
3313
+ //# debugId=1544B1119237E68264756E2164756E21
3294
3314
  //# sourceMappingURL=catchup.js.map