Skip to content

Latest commit

 

History

History
112 lines (76 loc) · 4.11 KB

File metadata and controls

112 lines (76 loc) · 4.11 KB

Prisma Postgres Example: Deno Deploy (Deno 2, ESM)

This project showcases how to use the Prisma ORM with Prisma Postgres in an ESM Deno Deploy application.

Prerequisites

To successfully run the project, you will need the following:

  • Two Prisma Postgres connection strings:
    • Your Prisma Postgres + Accelerate connection string (containing your Prisma API key) which you can get by enabling Postgres in a project in your Prisma Data Platform account. You will use this connection string to run Prisma migrations.
    • Your Prisma Postgres direct TCP connection string which you will use with Prisma Client. Learn more in the docs.

Tech Stack

  • Deno 2

  • ESM

  • Prisma Client with the prisma-client generator See the Prisma schema file for details.

    generator client {
      provider = "prisma-client"
      output = "../src/generated/prisma"
      previewFeatures = ["driverAdapters", "queryCompiler"]
      runtime = "deno"
    }

Getting started

1. Clone the repository

Clone the repository, navigate into it and install dependencies:

git clone git@github.com:prisma/prisma-examples.git --depth=1
cd prisma-examples/generator-prisma-client/deno-deploy
deno install

2. Configure environment variables

Create a .env in the root of the project directory:

touch .env

Now, open the .env file and set the DATABASE_URL environment variables with the values of your connection string and your Prisma Postgres connection string:

# .env

# Prisma Postgres connection string (used for migrations)
DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__"

# Postgres connection string (used for queries by Prisma Client)
DIRECT_URL="__YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__"

Note that __YOUR_PRISMA_POSTGRES_CONNECTION_STRING__ is a placeholder value that you need to replace with the values of your Prisma Postgres + Accelerate connection string. Notice that the Accelerate connection string has the following structure: prisma+postgres://accelerate.prisma-data.net/?api_key=<api_key_value>.

Note that __YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__ is a placeholder value that you need to replace with the values of your Prisma Postgres direct TCP connection string. The direct connection string has the following structure: postgres://<username>:<password>@<host>:<port>/<database>.

3. Run a migration to create the database structure and seed the database

The Prisma schema file contains a single Quotes model and a QuoteKind enum. You can map this model to the database and create the corresponding Quotes table using the following command:

deno task prisma migrate dev --name init

You now have an empty Quotes table in your database. Next, run the seed script to create some sample records in the table:

deno task prisma db seed

4. Generate Prisma Client

Run:

deno task prisma generate

5. Start the app

You can run the app with the following command:

deno task dev

6. Deploy to Deno Deploy

To deploy your application to Deno Deploy, follow these steps:

deno install -gArf jsr:@deno/deployctl
deployctl deploy --project=prisma-client-deno-deploy --env-file=.env

Resources