Supabase

How to use Supabase with Prisma ORM?

Discover the effortless way to blend Supabase with Prisma ORM. Get clear, step-by-step guidance, uncover essential perks, and pick up valuable tips for smooth database management.

Developer profile skeleton
a developer thinking

Overview

Integrating Supabase with Prisma ORM supercharges apps by blending the best of both worlds. Supabase offers a solid, open-source backend-as-a-service, complete with a PostgreSQL database. Prisma ORM, on the other hand, makes working with databases a breeze, thanks to type-safe queries and easy migrations. Putting these two together lets developers craft scalable and maintainable apps smoothly. This guide covers all the key steps to set up Supabase, link Prisma, and tap into Prisma's querying features to work smartly with your Supabase-powered database.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to use Supabase with Prisma ORM?

Step 1: Set Up Supabase

  1. Sign up and create a new project on Supabase
  • Head over to supabase.com and sign up for a new account.
  • Create a new project, pick a name for it, and choose your database region.
  1. Get the API keys and Database URL
  • Go to your Supabase project dashboard.
  • Click on the "Settings" tab, then "API". Jot down the API URL and anon key.
  • Find the Database URL under the "Settings" tab in "Database".

Step 2: Set Up Prisma

  1. Initialize a new Node.js project (if not already done)
    ```sh
    mkdir my-prisma-project
    cd my-prisma-project
    npm init -y
    ```

  2. Install Prisma and initialize
    ```sh
    npm install prisma --save-dev
    npx prisma init
    ```

  3. Install the necessary Prisma client

```sh
npm install @prisma/client
```

  1. Configure the .env file
  • Open the .env file in your project directory.
  • Add the Supabase database connection string:
    ```env
    DATABASE_URL="your_supabase_database_url"
    ```

Step 3: Define your Prisma schema

  1. Update schema.prisma file
    • Open the prisma/schema.prisma file.

    • Define your data models. For example:
      ```prisma
      datasource db {
      provider = "postgresql"
      url = env("DATABASE_URL")
      }

      generator client {
      provider = "prisma-client-js"
      }

      model User {
      id Int @id @default(autoincrement())
      email String @unique
      name String?
      }
      ```

Step 4: Migrate the Database

  1. Run the migration
  • Save your schema and then run:
    ```sh
    npx prisma migrate dev --name init
    ```
  1. Generate the Prisma client
    ```sh
    npx prisma generate
    ```

Step 5: Use Prisma Client in Your Application

  1. Create a file to use the Prisma client (e.g., index.js)
  • For example, to create a new user in your database:
    ```js
    const { PrismaClient } = require('@prisma/client');
    const prisma = new PrismaClient();

    async function main() {
    const newUser = await prisma.user.create({
    data: {
    email: 'test@example.com',
    name: 'John Doe',
    },
    });
    console.log(newUser);
    }

    main()
    .catch(e => console.error(e))
    .finally(async () => {
    await prisma.$disconnect();
    });
    ```

  1. Run the file to test
    ```sh
    node index.js
    ```

Step 6: Querying the Database

  1. Add additional queries to index.js for retrieving data:

    • Example:
      ```js
      async function main() {
      // Create a new user
      const newUser = await prisma.user.create({
      data: {
      email: 'test@example.com',
      name: 'John Doe',
      },
      });

      // Fetch all users
      const allUsers = await prisma.user.findMany();
      console.log(allUsers);
      }

      main()
      .catch(e => console.error(e))
      .finally(async () => {
      await prisma.$disconnect();
      });
      ```

  2. Run your application again to see the changes:
    ```sh
    node index.js
    ```

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.