-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(store-indexer): add readme, refactor common env (#1533)
- Loading branch information
Showing
5 changed files
with
100 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store-indexer": patch | ||
--- | ||
|
||
Added README and refactored handling of common environment variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# store-indexer | ||
|
||
A minimal Typescript indexer for [MUD Store](https://mud.dev/store) events (built on [store-sync](https://npmjs.com/package/@latticexyz/store-sync)) | ||
|
||
## Usage | ||
|
||
Install and run with: | ||
|
||
```sh | ||
npm install @latticexyz/store-indexer | ||
|
||
npm sqlite-indexer | ||
# or | ||
npm postgres-indexer | ||
``` | ||
|
||
or execute the one of the package bins directly: | ||
|
||
```sh | ||
npx -p @latticexyz/store-indexer sqlite-indexer | ||
# or | ||
npx -p @latticexyz/store-indexer postgres-indexer | ||
``` | ||
|
||
## Configuration | ||
|
||
Each indexer can be configured with environment variables. | ||
|
||
### Common environment variables | ||
|
||
| Variable | Description | Default | | ||
| ------------------ | ---------------------------------------------------------- | --------- | | ||
| `HOST` | Host that the indexer server listens on | `0.0.0.0` | | ||
| `PORT` | Port that the indexer server listens on | `3001` | | ||
| `RPC_HTTP_URL` | HTTP URL for Ethereum RPC to fetch data from | | | ||
| `RPC_WS_URL` | WebSocket URL for Ethereum RPC to fetch data from | | | ||
| `START_BLOCK` | Block number to start indexing from | `0` | | ||
| `MAX_BLOCK_RANGE` | Maximum number of blocks to fetch from the RPC per request | `1000` | | ||
| `POLLING_INTERVAL` | How often to poll for new blocks (in milliseconds) | `1000` | | ||
|
||
Note that you only need one of `RPC_HTTP_URL` or `RPC_WS_URL`, but we recommend both. The WebSocket URL will be prioritized and fall back to the HTTP URL if there are any connection issues. | ||
|
||
### Postgres indexer environment variables | ||
|
||
| Variable | Description | Default | | ||
| -------------- | ----------------------- | ------- | | ||
| `DATABASE_URL` | Postgres connection URL | | | ||
|
||
### SQLite indexer environment variables | ||
|
||
| Variable | Description | Default | | ||
| ----------------- | ------------------------ | ------------ | | ||
| `SQLITE_FILENAME` | SQLite database filename | `indexer.db` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { isDefined } from "@latticexyz/common/utils"; | ||
import { z, ZodIntersection, ZodTypeAny } from "zod"; | ||
|
||
const commonSchema = z.intersection( | ||
z.object({ | ||
HOST: z.string().default("0.0.0.0"), | ||
PORT: z.coerce.number().positive().default(3001), | ||
START_BLOCK: z.coerce.bigint().nonnegative().default(0n), | ||
MAX_BLOCK_RANGE: z.coerce.bigint().positive().default(1000n), | ||
POLLING_INTERVAL: z.coerce.number().positive().default(1000), | ||
}), | ||
z | ||
.object({ | ||
RPC_HTTP_URL: z.string(), | ||
RPC_WS_URL: z.string(), | ||
}) | ||
.partial() | ||
.refine((values) => Object.values(values).some(isDefined)) | ||
); | ||
|
||
export function parseEnv<TSchema extends ZodTypeAny | undefined = undefined>( | ||
schema?: TSchema | ||
): z.infer<TSchema extends ZodTypeAny ? ZodIntersection<typeof commonSchema, TSchema> : typeof commonSchema> { | ||
const envSchema = schema !== undefined ? z.intersection(commonSchema, schema) : commonSchema; | ||
return envSchema.parse(process.env, { | ||
errorMap: (issue) => ({ | ||
message: `Missing or invalid environment variable: ${issue.path.join(".")}`, | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters