Skip to content

Latest commit

 

History

History
81 lines (56 loc) · 2.38 KB

File metadata and controls

81 lines (56 loc) · 2.38 KB

cloudflare-d1-prisma-honox-starter

A starter project showing how you can integrate Prisma and Cloudflare D1 with a HonoX application to build awesome full-stack apps.

Tip

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

  • HonoX is a full-stack framework combining backend API endpoints with the ability to render frontend applications with React
  • Cloudflare D1 is a SQL database
  • Prisma provides an ORM wrapper around D1, to allow data models and querying using a straightforward syntax

Tweet

Installation

  1. Clone the database and install all dependencies:
$ git clone https://github.com/kristianfreeman/cloudflare-d1-prisma-honox-starter starter
$ cd starter
$ npm install
  1. Copy the wrangler.toml.example file to wrangler.toml and fill in the necessary values:
$ cp wrangler.toml.example wrangler.toml
  • Replace $APP_NAME with your Cloudflare Workers application name
  • Replace $CURRENT_DATE with the current date like this 2022-01-01
  1. Create a new D1 database using Wrangler:
$ npx wrangler d1 create <your-database-name>

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

Copy the output of this command, which is structured TOML configuration, into your wrangler.toml.

  1. Create a new migration file using the d1 migrations subcommand:
$ npx wrangler d1 migrations create <your-database-name> create_tables
  1. Using Prisma's CLI, generate a SQL file based on your Prisma schema in the empty migration file you just created:
$ npx prisma migrate diff \
  --from-empty \
  --to-schema-datamodel ./prisma/schema.prisma \
  --script \
  --output migrations/0001_create_tables.sql
  1. Generate the necessary Prisma files for Zod validations and any other configured Prisma outputs:
$ npx prisma generate
  1. Apply the migration to your local database:
$ npx wrangler d1 migrations apply <your-database-name>
  1. When you're ready, deploy your application:
$ npm run deploy
  1. Once you've deployed your application, you can apply the migrations to your remote (production) D1 database:
$ npx wrangler d1 migrations apply <your-database-name> --remote