|
| 1 | +--- |
| 2 | +title: 'Integrate Prisma Postgres with Deno Deploy' |
| 3 | +metaTitle: 'Integrate Prisma Postgres with Deno Deploy' |
| 4 | +metaDescription: 'Learn how to integrate Prisma Postgres in a Deno Deploy project using a simple Deno application.' |
| 5 | +sidebar_label: 'Prisma Postgres on Deno' |
| 6 | +image: '/img/guides/prisma-deno-integration-cover.png' |
| 7 | +completion_time: '15 min' |
| 8 | +community_section: true |
| 9 | +--- |
| 10 | + |
| 11 | +[Deno Deploy](https://deno.com/deploy) includes a feature that allows you to provision a Prisma Postgres database directly within the platform. This guide demonstrates how to integrate Prisma Postgres in a Deno Deploy project using a minimal Deno application that logs HTTP requests to the database. |
| 12 | + |
| 13 | +By the end of this guide, you will have a deployed Deno app that writes to and reads from a Prisma Postgres database provisioned in Deno Deploy, using Prisma Client with `runtime = "deno"`. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- A [Deno Deploy](https://deno.com/deploy) account |
| 18 | +- Deno runtime installed ([installation guide](https://docs.deno.com/runtime/#install-deno)) |
| 19 | +- [Deno extension for VS Code](https://docs.deno.com/runtime/reference/vscode/) |
| 20 | + |
| 21 | +## 1. Create and set up a new Deno project |
| 22 | + |
| 23 | +Create a new Deno project using the `deno init` command, which generates a basic project structure with a main entry file and configuration. |
| 24 | + |
| 25 | +```terminal |
| 26 | +deno init prisma-postgres-deno-deploy |
| 27 | +cd prisma-postgres-deno-deploy |
| 28 | +``` |
| 29 | + |
| 30 | +### 1.1 Configure VS Code for Deno |
| 31 | + |
| 32 | +To ensure VS Code recognizes this as a Deno project and provides proper TypeScript validation, you need to initialize the workspace. Without this, VS Code will show errors when using Deno-specific APIs like `Deno.serve`. |
| 33 | + |
| 34 | +Install the [Deno extension for VS Code](https://docs.deno.com/runtime/reference/vscode/), then: |
| 35 | +1. Select **View** > **Command Palette** _(or press `Cmd+Shift+P` on macOS or `Ctrl+Shift+P` on Windows)_ |
| 36 | +2. Run the command **Deno: Initialize Workspace Configuration** |
| 37 | + |
| 38 | +### 1.2 Create a basic HTTP server |
| 39 | + |
| 40 | +Update the `main.ts` file to create a simple HTTP server that responds with "Hello, World!", establishing the foundation for your application before adding database functionality. |
| 41 | + |
| 42 | +```tsx file=main.ts |
| 43 | +function handler(_req: Request): Response { |
| 44 | + return new Response("Hello, World!"); |
| 45 | +} |
| 46 | + |
| 47 | +Deno.serve(handler); |
| 48 | +``` |
| 49 | + |
| 50 | +You can test the server locally by running: |
| 51 | + |
| 52 | +```terminal |
| 53 | +deno run dev |
| 54 | +``` |
| 55 | + |
| 56 | +Visit `localhost:8000` in your browser to see the application running. |
| 57 | + |
| 58 | +### 1.3 Push initial project to GitHub |
| 59 | + |
| 60 | +To connect your project to Deno Deploy and get a database connection string, you need to have a successful deployment. Set up a GitHub repository and push your project to it. |
| 61 | + |
| 62 | +## 2. Deploy the project to Deno Deploy |
| 63 | + |
| 64 | +Deploy your repository to Deno Deploy. Any subsequent commits will trigger automatic redeployments. You need to deploy now, as the database string requires a successful deployment to generate. |
| 65 | + |
| 66 | +1. Navigate to the [Deno Deploy dashboard](https://dash.deno.com/) and select **New App** |
| 67 | +2. Configure GitHub app permissions by following GitHub's prompts |
| 68 | +3. Choose your GitHub repository in the Deno Deploy interface |
| 69 | +4. Click **Create App** to complete the deployment |
| 70 | + |
| 71 | +The application will deploy automatically. |
| 72 | + |
| 73 | +## 3. Provision a Prisma Postgres database |
| 74 | + |
| 75 | +Provision a Prisma Postgres database in Deno Deploy, and link it to your application: |
| 76 | + |
| 77 | +1. Go to the **Databases** section in the Deno Deploy dashboard |
| 78 | +2. Select **Provision Database** and choose **Prisma Postgres** |
| 79 | +3. Complete the database configuration and confirm provisioning |
| 80 | +4. Click **Assign** and select your application |
| 81 | +5. Copy the **Production connection string** |
| 82 | +6. Add the connection string to your `.env` file: |
| 83 | + |
| 84 | +```env |
| 85 | +DATABASE_URL="postgresql://<username>:<password>@db.prisma.io:5432/<database_name>-production" |
| 86 | +``` |
| 87 | + |
| 88 | +## 4. Configure Prisma ORM |
| 89 | + |
| 90 | +### 4.1 Enable environment variables |
| 91 | + |
| 92 | +To access the database connection string during local development, configure Deno to load environment variables from your `.env` file using the `--env-file` flag. |
| 93 | + |
| 94 | +Update the `dev` task in `deno.json`: |
| 95 | + |
| 96 | +```json file=deno.json |
| 97 | +{ |
| 98 | + "tasks": { |
| 99 | + // edit-next-line |
| 100 | + "dev": "deno run --watch --env-file main.ts" |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### 4.2 Initialize Prisma and create schema |
| 106 | + |
| 107 | +Install the Prisma Client: |
| 108 | + |
| 109 | +```terminal |
| 110 | +deno install npm:@prisma/client |
| 111 | +``` |
| 112 | + |
| 113 | +Initialize Prisma in your project, which creates the necessary configuration files and folder structure for defining your database models. |
| 114 | + |
| 115 | +```terminal |
| 116 | +npx prisma init |
| 117 | +``` |
| 118 | + |
| 119 | +Update the Prisma schema with these changes: |
| 120 | +1. Change the client output from `prisma-client-js` to `prisma-client`. |
| 121 | +2. Add the Deno runtime configuration. *(This is required for Deno to run properly)* |
| 122 | +3. Add the Log model for storing request information. |
| 123 | + |
| 124 | +```prisma file=prisma/schema.prisma |
| 125 | +generator client { |
| 126 | + // edit-next-line |
| 127 | + provider = "prisma-client" |
| 128 | + output = "../generated/prisma" |
| 129 | + // add-next-line |
| 130 | + runtime = "deno" |
| 131 | +} |
| 132 | +
|
| 133 | +datasource db { |
| 134 | + provider = "postgresql" |
| 135 | + url = env("DATABASE_URL") |
| 136 | +} |
| 137 | +
|
| 138 | +// add-start |
| 139 | +model Log { |
| 140 | + id Int @id @default(autoincrement()) |
| 141 | + level Level |
| 142 | + message String |
| 143 | + meta Json |
| 144 | +} |
| 145 | +
|
| 146 | +enum Level { |
| 147 | + Info |
| 148 | + Warn |
| 149 | + Error |
| 150 | +} |
| 151 | +// add-end |
| 152 | +``` |
| 153 | + |
| 154 | +### 4.3 Generate and apply migrations |
| 155 | + |
| 156 | +Migrations create the actual database tables based on your Prisma schema. This command generates SQL migration files and executes them against your database, creating the `Log` table with the specified fields. |
| 157 | + |
| 158 | +```terminal |
| 159 | +deno run -A npm:prisma migrate dev --name init |
| 160 | +``` |
| 161 | + |
| 162 | +## 5. Update the application to use Prisma |
| 163 | + |
| 164 | +Now that the database is configured, update your application to interact with it. This implementation creates a logging system that captures every HTTP request, stores it in the database, and returns the logged entry as JSON. |
| 165 | + |
| 166 | +```tsx file=main.ts |
| 167 | +import { PrismaClient } from "./generated/prisma/client.ts"; |
| 168 | +import process from "node:process"; |
| 169 | + |
| 170 | +const prisma = new PrismaClient({ |
| 171 | + datasources: { |
| 172 | + db: { |
| 173 | + url: process.env.DATABASE_URL, |
| 174 | + }, |
| 175 | + }, |
| 176 | +}); |
| 177 | + |
| 178 | +async function handler(request: Request) { |
| 179 | + const url = new URL(request.url); |
| 180 | + if (url.pathname === "/favicon.ico") { |
| 181 | + return new Response(null, { status: 204 }); |
| 182 | + } |
| 183 | + |
| 184 | + const log = await prisma.log.create({ |
| 185 | + data: { |
| 186 | + level: "Info", |
| 187 | + message: `${request.method} ${request.url}`, |
| 188 | + meta: { |
| 189 | + headers: JSON.stringify(request.headers), |
| 190 | + }, |
| 191 | + }, |
| 192 | + }); |
| 193 | + const body = JSON.stringify(log, null, 2); |
| 194 | + return new Response(body, { |
| 195 | + headers: { "content-type": "application/json; charset=utf-8" }, |
| 196 | + }); |
| 197 | +} |
| 198 | + |
| 199 | +Deno.serve(handler); |
| 200 | +``` |
| 201 | + |
| 202 | +Test the application locally by running: |
| 203 | + |
| 204 | +```terminal |
| 205 | +deno run dev |
| 206 | +``` |
| 207 | + |
| 208 | +:::note |
| 209 | + |
| 210 | +It may ask you for access to your environment variables. Select **Allow** to grant access. |
| 211 | + |
| 212 | +::: |
| 213 | + |
| 214 | +Visit `localhost:8000` in your browser to see the application running. You should see a JSON response containing the log entry: |
| 215 | + |
| 216 | +```json |
| 217 | +{ |
| 218 | + "id": 1, |
| 219 | + "level": "Info", |
| 220 | + "message": "GET http://localhost:8000/", |
| 221 | + "meta": { |
| 222 | + "headers": "..." |
| 223 | + } |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +## 6. Deploy the application |
| 228 | + |
| 229 | +The build command must generate the Prisma Client code to ensure it is available in production. |
| 230 | + |
| 231 | +### 6.1 Update build command in Deno Deploy |
| 232 | + |
| 233 | +1. Go to the application in Deno Deploy and click **Settings** |
| 234 | +2. Under **Build configuration**, hit **Edit** and add `deno run -A npm:prisma generate` to the build command |
| 235 | +3. Click **Save** |
| 236 | + |
| 237 | +### 6.2 Push changes to GitHub |
| 238 | + |
| 239 | +Commit and push your changes to trigger an automatic deployment: |
| 240 | + |
| 241 | +```terminal |
| 242 | +git add . |
| 243 | +git commit -m "added prisma" |
| 244 | +git push |
| 245 | +``` |
| 246 | + |
| 247 | +Navigate back to Deno Deploy and you should see a successful build. Once deployed, click the deployment URL at the top right of the dashboard. |
| 248 | + |
| 249 | +### 6.3 Verify the deployment |
| 250 | + |
| 251 | +When you visit your deployed application, you should see a response that looks like this: |
| 252 | + |
| 253 | +```json |
| 254 | +{ |
| 255 | + "id": 1, |
| 256 | + "level": "Info", |
| 257 | + "message": "GET https://prisma-postgres-deno-deploy.<org-name>.deno.net/", |
| 258 | + "meta": { |
| 259 | + "headers": "{}" |
| 260 | + } |
| 261 | +} |
| 262 | +``` |
| 263 | + |
| 264 | +You're done! Each time you refresh the page, a new log entry is created in your database. |
| 265 | + |
| 266 | +## Next Steps |
| 267 | + |
| 268 | +Now that you have a working Deno app connected to a Prisma Postgres database, you can: |
| 269 | + |
| 270 | +- **Enhance your data model** - Add relationships, validations, and indexes to your Prisma schema |
| 271 | +- **Secure your API** - Implement authentication, rate limiting, and proper error handling |
| 272 | +- **Improve deployment** - Set up CI/CD, monitoring, and database backups for production |
| 273 | + |
| 274 | +## More info |
| 275 | + |
| 276 | +- [Prisma ORM Documentation](/orm/overview/introduction) |
| 277 | +- [Deno Deploy Documentation](https://docs.deno.com/deploy) |
0 commit comments