Skip to content

Commit

Permalink
feat(store-indexer): use fastify, move trpc to /trpc (#1232)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Cory <[email protected]>
  • Loading branch information
holic and roninjin10 authored Aug 15, 2023
1 parent b8a6158 commit b621fb9
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 32 deletions.
22 changes: 22 additions & 0 deletions .changeset/fifty-suits-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@latticexyz/store-indexer": major
---

Adds a [Fastify](https://fastify.dev/) server in front of tRPC and puts tRPC endpoints under `/trpc` to make way for other top-level endpoints (e.g. [tRPC panel](https://github.com/iway1/trpc-panel) or other API frontends like REST or gRPC).

If you're using `@latticexyz/store-sync` packages with an indexer (either `createIndexerClient` or `indexerUrl` argument of `syncToRecs`), then you'll want to update your indexer URL:

```diff
createIndexerClient({
- url: "https://indexer.dev.linfra.xyz",
+ url: "https://indexer.dev.linfra.xyz/trpc",
});
```

```diff
syncToRecs({
...
- indexerUrl: "https://indexer.dev.linfra.xyz",
+ indexerUrl: "https://indexer.dev.linfra.xyz/trpc",
});
```
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/setup/startIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function startIndexer(
});

return {
url: `http://127.0.0.1:${port}`,
url: `http://127.0.0.1:${port}/trpc`,
doneSyncing,
process: proc,
kill: () =>
Expand Down
30 changes: 20 additions & 10 deletions packages/store-indexer/bin/sqlite-indexer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from "node:fs";
import { z } from "zod";
import cors from "cors";
import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";
import { createPublicClient, fallback, webSocket, http, Transport } from "viem";
import { createHTTPServer } from "@trpc/server/adapters/standalone";
import fastify from "fastify";
import { fastifyTRPCPlugin } from "@trpc/server/adapters/fastify";
import { createAppRouter } from "@latticexyz/store-sync/trpc-indexer";
import { chainState, schemaVersion, syncToSqlite } from "@latticexyz/store-sync/sqlite";
import { createStorageAdapter } from "../src/sqlite/createStorageAdapter";
Expand Down Expand Up @@ -108,13 +108,23 @@ combineLatest([latestBlockNumber$, blockStorageOperations$])
console.log("all caught up");
});

const server = createHTTPServer({
middleware: cors(),
router: createAppRouter(),
createContext: async () => ({
storageAdapter: await createStorageAdapter(database),
}),
// @see https://fastify.dev/docs/latest/
const server = fastify({
maxParamLength: 5000,
});

const { port } = server.listen(env.PORT);
console.log(`tRPC listening on http://127.0.0.1:${port}`);
await server.register(import("@fastify/cors"));

// @see https://trpc.io/docs/server/adapters/fastify
server.register(fastifyTRPCPlugin, {
prefix: "/trpc",
trpcOptions: {
router: createAppRouter(),
createContext: async () => ({
storageAdapter: await createStorageAdapter(database),
}),
},
});

await server.listen({ port: env.PORT });
console.log(`indexer server listening on http://127.0.0.1:${env.PORT}`);
3 changes: 2 additions & 1 deletion packages/store-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"test": "tsc --noEmit --skipLibCheck"
},
"dependencies": {
"@fastify/cors": "^8.3.0",
"@latticexyz/block-logs-stream": "workspace:*",
"@latticexyz/common": "workspace:*",
"@latticexyz/store": "workspace:*",
Expand All @@ -35,9 +36,9 @@
"@trpc/server": "10.34.0",
"@wagmi/chains": "^0.2.22",
"better-sqlite3": "^8.4.0",
"cors": "^2.8.5",
"debug": "^4.3.4",
"drizzle-orm": "^0.27.0",
"fastify": "^4.21.0",
"rxjs": "7.5.5",
"superjson": "^1.12.4",
"viem": "1.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AppRouter } from "./createAppRouter";

type CreateIndexerClientOptions = {
/**
* tRPC endpoint URL like `https://indexer.dev.linfra.xyz`.
* tRPC endpoint URL like `https://indexer.dev.linfra.xyz/trpc`.
*/
url: string;
};
Expand Down
Loading

0 comments on commit b621fb9

Please sign in to comment.