Supabase
For seeding data into your Supabase project, we recommend selecting public
and auth
schemas only.
If you experience any issues with any of the paths, reach out to us on Discord (opens in a new tab).
Generate data for your Supabase local development stack
Supabase makes it easy to set up a local development environment linked to your Supabase project. Seed can improve your local development by seeding your local database with data, making coding and testing more efficient and accurate.
In this short guide we'll focus on how to integate @snaplet/seed
with your Supabase project.
1. Setup your supabase project
Once you have a working supabase project with a local development stack (opens in a new tab) setup.
Running npx supabase start
should spin up the database and give you the URL to access postgres database.
Started supabase local development setup. ... DB URL: postgresql://postgres:postgres@127.0.0.1:54322/postgresql ...
Make sure that you have the migrations applied in your local database with your differents app tables on it. If you don't refer to Supabase docs (opens in a new tab) to run your migrations.
2. Set up Seed
2.1. To interface with your postgres database we'll need to install an adapter for @snaplet/seed
you can use either node-postgres
or postgres
npm install -D postgres
2.2. Initialise @snaplet/seed
in your your root project.
2.3. The init command will create a seed.config.ts
file in which you can specify the database connection details and exclude part of the
database schema you don't want to alter with @snaplet/seed
import { SeedPostgres } from "@snaplet/seed/adapter-postgres";import { defineConfig } from "@snaplet/seed/config";import postgres from "postgres";export default defineConfig({ adapter: () => { const client = postgres("postgresql://postgres:postgres@127.0.0.1:54322/postgresql"); return new SeedPostgres(client); }, select: [ // We don't alter any extensions tables that might be owned by extensions "!*", // We want to alter all the tables under public schema "public*", // We also want to alter some of the tables under the auth schema "auth.users", "auth.identities", "auth.sessions", ]});
Once you save the edited file, the init
command will continue and create a seed.ts
example file in your project folder.
You are now ready to seed your database.
4. Keep Seed Client in sync with your database
Whenever your database structure changes (e.g after a new migration is applied), @snaplet/seed
will need be regenerated to reflect the new structure.
To do this, run the following command:
4. (optional) Hook into Supabase seeding workflow
You can hook into Supabase seeding workflow (opens in a new tab) using the dryRun
option for client creation in the seed.ts
file:
import { createSeedClient } from "@snaplet/seed";const seed = await createSeedClient({ dryRun: true });
This option will turn on the dry run mode, which will log the SQL queries that would be executed to the console, without actually executing them.
You can then put those queries into the sql seed script expected by supabase:
npx tsx seed.ts > supabase/seed.sql
Then, running npx supabase db reset
should apply the seed script to your local database.
Going futher
For a full example of a project using @snaplet/seed
with Supabase including:
- seeding data
- seeding with e2e tests
- seeding with supabase created user pool
check out the example project (opens in a new tab)