Files
SvelteKitSaasBoilerplate/docs/database-setup.md
T
2024-09-17 21:14:37 +01:00

2.8 KiB

Database Setup

Databases are stored in Supabase. The auth table for signing up and logging in users is created automatically when you set up Supabase Auth.

Initial Setup

To add the tables for recording their active subscriptions and bought products, as well as products and prices, you can use the SQL scripts from the supabase/migrations folder in the Supabase SQL Editor.

Generating Types

To get the types for the tables, you can follow these instructions taken from here:

npm i supabase@">=1.8.1" --save-dev

npx supabase login

npx supabase init

Replace $PROJECT_REF with your project reference: npx supabase gen types typescript --project-id "$PROJECT_REF" --schema public > types/supabase.ts

Replace types/supabase.d.ts with whatever file you want your types in: npx supabase gen types typescript --local > types/supabase.d.ts

Local DB Development

Ensure you have Docker installed and running.

You can start and stop the local DB environment with:

npx supabase start

npx supabase stop

Migrations

There are two options to create a migration file:

  1. Writing the SQL manually

    • Create new blank migration file

      npx supabase migration new <migration_name>

    • Add the SQL you want to deploy as part of the migration e.g.

      create table
      employees (
      id bigint primary key generated always as identity,
      name text,
      email text,
      created_at timestamptz default now()
      );
      
    • Reset the DB and apply the latest migrations

      npx supabase db reset

  2. Generate SQL based on differences to the schema

    • With the local DB running, enter the studio and make your changes to the schema

    • Generate the migration file containing the SQL with the differences in the schema

      npx supabase db diff -f <migration_name>

Applying migrations

Reset database in cloud

Prerequisite login using npx supabase login

npx supabase db reset --linked

Enabling sending emails with Brevo

  1. Go to the Project Settings in Supabase, then Authentication, then SMTP Settings.
  2. Enable custom SMTP and fill in the required fields with your Brevo credentials.
  • You need to have an email address that you can send stuff from
  • You need to have a Brevo account
  • sender email as noreply@yourdomain.com
  1. https://app.brevo.com/senders/list to add senders
  2. Add a sender for noreply@yourdomain.com
  3. Verify your email domain via DKIM or DMARC
  4. In Brevo go to https://app.brevo.com/settings/keys/smtp to see your SMTP credentials.
  5. Fill in the SMTP credentials in Supabase.