GuidesDatabase

MongoDB

Setup

  • Create a new project and deploy a cluster on MongoDB Atlas
  • Run a local database for your dev setup so you can work offline and it's faster. You can read more here.

  • In your project on MongoDB Altas, click [Network Access] then [+ Add IP Address]. Enter 0.0.0.0/0 in [Access List Entry]. This allows connections from your computer and your production deployment(s) (Vercel for instance).
  • If you haven't done it yet, rename .env.example to .env.local. Then add your connection string to MONGODB_URI in .env.local.

Mongoose (Optional)

Mongoose makes it easier to deal with MongoDB and has some cool features.

Models are defined in the folder /models. Add any new models there.

The plugin toJSON is added to all models to remove the _id and __v (easier on front-end). Also if you add private: true to any field it will be removed from the response. I.e. make email private so it's not sent to the front-end.

Supabase

Setup

  • In Supabase SQL Editor, run this query to add a profiles table (an extension of the authenticated user to store data like Stripe customer_id, subscription access, etc...):
  • Supabase SQL Editor

    1create table public.profiles (
    2  id uuid not null references auth.users on delete cascade,
    3  customer_id text,
    4  price_id text,
    5  has_access boolean,
    6  email text,
    7
    8  primary key (id)
    9);
    10
    11alter table public.profiles enable row level security;
  • Go to the new profiles table and add 2 RLS policies:
    • Enable read access for authenticated users only
    • Enable insert access for authenticated users only
    Supabase RLS - Enable read access for authenticated users onlySupabase RLS - Enable insert access for authenticated users only
  • (Optional) If you want to collect leads with ButtonLead, create a new table called leads and add a RLS policy with insert access for anyone:
  • Supabase SQL Editor

    1create table public.leads (
    2  id uuid default gen_random_uuid(),
    3  email text,
    4  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
    5
    6  primary key (id)
    7);
    8
    9alter table public.leads enable row level security;

Migrate to SSR