/supabase-tutorials

How to use Supabase with Prisma?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to use Supabase with Prisma?

 

Step 1: Initialize a Supabase Project

 

First, create a new project on the Supabase website:

  • Go to the Supabase website.
  • Log in or sign up for an account.
  • Click on the "New Project" button.
  • Fill in the required project information:
  • Project Name
  • Database Password
  • Region
  • Click "Create" to initialize the project.

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:

  • Open your terminal or command prompt.
  • Create a new directory for your project and navigate into it:

mkdir supabase-prisma-tutorial
cd supabase-prisma-tutorial
  • Initialize the Node.js project with 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:

  • Run the Prisma init command:

npx prisma init

This will create a prisma directory with a schema.prisma file.

  • Open 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:

  • Run the migration command:

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:

  • Inside your project directory, create a file named index.js.
  • Add the following code to connect to the database and perform a basic operation:

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();
  });
  • Run the script:

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.

 

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022