Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "indexer: do not call factory for pending blocks",
"packageName": "@apibara/indexer",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-drizzle: handle pending blocks",
"packageName": "@apibara/plugin-drizzle",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-mongo: handle pending blocks",
"packageName": "@apibara/plugin-mongo",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "plugin-sqlite: handle pending blocks",
"packageName": "@apibara/plugin-sqlite",
"email": "jadejajaipal5@gmail.com",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export async function run<TFilter, TBlock>(
let block: TBlock | null;

// when factory mode
if (isFactoryMode) {
if (isFactoryMode && finality !== "pending") {
assert(indexer.options.factory !== undefined);

const [factoryBlock, mainBlock] = blocks;
Expand Down
13 changes: 11 additions & 2 deletions packages/plugin-drizzle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function drizzleStorage<
let tableNames: string[] = [];
let indexerId = "";
const alwaysReindex = process.env["APIBARA_ALWAYS_REINDEX"] === "true";
let prevFinality: DataFinality | undefined;

try {
tableNames = Object.values((schema as TSchema) ?? db._.schema ?? {}).map(
Expand Down Expand Up @@ -299,7 +300,8 @@ export function drizzleStorage<
indexer.hooks.hook("handler:middleware", async ({ use }) => {
use(async (context, next) => {
try {
const { endCursor, finality } = context as {
const { endCursor, finality, cursor } = context as {
cursor: Cursor;
endCursor: Cursor;
finality: DataFinality;
};
Expand All @@ -315,6 +317,11 @@ export function drizzleStorage<
TSchema
>;

if (prevFinality === "pending") {
// invalidate if previous block's finality was "pending"
await invalidate(tx, cursor, idColumn, indexerId);
}

if (finality !== "finalized") {
await registerTriggers(
tx,
Expand All @@ -328,13 +335,15 @@ export function drizzleStorage<
await next();
delete context[DRIZZLE_PROPERTY];

if (enablePersistence) {
if (enablePersistence && finality !== "pending") {
await persistState({
tx,
endCursor,
indexerId,
});
}

prevFinality = finality;
});

if (finality !== "finalized") {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-drizzle/tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DrizzleStorageError } from "../src/utils";
export const testTable = pgTable("test", {
id: serial("id").primaryKey(),
blockNumber: integer("block_number").notNull(),
key: text("key"),
key: text("key").unique(),
count: integer("count"),
data: text("data"),
createdAt: timestamp("created_at"),
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function migratePgliteDb(db: PgLiteDb) {
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
block_number INTEGER NOT NULL,
key TEXT,
key TEXT UNIQUE,
count INTEGER,
data TEXT,
created_at TIMESTAMP
Expand Down
Loading