iTranslated by AI
Using Prisma Client for Database Connections in Deno Fresh Applications
The project, which had been steadily progressing thanks to the people at Prisma and Deno, has finally come to fruition. Since Prisma is now available on Deno Deploy, I decided to give it a try with Fresh.
Environment
- Deploy to Deno Deploy
- Must be able to reach the DB server from the Prisma Data Platform
- Use Supabase Cloud PostgreSQL for the DB server
Prisma Data Platform
This is the official way to use Prisma's Data Proxy feature.
By setting a connection URL like DATABASE_URL="prisma://aws-us-east-1.prisma-data.com/?api_key=XXX" in the Prisma Client, queries are executed over HTTP.
Deno has a postgres driver that can also be used on Deno Deploy, but at this stage, connecting via Data Proxy is required to run Prisma Client code through Deno.
Creating a Fresh Application
Just as described there. There are no examples in the Fresh documentation on how to build the database layer, and an issue like the one below has been opened:
Prisma Client Configuration
Documentation can be found here:
I modified schema.prisma as follows:
generator client {
provider = "prisma-client-js"
output = "../generated/client"
previewFeatures = ["deno"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Event {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
}
Define the environment variables in .env. Add the connection URL for the Prisma Data Platform.
DATABASE_URL="prisma://aws-us-east-1.prisma-data.com/?api_key=XXX"
PRISMA_GENERATE_DATAPROXY="true"
For the version using migrate, you can specify the Supabase host directly with postgres:// via DATABASE_MIGRATE_URL.
Since db push/pull cannot be performed using prisma://, I did it this way:
DATABASE_URL="postgresql://postgres:PASSWORD@db.XXX.supabase.co:5432/postgres?schema=public" deno run -A --unstable npm:prisma db push
prisma generate
Generate the Prisma Client code. You can either set PRISMA_GENERATE_DATAPROXY="true" or use the --data-proxy flag.
deno run -A --unstable npm:prisma generate --data-proxy
A deno directory will be created under generated/.
❯ tree generated/
generated/
└── client
├── deno
│ ├── edge.js
│ ├── edge.ts
│ ├── index.d.ts
│ └── polyfill.js
├── edge.d.ts
├── edge.js
├── index-browser.js
├── index.d.ts
├── index.js
├── libquery_engine-darwin-arm64.dylib.node
├── package.json
├── runtime
│ ├── edge-esm.js
│ ├── edge.js
│ ├── index-browser.d.ts
│ ├── index-browser.js
│ ├── index.d.ts
│ └── index.js
└── schema.prisma
DB Connection from API Routes
Create routes/api/counter.ts. I've set it up to insert a record into the database and increment the count on every access.
import { PrismaClient } from '../../generated/client/deno/edge.ts'
import { HandlerContext } from "$fresh/server.ts";
const prisma = new PrismaClient();
export const handler = async (_req: Request, _ctx: HandlerContext): Promise<Response> => {
await prisma.event.create({data:{}})
const count = await prisma.event.count()
return new Response(JSON.stringify({count}), {headers: {"Content-Type": "application/json"}});
};
❯ http http://localhost:8000/api/counter
HTTP/1.1 200 OK
content-length: 11
content-type: application/json
date: Thu, 20 Oct 2022 15:36:04 GMT
vary: Accept-Encoding
{
"count": 5
}
Deploy
Configure DATABASE_URL="prisma://..." in the Deno Deploy environment as well and deploy.
Discussion