apps/docs/content/docs/guides/runtimes/deno.mdx
Deno is a secure JavaScript and TypeScript runtime built on V8 with built-in TypeScript support, a permissions system, and web-standard APIs. In this guide, you will set up a Deno project with Prisma ORM and a Prisma Postgres database. You will create a simple HTTP server that reads from the database and returns the results.
<Youtube videoId="hAL2J3yQf7I" title="How to use Prisma ORM with Deno and Prisma Postgres" />First, create a directory for your project and navigate to it:
mkdir deno-prisma
cd deno-prisma
Then, initialize a new Deno project:
deno init
This creates a basic Deno project with a deno.json configuration file and a main.ts file.
Update the deno.json file with the following configuration to set up Node.js compatibility, import maps, and Prisma-related task scripts:
{
"nodeModulesDir": "auto",
"compilerOptions": {
"lib": ["deno.window"],
"types": ["node"]
},
"imports": {
"@prisma/adapter-pg": "npm:@prisma/adapter-pg@^7.0.0",
"@prisma/client": "npm:@prisma/client@^7.0.0",
"prisma": "npm:prisma@^7.0.0"
},
"tasks": {
"dev": "deno run -A --env=.env --watch main.ts",
"db:generate": "deno run -A --env=.env npm:prisma generate",
"db:push": "deno run -A --env=.env npm:prisma db push",
"db:migrate": "deno run -A --env=.env npm:prisma migrate dev",
"db:seed": "deno run -A --env=.env npm:prisma db seed"
}
}
:::info
The nodeModulesDir: "auto" setting allows Deno to automatically manage a node_modules directory, which is needed for Prisma's generated client. The import map entries let you use bare specifiers like @prisma/client in your code.
:::
Initialize Prisma ORM with Prisma Postgres in your project:
deno run -A npm:prisma init --db
:::info
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My Deno Project".
:::
This command creates:
prisma/ directory with your schema.prisma fileprisma.config.ts file.env file with your DATABASE_URLOpen the generated prisma.config.ts file. Since Deno loads environment variables using the --env=.env flag (configured in deno.json tasks), you can remove the dotenv/config import if it was generated:
import "dotenv/config"; // [!code --]
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});
We're going to use a direct connection string for connecting to Prisma Postgres. To get your direct connection string:
postgres://Update your .env file to replace the DATABASE_URL with the new connection string:
DATABASE_URL="your_database_url_here" // [!code --]
DATABASE_URL="your_direct_connection_string_here" // [!code ++]
Open prisma/schema.prisma and update it to include the Deno runtime and your data model:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
runtime = "deno" // [!code ++]
}
datasource db {
provider = "postgresql"
}
model User { // [!code ++]
id Int @id @default(autoincrement()) // [!code ++]
email String @unique // [!code ++]
name String? // [!code ++]
} // [!code ++]
:::note
The runtime = "deno" setting in the generator block is required for Prisma Client to work correctly with the Deno runtime.
:::
Generate the Prisma Client and apply your schema to the database:
deno task db:migrate --name init
deno task db:generate
This command:
generated/prisma directoryCreate a db.ts file in your project root to configure PrismaClient:
import { PrismaClient } from "./generated/prisma/client.ts";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({
connectionString: Deno.env.get("DATABASE_URL")!,
});
export const prisma = new PrismaClient({
adapter,
});
Create a seed script in the prisma folder to populate your database with sample data:
import { PrismaClient } from "../generated/prisma/client.ts";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({
connectionString: Deno.env.get("DATABASE_URL")!,
});
const prisma = new PrismaClient({
adapter,
});
async function main() {
// Create multiple users
await prisma.user.createMany({
data: [
{ email: "[email protected]", name: "Alice" },
{ email: "[email protected]", name: "Bob" },
{ email: "[email protected]", name: "Charlie" },
{ email: "[email protected]", name: "Diana" },
{ email: "[email protected]", name: "Eve" },
{ email: "[email protected]", name: "Frank" },
{ email: "[email protected]", name: "Grace" },
{ email: "[email protected]", name: "Henry" },
{ email: "[email protected]", name: "Isabella" },
{ email: "[email protected]", name: "Jack" },
],
skipDuplicates: true, // prevents errors if you run the seed multiple times
});
console.log("Seed data inserted!");
}
main()
.catch((e) => {
console.error(e);
Deno.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Update the prisma.config.ts file to include the seed command:
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
seed: "deno run -A --env=.env ./prisma/seed.ts", // [!code ++]
},
datasource: {
url: env("DATABASE_URL"),
},
});
Run the seed script to populate your database:
deno task db:seed
Replace the main.ts file contents with the following code to build a simple HTTP server that uses Prisma ORM to fetch and display users:
import { prisma } from "./db.ts";
async function handler(req: Request): Promise<Response> {
const { pathname } = new URL(req.url);
// Skip favicon route
if (pathname === "/favicon.ico") {
return new Response(null, { status: 204 });
}
// Return all users
const users = await prisma.user.findMany();
// Count all users
const count = await prisma.user.count();
// Format the response with JSON
return new Response(
JSON.stringify({
users: users,
totalUsers: count,
}),
{ headers: { "Content-Type": "application/json" } },
);
}
Deno.serve({ port: 8000 }, handler);
Start your Deno server:
deno task dev
You should see the server running on http://localhost:8000 in the console. When you visit http://localhost:8000 in your browser, you'll see a JSON response with all the users in your database and the total count.
Now that you have a Deno application connected to a Prisma Postgres database, you can continue by: