Skip to content

Commit b2f4057

Browse files
committed
chore: prepare Cloudflare Workers deployment
OpenNext + wrangler config, Hyperdrive pg per request on Workers, KYC files to R2 (still AES-256-GCM), PGlite excluded from the Worker bundle, db:migrate script and idempotent db:seed, README Deploy section.
1 parent 93129e1 commit b2f4057

18 files changed

Lines changed: 2964 additions & 218 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ node_modules/
99
next-env.d.ts
1010
.env*
1111
!.env.example
12+
.open-next/
13+
.wrangler/

‎README.md‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,39 @@ pnpm dev # http://localhost:3000 (next dev --webpack)
6060
```
6161

6262
- The database is PGlite, a file at `./.data/pglite`. It migrates and seeds the demo business on first start. Set `DATABASE_URL` to use a real Postgres instead.
63-
- `pnpm db:seed` deletes and re-creates the demo business. Owners can also reset it from **Settings**.
63+
- `pnpm db:seed` seeds the demo business if it is missing (safe to run twice); `pnpm db:seed -- --reset` deletes and re-creates it. Owners can also reset it from **Settings**.
64+
- `pnpm db:migrate` applies the committed SQL migrations to `DATABASE_URL`.
6465
- KYC files go to `./.data/uploads`, encrypted with AES-256-GCM (key from `KYC_KEY` or `SESSION_SECRET`).
6566
- QR codes and WhatsApp links use `APP_URL` (`https://nestkeep.craftlayer.store` in production, `http://localhost:31NN` in dev).
6667
- Checks: `pnpm typecheck && pnpm lint && pnpm test && pnpm build`, then `pnpm exec astryx doctor --built .next`.
6768
- `pnpm db:generate` writes a new SQL migration after a schema change (`src/server/db/migrations`, committed).
6869

