Learn how to use Supabase with Prisma in a Node.js project. Follow step-by-step instructions to initialize, configure, migrate your database, and generate a client.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Initialize a Supabase Project
First, create a new project on the Supabase website:
After your project is set up, you will be redirected to the project dashboard, where you can find the API URL and API Key in the "API" section.
Step 2: Set Up a Node.js Project
Set up a new Node.js project on your local machine:
mkdir supabase-prisma-tutorial
cd supabase-prisma-tutorial
npm
:
npm init -y
Step 3: Install Dependencies
You'll need to install the necessary packages for Prisma and connect it to Supabase:
npm install @prisma/client prisma @supabase/supabase-js
Step 4: Initialize Prisma
Set up Prisma in your Node.js project:
npx prisma init
This will create a prisma
directory with a schema.prisma
file.
schema.prisma
and configure it to connect to your Supabase database. Replace the DATABASE_URL
in the .env
file with the connection string from the Supabase dashboard. The format should look like this:
DATABASE\_URL="postgresql://username:password@host:port/database"
Step 5: Define Your Data Model
Define your Prisma data model inside schema.prisma
. Here’s an example model:
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
createdAt DateTime @default(now())
}
Step 6: Migrate Your Database
With the data model defined, create the necessary tables in the Supabase database using a Prisma migration:
npx prisma migrate dev --name init
This command will apply your schema to the database. Check the Supabase dashboard to ensure the tables are created.
Step 7: Generate Prisma Client
Generate the Prisma client to interact with your Supabase database using:
npx prisma generate
Now you can use Prisma in your application to query and manipulate your database.
Step 8: Create a Node.js Script
Create a script to test the Prisma client:
index.js
.
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function main() {
const post = await prisma.post.create({
data: {
title: 'Hello Supabase',
content: 'This is our first post using Prisma with Supabase!',
},
});
console.log(post);
}
main()
.catch(e => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
node index.js
If everything is set up correctly, a new post will be created in your Supabase database, and the details of the post will be logged to the console.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.