Skip to main content

Command Palette

Search for a command to run...

Connecting Cloudflare Workers to PostgreSQL Using Prisma Accelerate and Hono

Published
3 min read
Connecting Cloudflare Workers to PostgreSQL Using Prisma Accelerate and Hono

Cloudflare Workers provide a powerful way to deploy serverless applications globally, while Prisma Accelerate optimizes database performance for distributed applications. In this blog, we will walk through setting up a Cloudflare Worker using Hono as the library and connecting it to PostgreSQL via Prisma Accelerate.

Prerequisites

  • Node.js installed

  • A PostgreSQL database (Neon or Aiven recommended)

  • Prisma account

  • Cloudflare Wrangler installed (choose cloudflare worker in hono set up and run

      npm install wrangler --save-dev
    

Step-by-Step Implementation

1. Initialize Hono

Hono is a lightweight web framework for Cloudflare Workers. Start by creating a new Hono project:

npm create hono@latest

2. Install Prisma

2. Install Prisma

Prisma is our ORM to interface with PostgreSQL. Install Prisma as a development dependency:

npm install --save-dev prisma

3. Initialize Prisma

Run the following command to initialize Prisma:

npx prisma init

This creates a prisma directory with a default schema.prisma file.

4. Define the Database Schema

Edit prisma/schema.prisma to include the following:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id       Int    @id @default(autoincrement())
  name     String
  email    String
  password String
}

5. Run Migrations

To apply the schema to your database, run:

npx prisma migrate dev --name init

6. Set Up Prisma Accelerate

Sign up for Prisma Accelerate here and enable it by retrieving your database key.

Replace the database URL in .env with:

DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=your_key"

7. Install Prisma Accelerate Extension

Install the Prisma Accelerate extension as a dependency:

npm install @prisma/extension-accelerate

8. Generate Prisma Client

Generate the Prisma client with:

npx prisma generate --no-engine

9. Set Up Cloudflare Worker with Hono

Create a src/index.ts file and add the following code:

import { Hono } from 'hono'
import { PrismaClient } from '@prisma/client/edge'
import { withAccelerate } from '@prisma/extension-accelerate'
import { env } from 'hono/adapter'

const app = new Hono()

app.post('/', async (c) => {
  const body: { name: string; email: string; password: string } = await c.req.json()
  const { DATABASE_URL } = env<{ DATABASE_URL: string }>(c)

  const prisma = new PrismaClient({ datasourceUrl: DATABASE_URL }).$extends(withAccelerate())

  await prisma.user.create({
    data: { name: body.name, email: body.email, password: body.password }
  })

  return c.json({ msg: "User created successfully" })
})

export default app

10. Configure Wrangler

Ensure you set the database URL in wrangler.json:

{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "my-app",
  "main": "src/index.ts",
  "compatibility_date": "2025-02-11",
  "vars": {
    "DATABASE_URL": ""
  }
}

11. Deploy Your Worker

Finally, deploy your Cloudflare Worker with:

npx wrangler publish

or

npm run deploy

Conclusion

You have successfully connected a Cloudflare Worker to PostgreSQL using Prisma Accelerate and Hono. This setup allows for optimized database queries and a lightweight, serverless application architecture. Happy coding!