• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
  • Global Configuration
    • Environment Variables
    • Feature Flags
  • Server Actions
    • Sending CSRF Token to Actions
    • Server Actions Error Handling
  • The Makerkit SDK
    • User SDK
    • Organization SDK
    • Organization Subscription SDK
    • Data Loader SDK
  • Architecture and Folder Structure
    • Structure your Application
    • Data Model
    • Introduction
    • Initial Setup
    • Running the App
    • Project Configuration
    • Environment Variables
    • Tailwind CSS and Styling
    • Authentication
    • Onboarding Flow
    • Database Schema
    • Supabase: Data Fetching
    • Supabase: Data Writing
    • Routing
    • Building the Tasks page
    • Building the Task Detail page
    • API Routes
    • Application Pages
    • API Routes Validation
    • Translations
    • Functions you need to know
    • Adding pages to the Marketing Site
    • Deploying to Production
    • Updating to the latest version
This documentation is for a legacy version of Next.js and Supabase. For the latest version, please visit the Next.js and Supabase V2 documentation

Supabase: Data Fetching

Learn how to fetch data from Supabase in your React applications.

With our tables and RLS in place, we can finally start writing our queries using the Supabase Postgres client.

My convention (used throughout the boilerplate) is to write functions that inject the Supabase SDK Client as a parameter: this allows us to reuse these functions in both the browser and the server.

Fetching a list of tasks

First, we want to write a function that is responsible for fetching the paginated list of tasks using the Supabase Postgres client.

Below, we will write the query necessary to fetching the tasks for a given project:

  • paginated using the pageIndex and perPage parameters
  • filtered by the query parameter that is matched against the name column.
lib/tasks/queries.ts
import type { SupabaseClient } from '@supabase/supabase-js';
import type { Database } from '~/database.types';
import { TASKS_TABLE } from '~/lib/db-tables';
import Task from '~/lib/tasks/types/task';
type Client = SupabaseClient<Database>;
const TASKS_PAGE_SIZE = 10;
export function getTasks(
client: Client,
params: {
organizationUid: string;
pageIndex: number;
perPage?: number;
query?: string;
},
) {
const { organizationUid, perPage, pageIndex } = params;
const { startOffset, endOffset } = getPaginationOffsets(pageIndex, perPage);
let query = client
.from(TASKS_TABLE)
.select<string, Task>(
`
id,
name,
organizationId: organization_id,
dueDate: due_date,
done,
description,
organization: organization_id !inner (
id,
uuid
)
`, { count: 'exact' }
)
.eq('organization.uuid', organizationUid)
.range(startOffset, endOffset);
if (params.query) {
query = query.textSearch('name', params.query);
}
return query;
}
function getPaginationOffsets(pageIndex: number, perPage?: number) {
const pageSize = perPage || TASKS_PAGE_SIZE;
const startOffset = pageIndex * pageSize;
const endOffset = startOffset + pageSize;
return {
startOffset,
endOffset,
};
}

Fetching a single task

Next, we want to write a function that is responsible for fetching a single task when viewing the task details page. We can place this function in the same file as the getTasks function.

lib/tasks/queries.ts
export function getTask(client: Client, id: number) {
return client
.from(TASKS_TABLE)
.select(
`
id,
name,
organizationId: organization_id,
dueDate: due_date,
description,
done
`,
)
.eq('id', id)
.single();
}

We now have all the queries we need to fetch tasks from Supabase, both as a paginated list and as a single task. In the next section, we will learn how to use these queries from the React Server Components.

On this page
  1. Fetching a list of tasks
    1. Fetching a single task