flora-liu / casa-kind

🀍 self-care web app using nextjs, supabase, chatgpt, and tailwind

Home Page:https://casa-kind.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🀍 Casa Kind

Web application to support mental, emotional wellness and self-development.

Tech Stack

Getting Started

Environment

Add the following to .env.local.

# PostgreSQL connection string used for migrations by Prisma Client
DIRECT_URL=""
# PostgreSQL connection string with pgBouncer config β€” used by Prisma Client
DATABASE_URL=""
OPENAI_API_KEY=""
NEXT_PUBLIC_SUPABASE_URL=""
NEXT_PUBLIC_SUPABASE_ANON_KEY=""

For deploying to Vercel, you will need to add these environment variables to your deployment settings.

Supabase

Set up database trigger to insert auth.users into public.profile

Since we are using Supabase Auth for user authentication, the auth.users table stores the user account information. To copy this user data to our public.profile table, add the following database trigger. You can run this SQL in the Supabase SQL Editor directly.

-- inserts a row into public.profile
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  insert into public.profile (id, email)
  values (new.id, new.email);
  return new;
end;
$$;

-- trigger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

We also want to add a trigger to delete the `public.profile`` row before a user is deleted.

create or replace function delete_old_profile()
returns trigger
language 'plpgsql'
security definer
as $$
begin
  delete from public.profiles where id = old.id;
  return old;
end;
$$;

create trigger before_delete_user
  before delete on auth.users
  for each row execute function public.delete_old_profile();

Generate database types

  1. Create a new file at src/lib/database/types.ts

  2. Run the command

    npx supabase gen types typescript --project-id "$PROJECT_REF" --schema public > src/lib/database/types.ts --project-id <supabase_project_id>

Prisma

Create a migration

  1. Make a change to prisma/schema.prisma

  2. Push change to sync Prisma schema with database schema

    prisma migrate dev
  3. Generate updated Prisma Client with the following command:

    sudo prisma generate
  4. Update local database types

    npx supabase gen types typescript --project-id "$PROJECT_REF" --schema public > src/lib/database/types.ts --project-id <supabase_project_id>

Seed database

  1. Add seed data to src/app/seed.ts

  2. Run seed command

    npx prisma db seed
    

Pull schema from remote

If changes are made to Supabase directly, pull the remote database schema for Prisma by running: prisma db pull

Development

Run application locally

First, run the development server:

npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev

Open http://localhost:3000 with your browser to see the result.

Generate prompts

To test generating the daily prompt:

curl localhost:3000/api/journal/prompt/generate

Troubleshooting

Missing grants

If your database schema is out of sync from your migration history, prisma migrate dev will detect a migration history conflict or a schema drift. When prisma migrate dev detects the drift, it might ask to to reset your database schema. If you choose yes, it will delete the public schema along with the default grants defined in your database.

If you run into this problem, create a draft migration using prisma migrate dev --create-only, and add the following helper SQL:

grant usage on schema public to postgres, anon, authenticated, service_role;

grant all privileges on all tables in schema public to postgres, anon, authenticated, service_role;
grant all privileges on all functions in schema public to postgres, anon, authenticated, service_role;
grant all privileges on all sequences in schema public to postgres, anon, authenticated, service_role;

alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role;

Unable to fetch Supabase data

If experiencing client permission denied for public schema, run the following in the Supabase SQL Editor:

grant usage on schema "public" to anon;
grant usage on schema "public" to authenticated;

GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO authenticated;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO anon;

About

🀍 self-care web app using nextjs, supabase, chatgpt, and tailwind

https://casa-kind.com


Languages

Language:TypeScript 99.3%Language:CSS 0.6%Language:JavaScript 0.1%