Skip to content

Commit

Permalink
feat(core.indexing-store): introduce mode param to indexing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tk-o committed Nov 29, 2024
1 parent afba4c3 commit 66499c0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/indexing-store/historical.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ test("find", async (context) => {
initialCheckpoint: encodeCheckpoint(zeroCheckpoint),
});

expect(indexingStore.mode).toBe("historical");

// empty

let result = await indexingStore.find(schema.account, {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/indexing-store/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,9 @@ export const createHistoricalIndexingStore = ({
isCacheFull() {
return cacheBytes > maxBytes;
},
get mode() {
return "historical" as const;
},
} satisfies IndexingStore<"historical">;

// @ts-ignore
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/indexing-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import type { Schema } from "@/drizzle/index.js";
import type { Db } from "@/types/db.js";

export type IndexingStore<policy extends "historical" | "realtime"> =
policy extends "realtime"
? Db<Schema>
: Db<Schema> & {
Db<Schema> & { mode: policy } & policy extends "historical"
? {
/** Persist the cache to the database. */
flush: () => Promise<void>;
/** Return `true` if the cache size in bytes is above the limit specified by `option.indexingCacheMaxBytes`. */
isCacheFull: () => boolean;
};
}
: {};

export const parseSqlError = (e: any): Error => {
let error = getBaseError(e);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/indexing-store/realtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ test("find", async (context) => {
schema,
});

expect(indexingStore.mode).toBe("realtime");

// empty

let result = await indexingStore.find(schema.account, {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/indexing-store/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ export const createRealtimeIndexingStore = ({
}),
{ schema, casing: "snake_case" },
),
get mode() {
return "realtime" as const;
},
} satisfies IndexingStore<"realtime">;

// @ts-ignore
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/indexing/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ test("processSetupEvents()", async (context) => {
expect(indexingFunctions["Erc20:setup"]).toHaveBeenCalledWith({
context: {
network: { chainId: 1, name: "mainnet" },
store: { mode: "realtime" },
contracts: {
Erc20: {
abi: expect.any(Object),
Expand Down Expand Up @@ -242,6 +243,7 @@ test("processEvent() log events", async (context) => {
},
context: {
network: { chainId: 1, name: "mainnet" },
store: { mode: "realtime" },
contracts: {
Erc20: {
abi: expect.any(Object),
Expand Down Expand Up @@ -317,6 +319,7 @@ test("processEvents() block events", async (context) => {
},
context: {
network: { chainId: 1, name: "mainnet" },
store: { mode: "realtime" },
contracts: {
Erc20: {
abi: expect.any(Object),
Expand Down Expand Up @@ -397,6 +400,7 @@ test("processEvents() call trace events", async (context) => {
},
context: {
network: { chainId: 1, name: "mainnet" },
store: { mode: "realtime" },
contracts: {
Erc20: {
abi: expect.any(Object),
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/indexing/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { type ReadOnlyClient, buildCachedActions } from "./ponderActions.js";

export type Context = {
network: { chainId: number; name: string };
store: Pick<IndexingStore<"historical" | "realtime">, "mode">;
client: ReadOnlyClient;
db: Db<Schema>;
contracts: Record<
Expand Down Expand Up @@ -171,6 +172,7 @@ export const create = ({
contextState,
context: {
network: { name: undefined!, chainId: undefined! },
store: { mode: undefined! },
contracts: undefined!,
client: undefined!,
db: undefined!,
Expand Down Expand Up @@ -367,6 +369,8 @@ export const setIndexingStore = (
delete: indexingStore.delete,
sql: indexingStore.sql,
};

indexingService.currentEvent.context.store.mode = indexingStore.mode;
};

export const kill = (indexingService: Service) => {
Expand Down

0 comments on commit 66499c0

Please sign in to comment.