70+
## Deploy (Cloudflare Workers)
71+
72+
Production runs on Cloudflare Workers via [OpenNext](https://opennext.js.org/cloudflare) at `https://nestkeep.craftlayer.store`. Config: [`wrangler.jsonc`](wrangler.jsonc), [`open-next.config.ts`](open-next.config.ts).
73+
74+
```bash
75+
DATABASE_URL=postgres://... pnpm db:migrate # apply committed migrations (Neon)
76+
DATABASE_URL=postgres://... pnpm db:seed # seed the demo business, idempotent
77+
pnpm cf:build # OpenNext build into .open-next/
78+
pnpm cf:preview # build and run locally in workerd
79+
pnpm cf:deploy # build and deploy
80+
pnpm cf-typegen # regenerate cloudflare-env.d.ts
81+
```
82+
83+
| Kind | Name | Purpose |
84+
|---|---|---|
85+
| Binding | `HYPERDRIVE` | Postgres (Neon) via Hyperdrive. A `pg` pool is created per request. Replace `REPLACE_WITH_HYPERDRIVE_ID` in `wrangler.jsonc` |
86+
| Binding | `UPLOADS` | R2 bucket `craftlayer-uploads`. KYC files, AES-256-GCM encrypted, keys under `nestkeep/`. Served only through staff-authorised routes |
87+
| Binding | `ASSETS` | Static assets from `.open-next/assets` |
88+
| Binding | `WORKER_SELF_REFERENCE` | Service binding to itself (OpenNext) |
89+
| Var | `APP_URL` | `https://nestkeep.craftlayer.store`, used in QR codes and WhatsApp links |
90+
| Var (optional) | `DEMO_MODE` | `1` shows OTPs on screen (demo only) |
91+
| Secret | `SESSION_SECRET` | Required, 16+ characters. Signs session cookies; KYC key fallback |
92+
| Secret | `KYC_KEY` | Recommended. Key source for KYC file encryption. Must never change once files exist |
93+
94+
On Workers the app does not migrate or seed at runtime, writes nothing to the filesystem, and never loads PGlite (the Cloudflare build aliases it away). Rate limits are in memory per isolate.
95+
6996
## Demo walkthrough
7097

7198
| Step | Where | What happens |
@@ -102,7 +129,7 @@ pnpm dev # http://localhost:3000 (next dev --webpack)
102129
| Electricity sub-meter billing, vacancy lead page, rent agreement e-sign, referral rewards | P1 | Planned |
103130
| Multi-property roles, accounting export, expenses, DigiLocker | P2 | Planned |
104131

105-
Integrations are adapters: WhatsApp (`src/server/adapters/messaging.ts`) writes to an `outbox` table; payments (`adapters/payments.ts`) build `upi://pay` links; documents (`adapters/storage.ts`) are local encrypted files. No real API keys and no third-party network calls.
132+
Integrations are adapters: WhatsApp (`src/server/adapters/messaging.ts`) writes to an `outbox` table; payments (`adapters/payments.ts`) build `upi://pay` links; documents (`adapters/storage.ts`) are AES-256-GCM encrypted files on local disk, or in R2 on Cloudflare Workers. No real API keys and no third-party network calls.
106133

107134
## Built on the shared core
108135

‎eslint.config.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import nextTs from "eslint-config-next/typescript";
55
const eslintConfig = defineConfig([
66
...nextVitals,
77
...nextTs,
8-
globalIgnores([".next/**", "out/**", "build/**", "next-env.d.ts"]),
8+
globalIgnores([".next/**", "out/**", "build/**", "next-env.d.ts", ".open-next/**", ".wrangler/**", "cloudflare-env.d.ts"]),
99
]);
1010

1111
export default eslintConfig;

‎next.config.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
import path from "node:path";
12
import type { NextConfig } from "next";
23

4+
/** OpenNext sets this while it runs `next build` for the Cloudflare Workers bundle. */
5+
const isCloudflareBuild = process.env.NEXT_PRIVATE_STANDALONE === "true";
6+
7+
const dbDir = path.resolve(process.cwd(), "src/server/db");
8+
39
const nextConfig: NextConfig = {
410
reactStrictMode: true,
511
poweredByHeader: false,
612
// PGlite ships WASM and data files; load it from node_modules at runtime instead of bundling.
7-
serverExternalPackages: ["@electric-sql/pglite"],
13+
// The Cloudflare build drops it entirely (see the webpack alias below).
14+
serverExternalPackages: isCloudflareBuild ? [] : ["@electric-sql/pglite"],
15+
webpack(config, { isServer, webpack }) {
16+
if (isCloudflareBuild && isServer) {
17+
// Swap the PGlite module for a stub so neither PGlite nor drizzle-orm/pglite is bundled.
18+
config.plugins.push(
19+
new webpack.NormalModuleReplacementPlugin(/^\.\/pglite$/, (resource: { request: string; context: string }) => {
20+
if (path.resolve(resource.context) === dbDir) resource.request = "./pglite.workers";
21+
}),
22+
);
23+
config.resolve ??= {};
24+
config.resolve.alias = { ...config.resolve.alias, "@electric-sql/pglite": false };
25+
}
26+
return config;
27+
},
828
};
929

1030
export default nextConfig;

‎open-next.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
2+
3+
export default defineCloudflareConfig({});

‎package.json‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
"typecheck": "next typegen && tsc --noEmit",
1212
"test": "vitest run",
1313
"db:generate": "drizzle-kit generate",
14-
"db:seed": "tsx src/server/db/seed-cli.ts"
14+
"db:seed": "tsx src/server/db/seed-cli.ts",
15+
"db:migrate": "tsx src/server/db/migrate-cli.ts",
16+
"cf:build": "opennextjs-cloudflare build",
17+
"cf:preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
18+
"cf:deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
19+
"cf-typegen": "wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts"
1520
},
1621
"packageManager": "pnpm@11.28.6",
1722
"engines": {
@@ -31,6 +36,7 @@
3136
},
3237
"devDependencies": {
3338
"@astryxdesign/cli": "0.7.17",
39+
"@opennextjs/cloudflare": "1.20.19",
3440
"@types/node": "^20.19.43",
3541
"@types/pg": "8.23.1",
3642
"@types/qrcode": "1.5.6",
@@ -41,6 +47,7 @@
4147
"eslint-config-next": "16.4.6",
4248
"tsx": "4.23.15",
4349
"typescript": "5.9.3",
44-
"vitest": "5.1.1"
50+
"vitest": "5.1.1",
51+
"wrangler": "4.168.0"
4552
}
4653
}

0 commit comments

Comments
 (0)