/supabase-tutorials

How to use Supabase in a monorepo?

Learn how to integrate Supabase in a monorepo using Nx or Lerna. This step-by-step guide covers project setup, environment configuration, and how to connect your app to Supabase.

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 in a monorepo?

 

Step 1: Setting Up a Monorepo Structure

 

To use Supabase in a monorepo, start by setting up a monorepo structure. A common tool for setting up monorepos is Nx or Lerna. Here is an example using Nx.

npx create-nx-workspace@latest supabase-monorepo

Follow the prompts to set up your workspace. Choose a package-based setup if prompted, so you can manage multiple projects more effectively.

 

Step 2: Adding a Supabase Project

 

Once your monorepo is ready, you can add a Supabase project. First, sign up and log in to Supabase and create a new project. Note your project's URL and API key as you'll need these to connect your application to Supabase.

 

Step 3: Installing the Supabase Client Library

 

Navigate to the package where you'll be using Supabase. Usually, this will be a frontend or backend service in your monorepo. Then install the Supabase client library using your package manager.


cd packages/your-app
npm install @supabase/supabase-js 

 

Step 4: Setting Up Environment Variables

 

Inside the package where you installed Supabase, create an .env file at the root and add your Supabase project's URL and API key.


SUPABASE\_URL=https://xyzcompany.supabase.co
SUPABASE_ANON_KEY=your-anon-public-key

Make sure to add this .env file to your .gitignore to avoid exposing your API keys.

 

Step 5: Initializing Supabase Client

 

Now, create a service file to initialize the Supabase client. This makes it easy to use across your application.


import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.SUPABASE\_URL
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY

const supabase = createClient(supabaseUrl, supabaseAnonKey)

export default supabase

 

Step 6: Using Supabase in Your Application

 

Import the configured Supabase client into your application code where you need to interact with your database.


import supabase from './path-to-your-supabaseServiceFile'

// Example function to get data from a table
async function getData() {
  let { data, error } = await supabase
    .from('your-table')
    .select('\*')

  if (error) console.error('Error fetching data: ', error)
  else console.log('Data: ', data)
}

You can call the getData() function within your application wherever you need to fetch the data.

 

Step 7: Contributing and Collaboration in a Monorepo

 

A key advantage of a monorepo is streamlined collaboration. Use your version control system, such as Git, to branch and manage changes. Others on your team can do the same, allowing for easier code reviews and project management.

 

By following these steps, you'll efficiently set up and utilize Supabase in a monorepo, leveraging all its capabilities with a structured approach.

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