|
| 1 | +# Cloudflare Workers Example |
| 2 | + |
| 3 | +This example shows how to implement a simple API using [Cloudflare Workers](https://workers.cloudflare.com) and [Prisma ORM](https://www.prisma.io/docs). The example creates a serverless worker that interacts with a PostgreSQL database to create and count users. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### 1. Download the example and navigate to the project directory |
| 8 | + |
| 9 | +Download this example: |
| 10 | + |
| 11 | +``` |
| 12 | +npx try-prisma@latest --template orm/cloudflare-workers |
| 13 | +``` |
| 14 | + |
| 15 | +Then navigate to the project directory |
| 16 | + |
| 17 | +``` |
| 18 | +cd cloudflare-workers |
| 19 | +``` |
| 20 | + |
| 21 | +<details><summary><strong>Alternative:</strong> Clone the entire repo</summary> |
| 22 | + |
| 23 | +Clone this repository: |
| 24 | + |
| 25 | +``` |
| 26 | +git clone git@github.com:prisma/prisma-examples.git --depth=1 |
| 27 | +``` |
| 28 | + |
| 29 | +Install npm dependencies: |
| 30 | + |
| 31 | +``` |
| 32 | +cd prisma-examples/orm/cloudflare-workers |
| 33 | +npm install |
| 34 | +``` |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +### 2. Create a Prisma Postgres instance |
| 39 | + |
| 40 | +This example uses a [Prisma Postgres](https://prisma.io/postgres) database by default. To get started with the project, you will need to setup a Prisma Postgres connection string: |
| 41 | + |
| 42 | +1. Set up a new Prisma Postgres instance in the [Prisma Data Platform Console](https://console.prisma.io) and copy the database connection URL. |
| 43 | + |
| 44 | +2. Add your database url to the `.env` |
| 45 | + |
| 46 | +That's it, your project is now configured to use Prisma Postgres! |
| 47 | + |
| 48 | + |
| 49 | +### 3. Generate Prisma Client |
| 50 | + |
| 51 | +Run the following command to generate the Prisma Client. This is what you will be using to interact with your database. |
| 52 | + |
| 53 | +``` |
| 54 | +npm run generate |
| 55 | +``` |
| 56 | + |
| 57 | +### 4. Start the Cloudflare Worker |
| 58 | + |
| 59 | +``` |
| 60 | +npm run dev |
| 61 | +``` |
| 62 | + |
| 63 | +The server is now running at http://localhost:8788 |
| 64 | + |
| 65 | +### 5. Test the API |
| 66 | + |
| 67 | +The API supports full CRUD operations. Here are some example `curl` commands: |
| 68 | + |
| 69 | +**Create a user:** |
| 70 | +```bash |
| 71 | +curl -X POST http://localhost:8788/users \ |
| 72 | + -H "Content-Type: application/json" \ |
| 73 | + -d '{"email":"john@example.com","name":"John Doe"}' |
| 74 | +``` |
| 75 | + |
| 76 | +**Get all users:** |
| 77 | +```bash |
| 78 | +curl http://localhost:8788/users |
| 79 | +``` |
| 80 | + |
| 81 | +**Get a specific user:** |
| 82 | +```bash |
| 83 | +curl http://localhost:8788/users/1 |
| 84 | +``` |
| 85 | + |
| 86 | +**Update a user:** |
| 87 | +```bash |
| 88 | +curl -X PUT http://localhost:8788/users/1 \ |
| 89 | + -H "Content-Type: application/json" \ |
| 90 | + -d '{"name":"John Updated"}' |
| 91 | +``` |
| 92 | + |
| 93 | +**Delete a user:** |
| 94 | +```bash |
| 95 | +curl -X DELETE http://localhost:8788/users/1 |
| 96 | +``` |
| 97 | + |
| 98 | +## Next steps |
| 99 | + |
| 100 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 101 | +- Share your feedback on the [Prisma Discord](https://pris.ly/discord/) |
| 102 | +- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) |
0 commit comments