Skip to content

Commit fd21326

Browse files
committed
kv
1 parent 4541d94 commit fd21326

14 files changed

Lines changed: 6135 additions & 4308 deletions

KV_SETUP.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Vercel KV Cache Setup
2+
3+
This project has been updated to use Vercel KV for caching graph data instead of generating it on every build.
4+
5+
## Setup
6+
7+
### 1. Install Dependencies
8+
9+
```bash
10+
npm install @vercel/kv
11+
```
12+
13+
### 2. Set up Vercel KV
14+
15+
1. Go to your Vercel project dashboard
16+
2. Navigate to the "Storage" tab
17+
3. Create a new KV database
18+
4. Copy the environment variables to your `.env.local` file:
19+
```
20+
KV_REST_API_URL=your_kv_url
21+
KV_REST_API_TOKEN=your_kv_token
22+
```
23+
24+
### 3. Environment Variables (Optional)
25+
26+
For additional security, you can set up a cron secret:
27+
28+
```
29+
CRON_SECRET=your_secret_key
30+
```
31+
32+
## How It Works
33+
34+
### 1. Automatic Updates
35+
36+
- The cron job runs every 6 hours (`0 */6 * * *`)
37+
- It fetches fresh data from the databases
38+
- Stores the processed graph data in Vercel KV
39+
40+
### 2. Data Serving
41+
42+
- The main page fetches data from `/api/graph-data`
43+
- Data is served from KV cache (fast)
44+
- Includes browser caching headers
45+
- Automatic refresh every 5 minutes client-side
46+
47+
### 3. Manual Updates
48+
49+
- Visit `/api/update-cache` to manually trigger a cache update
50+
- Useful for testing and initial setup
51+
52+
## API Routes
53+
54+
- `POST /api/cron` - Cron job endpoint (called automatically)
55+
- `GET /api/graph-data` - Serves cached data to the frontend
56+
- `GET /api/update-cache` - Manual cache update trigger
57+
58+
## Benefits
59+
60+
1. **Faster Loading**: No database queries during page load
61+
2. **Reduced Database Load**: Queries run only every 6 hours
62+
3. **Better UX**: Loading states and error handling
63+
4. **Scalability**: Can handle more concurrent users
64+
5. **Cost Efficiency**: Fewer database connections
65+
66+
## Monitoring
67+
68+
Check the Vercel function logs to monitor:
69+
70+
- Cron job execution
71+
- Cache update success/failures
72+
- Data freshness
73+
74+
## Troubleshooting
75+
76+
If you see "No data available":
77+
78+
1. Check that the databases are accessible
79+
2. Manually trigger cache update via `/api/update-cache`
80+
3. Check Vercel function logs for errors
81+
4. Verify KV environment variables are set correctly

db/db.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
import { Kysely, PostgresDialect } from "kysely";
2-
import { type DB } from "kysely-codegen";
32
import { Pool } from "pg";
4-
import { PointPlugin } from "./plugins";
3+
import { type DB as GraphDB } from "./graph-db";
4+
import { type DB as IndexerDB } from "./indexer-db";
55

6-
const globalForKysely = globalThis as unknown as {
7-
kysely: Kysely<DB> | undefined;
6+
const globalForDatabases = globalThis as unknown as {
7+
graphDB: Kysely<GraphDB> | undefined;
8+
indexerDB: Kysely<IndexerDB> | undefined;
89
};
9-
10-
export const kysely =
11-
globalForKysely.kysely ??
12-
new Kysely<DB>({
10+
export type { GraphDB, IndexerDB };
11+
export const graphDB =
12+
globalForDatabases.graphDB ??
13+
new Kysely<GraphDB>({
1314
dialect: new PostgresDialect({
1415
pool: new Pool({
1516
connectionString: process.env.DATABASE_URL,
17+
idleTimeoutMillis: 10000,
1618
}),
1719
}),
18-
plugins: [new PointPlugin()],
1920
});
20-
21-
if (process.env.NODE_ENV !== "production") globalForKysely.kysely = kysely;
21+
export const indexerDB =
22+
globalForDatabases.indexerDB ??
23+
new Kysely<IndexerDB>({
24+
dialect: new PostgresDialect({
25+
pool: new Pool({
26+
connectionString: process.env.INDEXER_DB_URL,
27+
}),
28+
}),
29+
// log: env.NODE_ENV !== "production" ? ["query"] : undefined,
30+
});
31+
globalForDatabases.graphDB = graphDB;
32+
globalForDatabases.indexerDB = indexerDB;

0 commit comments

Comments
 (0)