Setting Up Prisma (Latest Approach)
I previously wrote a blog on how to set up Prisma and learn its basics. The old guide still works if you want to understand Prisma fundamentals, but the setup process has changed quite a bit since then.
If you still want to check out that older guide, you can read it here: https://fardeen.tech/blogs/cm7ewul930000gs9o85b6ml42
Now, let’s walk through the new, up-to-date way of setting up Prisma in your project, including Prisma Accelerate for faster queries and an optional Prisma Postgres instance.
1. Install Prisma CLI and tsx.
npm install prisma tsx --save-dev
2. Install Prisma Client & Accelerate Extension.
npm install @prisma/extension-accelerate @prisma/client
3. Initialize Prisma in your project.
Now some changes here:
1. --output ../app/generated/prisma
: allow us to define the path for the converted database schema.
2. --db
: if you also want to get a postgres database instance from prisma, this flag allows you to initialize a database instance and put the database url to .env
file automatically.
npx prisma init --db --output ../app/generated/prisma
If you’re not using Prisma Postgres, you can skip the
--db
flag and manually update your.env
file with your own database connection string.
4. Define database schema
In your schema.prisma
file define the your schema.
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Also, when using Accelerate in production, your datasource
block should have both DATABASE_URL
and DIRECT_URL
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // accelerate-optimized url
directUrl = env("DIRECT_URL") // direct db url for migrations
}
5. Migrate the database.
npx prisma migrate dev --name init
6. Generate Prisma Client (Accelerate-Friendly)
When using Accelerate, you can skip the local query engine binary in production:
npx prisma generate --no-engine
7. Create an prisma singleton file
create an lib/prisma.ts
file and put this code in it:
import { PrismaClient } from '../app/generated/prisma'
import { withAccelerate } from '@prisma/extension-accelerate'
const globalForPrisma = global as unknown as {
prisma: PrismaClient
}
const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
export default prisma;
8. Import the prisma in your app
import prisma from "@lib/prisma"
export default function App() {
const fetchUser = async () => {
const user = await prisma.user.findMany();
return user;
}
return ()
}
- Summary of changes from old setup
--db
flag can now provision a Prisma-hosted Postgres database automatically.--output
lets you customize Prisma Client location.Added
directUrl
inschema.prisma
for migrations.Use
--no-engine
when generating Prisma Client with Accelerate.Updated singleton import to use
@prisma/client
directly.Clarified local vs production Accelerate behavior.
Hope this blog will help you, see you in the next one till than kepp making memes using memehub dot mom.
@fardeentwt