Home

Framework Quickstarts

Use Supabase with RedwoodJS

Learn how to create a Supabase project, add some sample data to your database using Prisma migration and seeds, and query the data from a RedwoodJS app.

1

Setup your new Supabase Project

Create a new project in the Supabase Dashboard.

New Project

2

Gather Database Connection Strings

After your project is ready, gather the following information about your database connections:

  • Connection String (port 5432)
  • Connection Pooling / Connection String (port 6543)

You will need these to setup environment variables in Step 5.

New Project

3

Create a RedwoodJS app

Create a RedwoodJS app with TypeScript.

Terminal

_10
yarn create redwood-app my-app --ts

4

Open your RedwoodJS app in VS Code

You'll develop your app, manage database migrations, and run your app in VS Code.

Terminal

_10
cd my-app
_10
code .

5

Configure Environment Variables

In your .env file, add the following environment variables for your database connection:

  • The DIRECT_URL should use the Connection String from your Supabase project. Hint: the port is 5432.

  • The DATABASE_URL should use the Connection Pooling / Connection String from your Supabase project. Hint: the port is 6432.

.env

_10
# PostgreSQL connection string used for migrations
_10
DIRECT_URL="postgres://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:5432/postgres"
_10
_10
# PostgreSQL connection string with pgBouncer config — used by Prisma Client
_10
DATABASE_URL="postgres://postgres:[YOUR-PASSWORD]@db.[YOUR-PROJECT-REF].supabase.co:6543/postgres?pgbouncer=true"

6

Update your Prisma Schema

By default, RedwoodJS ships with a SQLite database, but we want to use PostgreSQL.

Update your Prisma schema file api/db/schema.prisma to use your Supabase PostgreSQL database connection environment variables you setup in Step 5.

api/prisma/schema.prisma

_10
datasource db {
_10
provider = "postgresql"
_10
url = env("DATABASE_URL")
_10
directUrl = env("DIRECT_URL")
_10
}

7

Create the Country model and apply a schema migration

Create the Country model in api/db/schema.prisma and then run yarn rw prisma migrate dev from your terminal to apply the migration.

api/db/schema.prisma

_10
model Country {
_10
id Int @id @default(autoincrement())
_10
name String @unique
_10
}

8

Update seed script

Let's seed the database with a few countries.

Update the file scripts/seeds.ts to contain the following code:

scripts/seed.ts

_20
import type { Prisma } from '@prisma/client'
_20
import { db } from 'api/src/lib/db'
_20
_20
export default async () => {
_20
try {
_20
const data: Prisma.CountryCreateArgs['data'][] = [
_20
{ name: 'United States' },
_20
{ name: 'Canada' },
_20
{ name: 'Mexico' },
_20
]
_20
_20
console.log('Seeding countries ...')
_20
_20
const countries = await db.country.createMany({ data })
_20
_20
console.log('Done.', countries)
_20
} catch (error) {
_20
console.error(error)
_20
}
_20
}

9

Seed your database

Run the seed database command to populate the Country table with the countries you just created.

Terminal

_10
yarn rw prisma db seed

10

Scaffold the Country UI

Now, we'll use RedwoodJS generators to scaffold a CRUD UI for the Country model.

Terminal

_10
yarn rw g scaffold country

11

Start the app

Start the app via yarn rw dev. A browser will open to the RedwoodJS Splash page.

RedwoodJS Splash Page

12

View Countries UI

Click on /countries to visit http://localhost:8910/countries where should see the list of countries.

You may now edit, delete, and add new countries using the scaffolded UI.

RedwoodJS Splash Page