This example shows how to implement a simple web app using Next.js and Prisma Client. The example uses an SQLite database file with some initial dummy data. The example was bootstrapped using the Next.js CLI command create-next-app
.
Download this example:
npx try-prisma@latest --template orm/nextjs
Then, navigate into the project directory:
cd nextjs
Alternative: Clone the entire repo
Clone this repository:
git clone [email protected]:prisma/prisma-examples.git --depth=1
Install npm dependencies:
cd prisma-examples/orm/nextjs
npm install
This example uses a local SQLite database by default. If you want to use to Prisma Postgres, follow these instructions (otherwise, skip to the next step):
-
Set up a new Prisma Postgres instance in the Prisma Data Platform Console and copy the database connection URL.
-
Update the
datasource
block to usepostgresql
as theprovider
and paste the database connection URL as the value forurl
:datasource db { provider = "postgresql" url = "prisma+postgres://accelerate.prisma-data.net/?api_key=ey...." }
Note: In production environments, we recommend that you set your connection URL via an environment variable, e.g. using a
.env
file. -
Install the Prisma Accelerate extension:
npm install @prisma/extension-accelerate
-
Add the Accelerate extension to the
PrismaClient
instance:+ import { withAccelerate } from "@prisma/extension-accelerate" + const prisma = new PrismaClient().$extends(withAccelerate())
That's it, your project is now configured to use Prisma Postgres!
Run the following command to create your database. This also creates the User
and Post
tables that are defined in prisma/schema.prisma
:
npx prisma migrate dev --name init
When npx prisma migrate dev
is executed against a newly created database, seeding is also triggered. The seed file in prisma/seed.ts
will be executed and your database will be populated with the sample data.
If you switched to Prisma Postgres in the previous step, you need to trigger seeding manually (because Prisma Postgres already created an empty database instance for you, so seeding isn't triggered):
npx prisma db seed
npm run dev
The server is now running on http://localhost:3000
. You can now view pages, e.g. http://localhost:3000/posts
.
Evolving the application typically requires two steps:
- Migrate your database using Prisma Migrate
- Update your application code
For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.
The first step is to add a new table, e.g. called Profile
, to the database. You can do this by adding a new model to your Prisma schema file file and then running a migration afterwards:
// ./prisma/schema.prisma
model User {
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
+ profile Profile?
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
+model Profile {
+ id Int @default(autoincrement()) @id
+ bio String?
+ user User @relation(fields: [userId], references: [id])
+ userId Int @unique
+}
Once you've updated your data model, you can execute the changes against your database with the following command:
npx prisma migrate dev --name add-profile
This adds another migration to the prisma/migrations
directory and creates the new Profile
table in the database.
You can now use your PrismaClient
instance to perform operations against the new Profile
table. Those operations can be used to implement new pages in the app.
If you want to try this example with another database than SQLite, you can adjust the the database connection in prisma/schema.prisma
by reconfiguring the datasource
block.
Learn more about the different connection configurations in the docs.
Expand for an overview of example configurations with different databases
For PostgreSQL, the connection URL has the following structure:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}
Here is an example connection string with a local PostgreSQL database:
datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
}
For MySQL, the connection URL has the following structure:
datasource db {
provider = "mysql"
url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
}
Here is an example connection string with a local MySQL database:
datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/notesapi"
}
Here is an example connection string with a local Microsoft SQL Server database:
datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}
Here is an example connection string with a local MongoDB database:
datasource db {
provider = "mongodb"
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}
- Check out the Prisma docs
- Share your feedback on the Prisma Discord
- Create issues and ask questions on GitHub