deploy/classic/prisma-postgres.md
:::info Legacy Documentation
You are viewing legacy documentation for Deno Deploy Classic. We recommend migrating to the new <a href="/deploy/">Deno Deploy</a> platform.
:::
This tutorial covers how to connect to a Prisma Postgres database from an application deployed on Deno Deploy.
There are several ways to set up a Prisma Postgre database for your Prisma project. This guide covers the most common approaches.
Run the following command to initialize a new Prisma project with a database:
npx prisma init --db
This will prompt you to select your preferred region and database name. Once
completed, you'll find the DATABASE_URL connection string in your .env file.
npx create-dbAlternatively, you can use the dedicated database creation tool:
npx create-db@latest
This command will provide you with two connection strings tied to the same database:
Prisma ORM optimized connection string:
prisma+postgres://accelerate.prisma-data.net/?api_key=<api_key>
Standard Prisma Postgres connection string:
postgresql://<username>:<password>@db.prisma.io:5432/postgres
In order to keep the database created with npx create-db, you must follow
through with the claim process. That can be done via the claim link provided in
the terminal.
The Prisma ORM optimized connection string (prisma+postgres://) only works
with the Prisma ORM, while the standard Prisma Postgre connection string can be
used with other database tools and libraries.
Next, let's create a project in Deno Deploy Classic and set it up with the requisite environment variables:
DATABASE_URL - The value should be set to the connection string you saved in
the last step.Now that you have your database set up, let's create a simple application that connects to the Prisma Postgres database using Prisma ORM.
First, install the required dependencies:
deno install npm:@prisma/client
deno install npm:@prisma/extension-accelerate
deno install npm:dotenv-cli
:::note
The dotenv-cli package is needed because Prisma Client doesn't read .env
files by default on Deno.
:::
With your database connection configured, you can now apply the data model to your database:
deno run -A npm:prisma migrate dev --name init
This command creates a new SQL migration file and runs it against your database.
Edit your prisma/schema.prisma file to define a Log model and configure it
for Deno:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
runtime = "deno"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Log {
id Int @id @default(autoincrement())
level Level
message String
meta Json
}
enum Level {
Info
Warn
Error
}
Create index.ts in your project root with the following content:
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { withAccelerate } from "npm:@prisma/extension-accelerate";
import { PrismaClient } from "./generated/prisma/client.ts";
const prisma = new PrismaClient().$extends(withAccelerate());
async function handler(request: Request) {
// Ignore /favicon.ico requests:
const url = new URL(request.url);
if (url.pathname === "/favicon.ico") {
return new Response(null, { status: 204 });
}
const log = await prisma.log.create({
data: {
level: "Info",
message: `${request.method} ${request.url}`,
meta: {
headers: JSON.stringify(request.headers),
},
},
});
const body = JSON.stringify(log, null, 2);
return new Response(body, {
headers: { "content-type": "application/json; charset=utf-8" },
});
}
serve(handler);
Start your application locally to test the database connection:
npx dotenv -- deno run -A ./index.ts
Visit http://localhost:8000 in your browser. Each request will create a new
log entry in your database and return the log data as JSON.
Once you have finished writing your application, you can deploy it on Deno Deploy Classic.
To do this, go back to your project page at
https://dash.deno.com/projects/<project-name>.
You should see a couple of options to deploy:
deployctl
deployctl deploy --project=<project-name> <application-file-name>
Unless you want to add a build step, we recommend that you select the GitHub integration.
For more details on the different ways to deploy on Deno Deploy Classic and the different configuration options, read here.