Sample of Prisma engine running in cloudflare workers and behaving similarly to prisma-accelerate.
- package.json
Need pg-compat
to patch pg
to fix it.
{
"name": "prisma-accelerate-workers",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"dependencies": {
"@prisma/adapter-pg": "^5.20.0",
"@prisma/client": "^5.20.0",
"pg": "^8.13.0",
"prisma-accelerate-local": "^1.1.10"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241011.0",
"@types/pg": "^8.11.10",
"pg-compat": "^0.0.7",
"typescript": "^5.6.3",
"wrangler": "^3.80.4"
}
}
- wrangler.toml
Set nodejs_compat_v2
.
name = "prisma-accelerate-workers"
main = "src/index.ts"
minify = true
compatibility_date = "2024-09-23"
compatibility_flags = ["nodejs_compat_v2"]
[placement]
mode = "smart"
[observability]
enabled = true
- src/index.ts
import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { createFetcher } from 'prisma-accelerate-local/workers';
import WASM from '@prisma/client/runtime/query_engine_bg.postgresql.wasm';
export type Env = {
SECRET: string;
};
export default {
fetch: createFetcher({
runtime: () => require(`@prisma/client/runtime/query_engine_bg.postgresql.js`),
secret: (env: Env) => env.SECRET,
queryEngineWasmModule: WASM,
adapter: (datasourceUrl: string) => {
const url = new URL(datasourceUrl);
const schema = url.searchParams.get('schema') ?? undefined;
const pool = new Pool({
connectionString: url.toString() ?? undefined,
});
return new PrismaPg(pool, {
schema,
});
},
}),
};
npx prisma-accelerate-local -s SECRET -m DB_URL
npx prisma-accelerate-local -s abc -m postgres://postgres:[email protected]:5432/postgres?schema=public
DATABASE_URL="prisma://xxxx.workers.dev/?api_key=xxx"