Skip to content

Commit 1f19b94

Browse files
improve deno example (#8400)
1 parent 8cd7783 commit 1f19b94

11 files changed

Lines changed: 244 additions & 264 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DATABASE_URL="prisma+postgres://..."
1+
DATABASE_URL="postgresql://user:password@db.prisma.io:5432/database"
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
src/generated/
1+
.env
2+
node_modules/
3+
generated/
4+
deno.lock
Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,119 @@
1-
# Prisma Postgres Example: Deno Deploy (Deno 2, ESM)
1+
# Prisma + Deno Deploy Example
22

3-
This project showcases how to use the Prisma ORM with Prisma Postgres in an ESM Deno Deploy application.
3+
This project showcases how to use Prisma ORM with Prisma Postgres in a Deno Deploy application. It implements a simple Task API with full CRUD operations.
44

55
## Prerequisites
66

7-
To successfully run the project, you will need the following:
8-
9-
- A **Prisma Postgres** connection string. You can create a project in your [Prisma Data Platform](https://pris.ly/pdp) account and enable Postgres to obtain one.
7+
- [Deno](https://docs.deno.com/runtime/#install-deno) v2.0 or later installed
8+
- A [Prisma Data Platform](https://console.prisma.io/login) account for Prisma Postgres
109

1110
## Tech Stack
1211

1312
- Deno 2
14-
- ESM
15-
- Prisma Client with the `prisma-client` generator
16-
See the [Prisma schema file](./prisma/schema.prisma) for details.
17-
18-
```prisma
19-
generator client {
20-
provider = "prisma-client"
21-
output = "../src/generated/prisma"
22-
runtime = "deno"
23-
}
24-
```
25-
26-
## Getting started
13+
- Prisma Client with the `prisma-client` generator and Deno runtime
14+
- Prisma Postgres
2715

28-
### 1. Clone the repository
16+
## Getting Started
2917

30-
Clone the repository, navigate into it and install dependencies:
18+
### 1. Clone the repository
3119

32-
```
20+
```bash
3321
git clone git@github.com:prisma/prisma-examples.git --depth=1
3422
cd prisma-examples/generator-prisma-client/deno-deploy
35-
deno install
3623
```
3724

38-
### 2. Configure environment variables
39-
40-
Create a `.env` in the root of the project directory:
25+
### 2. Install dependencies
4126

4227
```bash
43-
touch .env
28+
deno install
29+
deno install --allow-scripts
4430
```
4531

46-
Now, open the `.env` file and set the `DATABASE_URL` environment variable with your Prisma Postgres connection string:
32+
### 3. Configure environment variables
4733

48-
```bash
49-
# .env
34+
Create a `.env` file with your Prisma Postgres connection string:
5035

51-
DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__"
36+
```bash
37+
cp .env.example .env
5238
```
5339

54-
Replace `__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__` with your actual Prisma Postgres connection string.
40+
Then edit `.env` and set your `DATABASE_URL`:
5541

56-
### 3. Generate Prisma Client
42+
```env
43+
DATABASE_URL="postgresql://user:password@db.prisma.io:5432/database"
44+
```
5745

58-
Run:
46+
### 4. Push schema and generate client
5947

60-
```
61-
deno task prisma generate
48+
```bash
49+
deno task db:push
6250
```
6351

64-
### 4. Run a migration to create the database structure and seed the database
52+
This creates the `Task` table and generates the Prisma Client.
6553

66-
The [Prisma schema file](./prisma/schema.prisma) 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:
54+
### 5. Start the development server
6755

68-
```
69-
deno task prisma migrate dev --name init
56+
```bash
57+
deno task dev
7058
```
7159

72-
You now have an empty `Quotes` table in your database. Next, run the [seed script](./prisma/seed.ts) to create some sample records in the table:
60+
The API will be available at `http://localhost:8000`.
7361

74-
```
75-
deno task prisma db seed
76-
```
62+
### 6. Test the API
7763

78-
### 5. Start the app
64+
```bash
65+
# Get API info
66+
curl http://localhost:8000/
7967

80-
You can run the app with the following command:
68+
# Create a task
69+
curl -X POST http://localhost:8000/tasks \
70+
-H "Content-Type: application/json" \
71+
-d '{"title": "Learn Prisma", "description": "Complete the Deno guide"}'
8172

82-
```
83-
deno task dev
73+
# List all tasks
74+
curl http://localhost:8000/tasks
75+
76+
# Update a task
77+
curl -X PATCH http://localhost:8000/tasks/1 \
78+
-H "Content-Type: application/json" \
79+
-d '{"completed": true}'
80+
81+
# Delete a task
82+
curl -X DELETE http://localhost:8000/tasks/1
8483
```
8584

86-
### 6. Deploy to Deno Deploy
85+
## API Endpoints
8786

88-
To deploy your application to Deno Deploy, follow these steps:
87+
| Method | Endpoint | Description |
88+
|--------|----------|-------------|
89+
| GET | `/` | API info |
90+
| GET | `/tasks` | List all tasks |
91+
| POST | `/tasks` | Create a new task |
92+
| GET | `/tasks/:id` | Get a specific task |
93+
| PATCH | `/tasks/:id` | Update a task |
94+
| DELETE | `/tasks/:id` | Delete a task |
8995

90-
```
96+
## Deploy to Deno Deploy
97+
98+
1. Push your code to GitHub
99+
2. Go to [dash.deno.com](https://dash.deno.com/) and create a new project
100+
3. Configure the deployment:
101+
- **Install command**: `deno install`
102+
- **Build command**: `deno run -A npm:prisma generate`
103+
- **Entrypoint**: `src/main.ts`
104+
4. Add `DATABASE_URL` environment variable in Settings
105+
5. Deploy!
106+
107+
Or use the Deno Deploy CLI:
108+
109+
```bash
91110
deno install -gArf jsr:@deno/deployctl
92-
deployctl deploy --project=prisma-client-deno-deploy --env-file=.env
111+
deployctl deploy --project=your-project-name --env-file=.env
93112
```
94113

95114
## Resources
96115

97116
- [Prisma Postgres documentation](https://www.prisma.io/docs/postgres)
98-
- Check out the [Prisma docs](https://www.prisma.io/docs)
99-
- [Join our community on Discord](https://pris.ly/discord?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) to share feedback and interact with other users.
100-
- [Subscribe to our YouTube channel](https://pris.ly/youtube?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for live demos and video tutorials.
101-
- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates.
102-
- Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section).
117+
- [Deploy to Deno Deploy guide](https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-deno-deploy)
118+
- [Prisma documentation](https://www.prisma.io/docs)
119+
- [Join our Discord community](https://pris.ly/discord)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"nodeModulesDir": "auto",
3+
"compilerOptions": {
4+
"lib": ["deno.window"],
5+
"types": ["node"]
6+
},
7+
"imports": {
8+
"@prisma/adapter-pg": "npm:@prisma/adapter-pg@^7.0.0",
9+
"@prisma/client": "npm:@prisma/client@^7.0.0",
10+
"prisma": "npm:prisma@^7.0.0"
11+
},
12+
"tasks": {
13+
"dev": "deno run -A --env=.env --watch src/main.ts",
14+
"db:generate": "deno run -A --env=.env npm:prisma generate",
15+
"db:push": "deno run -A --env=.env npm:prisma db push",
16+
"db:migrate": "deno run -A --env=.env npm:prisma migrate dev",
17+
"db:seed": "deno run -A --env=.env npm:prisma db seed"
18+
},
19+
"deploy": {
20+
"exclude": ["**/node_modules"],
21+
"entrypoint": "src/main.ts"
22+
}
23+
}

generator-prisma-client/deno-deploy/deno.jsonc

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import { defineConfig, env } from 'prisma/config'
2-
3-
// Note: this wouldn't be needed if `deno task --env-file=.env ...` was supported.
4-
// See: https://github.com/denoland/deno/issues/27236
5-
import 'dotenv/config'
1+
import { defineConfig } from "prisma/config";
2+
import process from "node:process";
63

74
export default defineConfig({
8-
schema: './prisma/schema.prisma',
5+
schema: "./prisma/schema.prisma",
96
migrations: {
10-
path: './prisma/migrations',
11-
seed: 'deno run --allow-all ./prisma/seed.ts',
7+
path: "./prisma/migrations",
128
},
139
datasource: {
14-
url: env("DATABASE_URL"),
10+
url: process.env["DATABASE_URL"],
1511
},
16-
})
12+
});
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
generator client {
2-
provider = "prisma-client"
3-
output = "./generated/"
4-
runtime = "deno"
2+
provider = "prisma-client"
3+
output = "../generated/prisma"
4+
runtime = "deno"
55
}
66

77
datasource db {
88
provider = "postgresql"
99
}
1010

11-
enum QuoteKind {
12-
Fact
13-
Opinion
14-
}
15-
16-
model Quotes {
17-
id Int @id @default(autoincrement())
18-
quote String
19-
kind QuoteKind @default(Opinion)
20-
createdAt DateTime @default(now())
11+
model Task {
12+
id Int @id @default(autoincrement())
13+
title String
14+
description String?
15+
completed Boolean @default(false)
16+
createdAt DateTime @default(now())
17+
updatedAt DateTime @updatedAt
2118
}

0 commit comments

Comments
 (0)