@apibara/plugin-mongo 2.1.0-beta.10 → 2.1.0-beta.12

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/index.cjs CHANGED
@@ -360,6 +360,7 @@ function mongoStorage({
360
360
  return plugins.defineIndexerPlugin((indexer) => {
361
361
  let indexerId = "";
362
362
  const alwaysReindex = process.env["APIBARA_ALWAYS_REINDEX"] === "true";
363
+ let prevFinality;
363
364
  indexer.hooks.hook("run:before", async () => {
364
365
  const { indexerName } = plugins$1.useInternalContext();
365
366
  indexerId = internal.generateIndexerId(indexerName, identifier);
@@ -461,16 +462,19 @@ function mongoStorage({
461
462
  });
462
463
  indexer.hooks.hook("handler:middleware", async ({ use }) => {
463
464
  use(async (context, next) => {
464
- const { endCursor } = context;
465
+ const { endCursor, finality, cursor } = context;
465
466
  if (!endCursor) {
466
467
  throw new MongoStorageError("end cursor is undefined");
467
468
  }
468
469
  await withTransaction(client, async (session) => {
469
470
  const db = client.db(dbName, dbOptions);
470
471
  context[MONGO_PROPERTY] = new MongoStorage(db, session, endCursor);
472
+ if (prevFinality === "pending") {
473
+ await invalidate(db, session, cursor, collections);
474
+ }
471
475
  await next();
472
476
  delete context[MONGO_PROPERTY];
473
- if (enablePersistence) {
477
+ if (enablePersistence && finality !== "pending") {
474
478
  await persistState({
475
479
  db,
476
480
  endCursor,
@@ -478,6 +482,7 @@ function mongoStorage({
478
482
  indexerId
479
483
  });
480
484
  }
485
+ prevFinality = finality;
481
486
  });
482
487
  });
483
488
  });
package/dist/index.mjs CHANGED
@@ -358,6 +358,7 @@ function mongoStorage({
358
358
  return defineIndexerPlugin((indexer) => {
359
359
  let indexerId = "";
360
360
  const alwaysReindex = process.env["APIBARA_ALWAYS_REINDEX"] === "true";
361
+ let prevFinality;
361
362
  indexer.hooks.hook("run:before", async () => {
362
363
  const { indexerName } = useInternalContext();
363
364
  indexerId = generateIndexerId(indexerName, identifier);
@@ -459,16 +460,19 @@ function mongoStorage({
459
460
  });
460
461
  indexer.hooks.hook("handler:middleware", async ({ use }) => {
461
462
  use(async (context, next) => {
462
- const { endCursor } = context;
463
+ const { endCursor, finality, cursor } = context;
463
464
  if (!endCursor) {
464
465
  throw new MongoStorageError("end cursor is undefined");
465
466
  }
466
467
  await withTransaction(client, async (session) => {
467
468
  const db = client.db(dbName, dbOptions);
468
469
  context[MONGO_PROPERTY] = new MongoStorage(db, session, endCursor);
470
+ if (prevFinality === "pending") {
471
+ await invalidate(db, session, cursor, collections);
472
+ }
469
473
  await next();
470
474
  delete context[MONGO_PROPERTY];
471
- if (enablePersistence) {
475
+ if (enablePersistence && finality !== "pending") {
472
476
  await persistState({
473
477
  db,
474
478
  endCursor,
@@ -476,6 +480,7 @@ function mongoStorage({
476
480
  indexerId
477
481
  });
478
482
  }
483
+ prevFinality = finality;
479
484
  });
480
485
  });
481
486
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apibara/plugin-mongo",
3
- "version": "2.1.0-beta.10",
3
+ "version": "2.1.0-beta.12",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -35,7 +35,7 @@
35
35
  "mongodb": "^6.12.0"
36
36
  },
37
37
  "dependencies": {
38
- "@apibara/indexer": "2.1.0-beta.10",
39
- "@apibara/protocol": "2.1.0-beta.10"
38
+ "@apibara/indexer": "2.1.0-beta.12",
39
+ "@apibara/protocol": "2.1.0-beta.12"
40
40
  }
41
41
  }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import type { DbOptions, MongoClient } from "mongodb";
4
4
 
5
5
  import { generateIndexerId } from "@apibara/indexer/internal";
6
6
  import { useInternalContext } from "@apibara/indexer/internal/plugins";
7
+ import type { Cursor, DataFinality } from "@apibara/protocol";
7
8
  import { cleanupStorage, finalize, invalidate } from "./mongo";
8
9
  import {
9
10
  finalizeState,
@@ -62,6 +63,7 @@ export function mongoStorage<TFilter, TBlock>({
62
63
  return defineIndexerPlugin<TFilter, TBlock>((indexer) => {
63
64
  let indexerId = "";
64
65
  const alwaysReindex = process.env["APIBARA_ALWAYS_REINDEX"] === "true";
66
+ let prevFinality: DataFinality | undefined;
65
67
 
66
68
  indexer.hooks.hook("run:before", async () => {
67
69
  const { indexerName } = useInternalContext();
@@ -189,7 +191,11 @@ export function mongoStorage<TFilter, TBlock>({
189
191
 
190
192
  indexer.hooks.hook("handler:middleware", async ({ use }) => {
191
193
  use(async (context, next) => {
192
- const { endCursor } = context;
194
+ const { endCursor, finality, cursor } = context as {
195
+ cursor: Cursor;
196
+ endCursor: Cursor;
197
+ finality: DataFinality;
198
+ };
193
199
 
194
200
  if (!endCursor) {
195
201
  throw new MongoStorageError("end cursor is undefined");
@@ -199,10 +205,16 @@ export function mongoStorage<TFilter, TBlock>({
199
205
  const db = client.db(dbName, dbOptions);
200
206
  context[MONGO_PROPERTY] = new MongoStorage(db, session, endCursor);
201
207
 
208
+ if (prevFinality === "pending") {
209
+ // invalidate if previous block's finality was "pending"
210
+ await invalidate(db, session, cursor, collections);
211
+ }
212
+
202
213
  await next();
214
+
203
215
  delete context[MONGO_PROPERTY];
204
216
 
205
- if (enablePersistence) {
217
+ if (enablePersistence && finality !== "pending") {
206
218
  await persistState({
207
219
  db,
208
220
  endCursor,
@@ -210,6 +222,8 @@ export function mongoStorage<TFilter, TBlock>({
210
222
  indexerId,
211
223
  });
212
224
  }
225
+
226
+ prevFinality = finality;
213
227
  });
214
228
  });
215
229
  });