Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generator-prisma-client/deno-deploy/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DATABASE_URL="prisma+postgres://..."
DATABASE_URL="postgresql://user:password@db.prisma.io:5432/database"
5 changes: 4 additions & 1 deletion generator-prisma-client/deno-deploy/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
src/generated/
.env
node_modules/
generated/
deno.lock
129 changes: 73 additions & 56 deletions generator-prisma-client/deno-deploy/README.md
Original file line number Diff line number Diff line change
@@ -1,102 +1,119 @@
# Prisma Postgres Example: Deno Deploy (Deno 2, ESM)
# Prisma + Deno Deploy Example

This project showcases how to use the Prisma ORM with Prisma Postgres in an ESM Deno Deploy application.
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.

## Prerequisites

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

- 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.
- [Deno](https://docs.deno.com/runtime/#install-deno) v2.0 or later installed
- A [Prisma Data Platform](https://console.prisma.io/login) account for Prisma Postgres

## Tech Stack

- Deno 2
- ESM
- Prisma Client with the `prisma-client` generator
See the [Prisma schema file](./prisma/schema.prisma) for details.

```prisma
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
runtime = "deno"
}
```

## Getting started
- Prisma Client with the `prisma-client` generator and Deno runtime
- Prisma Postgres

### 1. Clone the repository
## Getting Started

Clone the repository, navigate into it and install dependencies:
### 1. Clone the repository

```
```bash
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:
### 2. Install dependencies

```bash
touch .env
deno install
deno install --allow-scripts
```

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

```bash
# .env
Create a `.env` file with your Prisma Postgres connection string:

DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__"
```bash
cp .env.example .env
```

Replace `__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__` with your actual Prisma Postgres connection string.
Then edit `.env` and set your `DATABASE_URL`:

### 3. Generate Prisma Client
```env
DATABASE_URL="postgresql://user:password@db.prisma.io:5432/database"
```

Run:
### 4. Push schema and generate client

```
deno task prisma generate
```bash
deno task db:push
```

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

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:
### 5. Start the development server

```
deno task prisma migrate dev --name init
```bash
deno task dev
```

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:
The API will be available at `http://localhost:8000`.

```
deno task prisma db seed
```
### 6. Test the API

### 5. Start the app
```bash
# Get API info
curl http://localhost:8000/

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

```
deno task dev
# List all tasks
curl http://localhost:8000/tasks

# Update a task
curl -X PATCH http://localhost:8000/tasks/1 \
-H "Content-Type: application/json" \
-d '{"completed": true}'

# Delete a task
curl -X DELETE http://localhost:8000/tasks/1
```

### 6. Deploy to Deno Deploy
## API Endpoints

To deploy your application to Deno Deploy, follow these steps:
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/` | API info |
| GET | `/tasks` | List all tasks |
| POST | `/tasks` | Create a new task |
| GET | `/tasks/:id` | Get a specific task |
| PATCH | `/tasks/:id` | Update a task |
| DELETE | `/tasks/:id` | Delete a task |

```
## Deploy to Deno Deploy

1. Push your code to GitHub
2. Go to [dash.deno.com](https://dash.deno.com/) and create a new project
3. Configure the deployment:
- **Install command**: `deno install`
- **Build command**: `deno run -A npm:prisma generate`
- **Entrypoint**: `src/main.ts`
4. Add `DATABASE_URL` environment variable in Settings
5. Deploy!

Or use the Deno Deploy CLI:

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

## Resources

- [Prisma Postgres documentation](https://www.prisma.io/docs/postgres)
- Check out the [Prisma docs](https://www.prisma.io/docs)
- [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.
- [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.
- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates.
- Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section).
- [Deploy to Deno Deploy guide](https://www.prisma.io/docs/orm/prisma-client/deployment/edge/deploy-to-deno-deploy)
- [Prisma documentation](https://www.prisma.io/docs)
- [Join our Discord community](https://pris.ly/discord)
23 changes: 23 additions & 0 deletions generator-prisma-client/deno-deploy/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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 src/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"
},
"deploy": {
"exclude": ["**/node_modules"],
"entrypoint": "src/main.ts"
}
}
26 changes: 0 additions & 26 deletions generator-prisma-client/deno-deploy/deno.jsonc

This file was deleted.

16 changes: 6 additions & 10 deletions generator-prisma-client/deno-deploy/prisma.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { defineConfig, env } from 'prisma/config'

// Note: this wouldn't be needed if `deno task --env-file=.env ...` was supported.
// See: https://github.com/denoland/deno/issues/27236
import 'dotenv/config'
import { defineConfig } from "prisma/config";
import process from "node:process";

export default defineConfig({
schema: './prisma/schema.prisma',
schema: "./prisma/schema.prisma",
migrations: {
path: './prisma/migrations',
seed: 'deno run --allow-all ./prisma/seed.ts',
path: "./prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
url: process.env["DATABASE_URL"],
},
})
});
23 changes: 10 additions & 13 deletions generator-prisma-client/deno-deploy/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
generator client {
provider = "prisma-client"
output = "./generated/"
runtime = "deno"
provider = "prisma-client"
output = "../generated/prisma"
runtime = "deno"
}

datasource db {
provider = "postgresql"
}

enum QuoteKind {
Fact
Opinion
}

model Quotes {
id Int @id @default(autoincrement())
quote String
kind QuoteKind @default(Opinion)
createdAt DateTime @default(now())
model Task {
id Int @id @default(autoincrement())
title String
description String?
completed Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Loading
Loading