|
| 1 | +# React Router 7 Example |
| 2 | + |
| 3 | +This example shows how to implement a simple web app using [React Router 7](https://reactrouter.com) and [Prisma ORM](https://www.prisma.io/docs). This example was bootstrapped using the React Router CLI tool `create-react-router`. |
| 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/react-router-7 |
| 13 | +``` |
| 14 | + |
| 15 | +Then navigate to the project directory |
| 16 | + |
| 17 | +``` |
| 18 | +cd react-router-7 |
| 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/nextjs |
| 33 | +npm install |
| 34 | +``` |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +#### [Optional] Switch database to Prisma Postgres |
| 39 | + |
| 40 | +This example uses a local SQLite database by default. If you want to use to [Prisma Postgres](https://prisma.io/postgres), follow these instructions (otherwise, skip to the next step): |
| 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 | +2. Update the `datasource` block to use `postgresql` as the `provider` and paste the database connection URL as the value for `url`: |
| 44 | + |
| 45 | + ```prisma |
| 46 | + datasource db { |
| 47 | + provider = "postgresql" |
| 48 | + url = "prisma+postgres://accelerate.prisma-data.net/?api_key=ey...." |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | + > **Note**: In production environments, we recommend that you set your connection URL via an [environment variable](https://www.prisma.io/docs/orm/more/development-environment/environment-variables/managing-env-files-and-setting-variables), e.g. using a `.env` file. |
| 53 | +
|
| 54 | +3. Install the Prisma Accelerate extension: |
| 55 | + ``` |
| 56 | + npm install @prisma/extension-accelerate |
| 57 | + ``` |
| 58 | +4. Add the Accelerate extension to the `PrismaClient` instance: |
| 59 | + |
| 60 | + ```diff |
| 61 | + + import { withAccelerate } from "@prisma/extension-accelerate" |
| 62 | + |
| 63 | + + const prisma = new PrismaClient().$extends(withAccelerate()) |
| 64 | + ``` |
| 65 | + |
| 66 | +That's it, your project is now configured to use Prisma Postgres! |
| 67 | + |
| 68 | +### 2. Generate Prisma Client |
| 69 | + |
| 70 | +Run the following command to generate the Prisma Client. This is what you will be using to interact with your database. |
| 71 | + |
| 72 | +``` |
| 73 | +npx prisma generate |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. Start the React Router 7 server |
| 77 | + |
| 78 | +``` |
| 79 | +npm run dev |
| 80 | +``` |
| 81 | + |
| 82 | +The server is now running at http://localhost:5173. You can now view your todo list! |
| 83 | + |
| 84 | +## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB) |
| 85 | + |
| 86 | +If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block. |
| 87 | + |
| 88 | +Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls). |
| 89 | + |
| 90 | +<details><summary>Expand for an overview of example configurations with different databases</summary> |
| 91 | + |
| 92 | +### PostgreSQL |
| 93 | + |
| 94 | +For PostgreSQL, the connection URL has the following structure: |
| 95 | + |
| 96 | +```prisma |
| 97 | +datasource db { |
| 98 | + provider = "postgresql" |
| 99 | + url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Here is an example connection string with a local PostgreSQL database: |
| 104 | + |
| 105 | +```prisma |
| 106 | +datasource db { |
| 107 | + provider = "postgresql" |
| 108 | + url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public" |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### MySQL |
| 113 | + |
| 114 | +For MySQL, the connection URL has the following structure: |
| 115 | + |
| 116 | +```prisma |
| 117 | +datasource db { |
| 118 | + provider = "mysql" |
| 119 | + url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE" |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Here is an example connection string with a local MySQL database: |
| 124 | + |
| 125 | +```prisma |
| 126 | +datasource db { |
| 127 | + provider = "mysql" |
| 128 | + url = "mysql://janedoe:mypassword@localhost:3306/notesapi" |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Microsoft SQL Server |
| 133 | + |
| 134 | +Here is an example connection string with a local Microsoft SQL Server database: |
| 135 | + |
| 136 | +```prisma |
| 137 | +datasource db { |
| 138 | + provider = "sqlserver" |
| 139 | + url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;" |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### MongoDB |
| 144 | + |
| 145 | +Here is an example connection string with a local MongoDB database: |
| 146 | + |
| 147 | +```prisma |
| 148 | +datasource db { |
| 149 | + provider = "mongodb" |
| 150 | + url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority" |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +</details> |
| 155 | + |
| 156 | +## Next steps |
| 157 | + |
| 158 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 159 | +- Share your feedback on the [Prisma Discord](https://pris.ly/discord/) |
| 160 | +- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) |
0 commit comments