The code from "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf 2021
34
stars
6
commits
JavaScript
primary language
May 6, 2026
updated
Note: This repository targets PlanetScale Vitess/MySQL. PlanetScale also offers managed Postgres. For more information, see the PlanetScale Postgres documentation.
Important update: This talk contains an outdated workflow of using
prisma migrate devwith a shadow branch to update your database schema. We now recommend usingprisma db pushwith no shadow branches. See the documentation on how to do migrations to your schema here:
This repo corresponds to the "Databases as Code with PlanetScale and Prisma" talk from Next.js Conf 2021.
This is a Next.js project bootstrapped with create-next-app.
In the demo from the talk, we created and edited an API route, http://localhost:3000/api/stars. This endpoint can be edited in pages/api/stars.js. The pages/api directory is mapped to /api/*. Files in this directory are treated as API routes instead of React pages.
Below are the steps I went through in the talk during Next.js Conf. You will likely find the talk to include a few more details around each step. I have created the following steps to give a brief idea for what is needed to go to production.
Prerequisite: You need to have the Prisma and PlanetScale CLIs installed
star-app databaseinitial-setup and shadow database branches from main branchNote: This talk contains an outdated workflow of using
prisma migrate devwith a shadow branch to update your database schema. We now recommend usingprisma db pushwith no shadow branches. See the documentation on how to do migrations to your schema here:
npx create-next-app@latest --use-npm
npm install @prisma\client
npx prisma init
prisma/schema.prisma file:datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
referentialIntegrity = "prisma"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
model Star {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @db.VarChar(255)
consellation String @db.VarChar(255)
}
.env file:DATABASE_URL="mysql://root@127.0.0.1:3309/star-app"
SHADOW_DATABASE_URL="mysql://root@127.0.0.1:3310/star-app"
pscale CLI to locally proxy into our PlanetScale database. In a two different terminal tabs, run:pscale connect star-app initial-setup --port 3309
pscale connect star-app shadow --port 3310
prisma migrate dev --name init
You will notice a prisma/migrations folder as well as the schema in your initial-setup branch in PlanetScale.
11. Create lib/prisma.js file:
import { PrismaClient } from '@prisma/client'
const prisma = global.prisma || new PrismaClient();
if (process.env.NODE_ENV === "development") global.prisma = prisma;
export default prisma
pages/api/stars.js file:import prisma from "../../lib/prisma";
export default async function assetHandler(req, res) {
const { method } = req;
switch (method) {
case "GET":
try {
const stars = await prisma.star.findMany();
res.status(200).json(stars);
} catch(e) {
console.error("Request error", e);
res.status(500).json({ error: "Error fetching posts" });
}
break;
default:
res.setHeader("Allow", ["GET"]);
res.status(405).end(`Method ${method} Not Allowed`);
break;
}
}
npx prisma studio, which will open a browser window. After you add data, then close Prisma Studio. You can also check to see that the data is in your intial-setup branch in PlanetScale.npm run dev
Navigate to http://localhost:3000/api/stars to see data returned from your API endpoint!main database branch; otherwise, your API route will return no data.Warning: You need to make sure to follow the steps from the "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf before deploying to Vercel.
Deploy this application quickly to Vercel using the following Deploy button:
In order to connect your database to your application deployed in Vercel, you will need to set the DATABASE_URL variable. This URL is how your Vercel application is able to securely connect to PlanetScale. The URL is in the following format: mysql://[username]:[password]@[host]/star-app?sslaccept=strict
You can create a password in PlanetScale and generate this URL in the Connect modal in the database branch of your choice, select "Prisma" to get the Prisma specific URL:

Note: If you see a 404 page after your build is successful, go into your app's Vercel Settings > General > Build & Development Settings and make sure that Next.js is the selected framework.
JavaScript
62.0%
CSS
38.0%
The code from "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf 2021
34
stars
6
commits
JavaScript
primary language
May 6, 2026
updated
Note: This repository targets PlanetScale Vitess/MySQL. PlanetScale also offers managed Postgres. For more information, see the PlanetScale Postgres documentation.
Important update: This talk contains an outdated workflow of using
prisma migrate devwith a shadow branch to update your database schema. We now recommend usingprisma db pushwith no shadow branches. See the documentation on how to do migrations to your schema here:
This repo corresponds to the "Databases as Code with PlanetScale and Prisma" talk from Next.js Conf 2021.
This is a Next.js project bootstrapped with create-next-app.
In the demo from the talk, we created and edited an API route, http://localhost:3000/api/stars. This endpoint can be edited in pages/api/stars.js. The pages/api directory is mapped to /api/*. Files in this directory are treated as API routes instead of React pages.
Below are the steps I went through in the talk during Next.js Conf. You will likely find the talk to include a few more details around each step. I have created the following steps to give a brief idea for what is needed to go to production.
Prerequisite: You need to have the Prisma and PlanetScale CLIs installed
star-app databaseinitial-setup and shadow database branches from main branchNote: This talk contains an outdated workflow of using
prisma migrate devwith a shadow branch to update your database schema. We now recommend usingprisma db pushwith no shadow branches. See the documentation on how to do migrations to your schema here:
npx create-next-app@latest --use-npm
npm install @prisma\client
npx prisma init
prisma/schema.prisma file:datasource db {
provider = "mysql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
referentialIntegrity = "prisma"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
model Star {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @db.VarChar(255)
consellation String @db.VarChar(255)
}
.env file:DATABASE_URL="mysql://root@127.0.0.1:3309/star-app"
SHADOW_DATABASE_URL="mysql://root@127.0.0.1:3310/star-app"
pscale CLI to locally proxy into our PlanetScale database. In a two different terminal tabs, run:pscale connect star-app initial-setup --port 3309
pscale connect star-app shadow --port 3310
prisma migrate dev --name init
You will notice a prisma/migrations folder as well as the schema in your initial-setup branch in PlanetScale.
11. Create lib/prisma.js file:
import { PrismaClient } from '@prisma/client'
const prisma = global.prisma || new PrismaClient();
if (process.env.NODE_ENV === "development") global.prisma = prisma;
export default prisma
pages/api/stars.js file:import prisma from "../../lib/prisma";
export default async function assetHandler(req, res) {
const { method } = req;
switch (method) {
case "GET":
try {
const stars = await prisma.star.findMany();
res.status(200).json(stars);
} catch(e) {
console.error("Request error", e);
res.status(500).json({ error: "Error fetching posts" });
}
break;
default:
res.setHeader("Allow", ["GET"]);
res.status(405).end(`Method ${method} Not Allowed`);
break;
}
}
npx prisma studio, which will open a browser window. After you add data, then close Prisma Studio. You can also check to see that the data is in your intial-setup branch in PlanetScale.npm run dev
Navigate to http://localhost:3000/api/stars to see data returned from your API endpoint!main database branch; otherwise, your API route will return no data.Warning: You need to make sure to follow the steps from the "Databases as Code with PlanetScale and Prisma" talk at Next.js Conf before deploying to Vercel.
Deploy this application quickly to Vercel using the following Deploy button:
In order to connect your database to your application deployed in Vercel, you will need to set the DATABASE_URL variable. This URL is how your Vercel application is able to securely connect to PlanetScale. The URL is in the following format: mysql://[username]:[password]@[host]/star-app?sslaccept=strict
You can create a password in PlanetScale and generate this URL in the Connect modal in the database branch of your choice, select "Prisma" to get the Prisma specific URL:

Note: If you see a 404 page after your build is successful, go into your app's Vercel Settings > General > Build & Development Settings and make sure that Next.js is the selected framework.
JavaScript
62.0%
CSS
38.0%