|
1 | | -# Prisma Postgres Example: Deno Deploy (Deno 2, ESM) |
| 1 | +# Prisma + Deno Deploy Example |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ## Prerequisites |
6 | 6 |
|
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 |
10 | 9 |
|
11 | 10 | ## Tech Stack |
12 | 11 |
|
13 | 12 | - 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 |
27 | 15 |
|
28 | | -### 1. Clone the repository |
| 16 | +## Getting Started |
29 | 17 |
|
30 | | -Clone the repository, navigate into it and install dependencies: |
| 18 | +### 1. Clone the repository |
31 | 19 |
|
32 | | -``` |
| 20 | +```bash |
33 | 21 | git clone git@github.com:prisma/prisma-examples.git --depth=1 |
34 | 22 | cd prisma-examples/generator-prisma-client/deno-deploy |
35 | | -deno install |
36 | 23 | ``` |
37 | 24 |
|
38 | | -### 2. Configure environment variables |
39 | | - |
40 | | -Create a `.env` in the root of the project directory: |
| 25 | +### 2. Install dependencies |
41 | 26 |
|
42 | 27 | ```bash |
43 | | -touch .env |
| 28 | +deno install |
| 29 | +deno install --allow-scripts |
44 | 30 | ``` |
45 | 31 |
|
46 | | -Now, open the `.env` file and set the `DATABASE_URL` environment variable with your Prisma Postgres connection string: |
| 32 | +### 3. Configure environment variables |
47 | 33 |
|
48 | | -```bash |
49 | | -# .env |
| 34 | +Create a `.env` file with your Prisma Postgres connection string: |
50 | 35 |
|
51 | | -DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__" |
| 36 | +```bash |
| 37 | +cp .env.example .env |
52 | 38 | ``` |
53 | 39 |
|
54 | | -Replace `__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__` with your actual Prisma Postgres connection string. |
| 40 | +Then edit `.env` and set your `DATABASE_URL`: |
55 | 41 |
|
56 | | -### 3. Generate Prisma Client |
| 42 | +```env |
| 43 | +DATABASE_URL="postgresql://user:password@db.prisma.io:5432/database" |
| 44 | +``` |
57 | 45 |
|
58 | | -Run: |
| 46 | +### 4. Push schema and generate client |
59 | 47 |
|
60 | | -``` |
61 | | -deno task prisma generate |
| 48 | +```bash |
| 49 | +deno task db:push |
62 | 50 | ``` |
63 | 51 |
|
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. |
65 | 53 |
|
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 |
67 | 55 |
|
68 | | -``` |
69 | | -deno task prisma migrate dev --name init |
| 56 | +```bash |
| 57 | +deno task dev |
70 | 58 | ``` |
71 | 59 |
|
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`. |
73 | 61 |
|
74 | | -``` |
75 | | -deno task prisma db seed |
76 | | -``` |
| 62 | +### 6. Test the API |
77 | 63 |
|
78 | | -### 5. Start the app |
| 64 | +```bash |
| 65 | +# Get API info |
| 66 | +curl http://localhost:8000/ |
79 | 67 |
|
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"}' |
81 | 72 |
|
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 |
84 | 83 | ``` |
85 | 84 |
|
86 | | -### 6. Deploy to Deno Deploy |
| 85 | +## API Endpoints |
87 | 86 |
|
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 | |
89 | 95 |
|
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 |
91 | 110 | 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 |
93 | 112 | ``` |
94 | 113 |
|
95 | 114 | ## Resources |
96 | 115 |
|
97 | 116 | - [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) |
0 commit comments