@secondlayer/subgraphs 3.26.0 → 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.
package/dist/src/index.js CHANGED
@@ -820,14 +820,10 @@ class SubgraphContext {
820
820
  this.ops.length = 0;
821
821
  const statements = this.buildStatements(opsToFlush);
822
822
  if ("isTransaction" in this.db) {
823
- for (const stmt of statements) {
824
- await sql.raw(stmt).execute(this.db);
825
- }
823
+ await this.execStatements(this.db, statements);
826
824
  } else {
827
825
  await this.db.transaction().execute(async (tx) => {
828
- for (const stmt of statements) {
829
- await sql.raw(stmt).execute(tx);
830
- }
826
+ await this.execStatements(tx, statements);
831
827
  });
832
828
  }
833
829
  const writes = opsToFlush.map((op, rowIndex) => {
@@ -846,6 +842,25 @@ class SubgraphContext {
846
842
  });
847
843
  return { count: opsToFlush.length, writes };
848
844
  }
845
+ async execStatements(executor, statements) {
846
+ const affected = (r) => r.numAffectedRows ?? 0n;
847
+ for (const stmt of statements) {
848
+ if (typeof stmt === "string") {
849
+ await sql.raw(stmt).execute(executor);
850
+ continue;
851
+ }
852
+ const updated = await sql.raw(stmt.update).execute(executor);
853
+ if (affected(updated) > 0n)
854
+ continue;
855
+ const inserted = await sql.raw(stmt.insert).execute(executor);
856
+ if (affected(inserted) > 0n)
857
+ continue;
858
+ const retried = await sql.raw(stmt.update).execute(executor);
859
+ if (affected(retried) === 0n) {
860
+ throw new Error(`increment could not be applied to ${this.pgSchemaName}.${stmt.table} where ${stmt.keyPred}: row neither updated nor inserted`);
861
+ }
862
+ }
863
+ }
849
864
  prepareInsert(op) {
850
865
  const upsertKeys = op.data._upsert_keys;
851
866
  const data = { ...op.data };
@@ -925,7 +940,6 @@ class SubgraphContext {
925
940
  for (const r of batch.rows.values()) {
926
941
  const keyPred = batch.keyCols.map((k) => `"${k}" = ${escapeLiteral(r.keys[k])}`).join(" AND ");
927
942
  const updSet = batch.deltaCols.map((c) => `"${c}" = COALESCE("${c}", 0) + (${String(r.deltas[c] ?? 0n)})`).join(", ");
928
- statements.push(`UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`);
929
943
  const insertVals = [
930
944
  ...batch.keyCols.map((k) => escapeLiteral(r.keys[k])),
931
945
  ...batch.deltaCols.map((c) => String(r.deltas[c] ?? 0n)),
@@ -933,7 +947,13 @@ class SubgraphContext {
933
947
  escapeLiteral(r.meta.txId),
934
948
  "NOW()"
935
949
  ];
936
- statements.push(`INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`);
950
+ statements.push({
951
+ kind: "increment",
952
+ table: batch.table,
953
+ keyPred,
954
+ update: `UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`,
955
+ insert: `INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`
956
+ });
937
957
  }
938
958
  incBatch = null;
939
959
  incBatchKey = "";
@@ -5086,5 +5106,5 @@ export {
5086
5106
  ByoBreakingChangeError
5087
5107
  };
5088
5108
 
5089
- //# debugId=06A0A7FF47E0C5EE64756E2164756E21
5109
+ //# debugId=7F2918CDE1AF5BC064756E2164756E21
5090
5110
  //# sourceMappingURL=index.js.map