Skip to content

Using the PlanetScale serverless driver with Prisma

How to use the PlanetScale serverless driver with Prisma.

Overview

This document outlines how you can use the PlanetScale serverless driver along with Prisma in your application.

Set up

To get started:

  1. Install the Prisma driver adapter for PlanetScale (@prisma/adapter-planetscale), PlanetScale serverless driver (@planetscale/database), and undici packages:
Terminal
npm install @prisma/adapter-planetscale @planetscale/database undici
Note

When using an older version of Node.js, you can provide a custom fetch function implementation. We recommend the undici package on which Node's built-in fetch is based. Node.js version 18 includes a built-in global fetch function.

  1. Enable the driverAdapters Preview feature flag:
JavaScript
// schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
Note

Ensure you update the host value in your connection string to aws.connect.psdb.cloud. You can learn more about this here.

  1. Generate Prisma Client:
Terminal
npx prisma generate
  1. Update your Prisma Client instance to use the PlanetScale serverless driver:
JavaScript
import { Client } from '@planetscale/database'
import { PrismaPlanetScale } from '@prisma/adapter-planetscale'
import { PrismaClient } from '@prisma/client'
import dotenv from 'dotenv'
import { fetch as undiciFetch } from 'undici'
dotenv.config()
const connectionString = `${process.env.DATABASE_URL}`
const client = new Client({ url: connectionString, fetch: undiciFetch })
const adapter = new PrismaPlanetScale(client)
const prisma = new PrismaClient({ adapter })
async function main() {
const posts = await prisma.post.findMany()
console.log(posts)
}

You can then use Prisma Client as you usually would with auto-completion and full type-safety.

Need help?

Get help from the PlanetScale support team, or join our GitHub discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on Help us improve this page