Back to posts
  • Web Development
  • Prototyping
  • User Experience

Live-Coding a Fullstack Prototype – Episode 2

Nils Borgböhmer
Nils Borgböhmer
This post is part of a series

Welcome back to our second episode of live coding! In this series, we're developing a prototype of a virtual bookshelf. In our first episode, we created a simple application that displayed a list of books using mock data. Today, we're taking things up a notch by integrating SupaBase to replace our mock data with real data from a PostgreSQL database. This post is a summary of the YouTube video shown above.

Summary

In this blog post, we will set up SupaBase, fetch data from it, handle errors, and use TypeScript for type safety. We'll also discuss converting snake case to camel case, utilizing async/await and useEffect hooks for asynchronous operations, and outline the next steps for creating and writing data to the database.

Introduction and Recap

Last week, we ended with a list of books generated from a JSON file. Today, we aim to replace that mock data with data fetched from a SupaBase PostgreSQL database. SupaBase is an open-source backend as a service that offers a hosted PostgreSQL database along with APIs for easy data management.

Integrating SupaBase and Database Setup

We started by creating a new organization and project in SupaBase. SupaBase provides two keys and a URL upon project setup, which we added to our environment variables. After setting up the SupaBase client in our project, we created a books table using the visual table editor, defining columns such as title, author, publication_year, description, notes, and rating.

Fetching Data from SupaBase

To fetch data, we wrote a function to select data from the books table. This function handles the data retrieval process and deals with any potential errors that might occur.

Retrieving and Displaying Data from the SupaBase Database

In our React component, we used useState and useEffect hooks to manage and fetch book data. By doing this, we ensured that our application fetches and displays the data from SupaBase when it renders.

Handling Errors and Converting Snake Case to Camel Case

To maintain consistency with our TypeScript types, we converted snake case data from the database to camel case. We used a library to facilitate this conversion, ensuring our data matched the expected format in our application.

Using TypeScript to Define Types and Ensure Type Safety

We defined TypeScript interfaces to ensure type safety throughout our application. This step is crucial for maintaining robust and error-free code, especially when dealing with external data sources like SupaBase.

Utilizing Async/Await and useEffect Hooks for Asynchronous Operations

We utilized async/await in combination with useEffect to fetch data asynchronously and update the component state. This approach ensures our application handles asynchronous operations smoothly and efficiently.

Next Steps: Implementing Creating and Writing Data to the Database

In our next session, we'll implement forms to add new books to the database and handle the corresponding operations in SupaBase. This will involve creating new functions for data insertion and ensuring the new data integrates seamlessly with our existing setup.

FAQs

  1. What is SupaBase, and why use it?

    SupaBase is an open-source backend as a service that provides a hosted PostgreSQL database and API for easy data management. It's great for quick prototyping and simple applications.

  2. How do I handle errors when fetching data from SupaBase?

    You can handle errors by checking the error property returned by SupaBase's API and taking appropriate actions if an error is detected.

  3. Can SupaBase work with other frontend frameworks?

    Yes, SupaBase is framework-agnostic and can be used with various frontend frameworks like Next.js, Svelte, and Angular, among others.

Author

Nils Borgböhmer
Nils Borgböhmer

Co-Founder, Head of Interaction Design

Connect on LinkedIn

Similar articles