Skip to content

Commit

Permalink
example: add pglite persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
jaipaljadeja committed Dec 2, 2024
1 parent 86a09e3 commit 8431a8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist
.turbo
bun.lockb
.vscode
.apibara
.apibara
.persistence
20 changes: 8 additions & 12 deletions examples/cli/indexers/2-starknet.indexer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { defineIndexer, useSink } from "@apibara/indexer";
import { defineIndexer } from "@apibara/indexer";
import { drizzlePersistence } from "@apibara/indexer/plugins/drizzle-persistence";
import { useLogger } from "@apibara/indexer/plugins/logger";
import { sqlite } from "@apibara/indexer/sinks/sqlite";
import { StarknetStream } from "@apibara/starknet";
import type { ApibaraRuntimeConfig } from "apibara/types";
import Database from "better-sqlite3";
import { sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/node-postgres";
import { Client } from "pg";
import { drizzle } from "drizzle-orm/pglite";
import { hash } from "starknet";

export default function (runtimeConfig: ApibaraRuntimeConfig) {
console.log("--> Starknet Indexer Runtime Config: ", runtimeConfig);

// Sink Database
const database = new Database(runtimeConfig.databasePath);
database.exec("DROP TABLE IF EXISTS test");
Expand All @@ -21,10 +18,9 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
);

// Persistence Database
const client = new Client({
connectionString: "postgres://postgres:postgres@localhost:5432/postgres",
const persistDatabase = drizzle("./.persistence", {
logger: true,
});
const persistDatabase = drizzle(client);

return defineIndexer(StarknetStream)({
streamUrl: "https://starknet.preview.apibara.org",
Expand Down Expand Up @@ -52,7 +48,7 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
async transform({ endCursor, block: { header }, context }) {
const logger = useLogger();
logger.info("Transforming block ", endCursor?.orderKey);
const { writer } = useSink({ context });
// const { writer } = useSink({ context });

// writer.insert([{
// number: header?.blockNumber.toString(),
Expand All @@ -61,8 +57,6 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
},
hooks: {
async "run:before"() {
await client.connect();

// Normally user will do migrations of both tables, which are defined in
// ```
// import { checkpoints, filters } from "@apibara/indexer/plugins/drizzle-persistence"
Expand All @@ -75,7 +69,9 @@ export default function (runtimeConfig: ApibaraRuntimeConfig) {
order_key INTEGER NOT NULL,
unique_key TEXT
);
`);

await persistDatabase.execute(sql`
CREATE TABLE IF NOT EXISTS filters (
id TEXT NOT NULL,
filter TEXT NOT NULL,
Expand Down

0 comments on commit 8431a8f

Please sign in to comment.