Skip to content

zaherg/cloudflare-workers-prisma-postgres-example

Repository files navigation

cloudflare postgres prisma example

Note

This is an example to show how to use the mentioned tools, so use it as a reference to build your project.

An example project showing how you can integrate Prisma, Cloudflare Workers and Supabase with Hono framework to build awesome api backend.

Tip

If the above paragraph was a bunch of buzzwords that seem confusing, here's the simple version:

  • Hono is Web application framework which is fast, lightweight, built on web standards.
  • Cloudflare Workers is a serverless execution environment.
  • Prisma provides an ORM wrapper around postgres, to allow data models and querying using a straightforward syntax.
  • Supabase is a serverless database provider, mainly postgres.

Installation

  1. Clone the repo and install all dependencies:
$ git clone https://github.com/zaherg/cloudflare-workers-prisma-postgres-example starter
$ cd starter
$ npm install
  1. Create a new supabase project

  2. Copy the database connection strings and add them to a file called .dev.vars, the data should be something like:

# Connect to Supabase via connection pooling with Supavisor.
# remember to add ?pgbouncer=true to the end of the connection string.
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public?pgbouncer=true"

# Direct connection to the database. Used for migrations.
DIRECT_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
  1. Update the prisma/schema.prisma file to include all the models you need.

Note

It is important that you have a look at the scripts section as it has multiple commands that will help you with your development.

  1. Using Prisma's CLI, create a migration from changes in Prisma schema, apply it to the database, trigger generators (e.g. Prisma Client)
$ npm run prisma migrate dev
  1. You will need to add the Database Connection strings to your environment variables, and it is always advisable to add them as secrets
# When running this command, you will be prompted to input the secret’s value:

$ npx wrangler secret put DIRECT_URL
$ npx wrangler secret put DATABASE_URL
  1. When you're ready, deploy your application:
$ npm run deploy

Note that if you haven't yet used Wrangler, you will be prompted to login to Cloudflare.