/supabase-tutorials

How to generate types from Supabase schema?

Generate TypeScript types from your Supabase schema with our step-by-step guide. Learn to set up Supabase, install the CLI, and automate type generation for type safety.

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 generate types from Supabase schema?

 

Step 1: Set Up Supabase

 

  • Make sure you have a Supabase project set up. If not, you can start by creating an account at Supabase and setting up your database.

  • Get your API URL and the Anon key from your Supabase project dashboard. These will be used to connect your application to the Supabase instance.

 

Step 2: Install Supabase CLI

 

  • Supabase CLI is required for generating types. To install it, you should have Node.js installed on your system. Use the following command to install the Supabase CLI globally:

    npm install -g supabase

 

Step 3: Generate Types via Supabase CLI

 

  • Once the Supabase CLI is installed, navigate to your project directory in the terminal.

  • Use the following command to authenticate your CLI session with Supabase:

    supabase login
  • Fetch the database schema from your Supabase project. Use this command to output the current schema to the console:

    supabase db dump
  • To generate TypeScript types from the Supabase schema, run:

    supabase gen types typescript --db-url="your_database_url"

    Replace your_database_url with the connection URL of your Supabase project.

 

Step 4: Use the Generated Types in Your Project

 

  • After generating the types, they are typically saved in the supabase/types directory within your project. Inspect this folder to confirm the TypeScript files.

  • Import the generated types in your project files where you need to use them. For example:

    import { YourTableType } from './supabase/types';
  • Use these types in your application to ensure type safety when dealing with data from your Supabase database.

 

Step 5: Automate Type Generation

 

  • Consider creating a script in your package.json to automate this type generation process, especially if your database schema changes often. Here's an example of how to set it up:

    In your package.json, add:

    "scripts": {
      "generate:types": "supabase gen types typescript --db-url='your_database_url'"
    }
  • Now, you can run this command anytime to update the types:

    npm run generate:types

 

Step 6: Incorporate Types with Supabase Client

 

  • If you're using the Supabase client in your application, you can enhance it with the generated types to leverage TypeScript's type checking features:

    
    import { createClient } from "@supabase/supabase-js";
    import { Database } from "./supabase/types"; // Adjust the path as needed
    
    const supabase = createClient("your_supabase_url", "your_anon_key");
    
  • This will ensure that all your interactions with the database through the Supabase client are type-checked against your defined schema.

 

Step 7: Monitor for Schema Changes

 

  • Regularly check and regenerate your types, especially when there are changes to your database schema. This keeps your codebase aligned with the actual data structure, improving reliability and reducing bugs.

 

Conclusion

 

  • Generating types from your Supabase schema helps maintain type safety across your application, making it easier to manage database interactions and reduce runtime errors.

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