Giter Club home page Giter Club logo

follicle's Introduction

Ijin Project

Welcome to the Ijin Project! This guide will help you set up your development environment and go through some of the important aspects of the project, including Prisma setup and known issues.

Table of Contents

  1. Getting Started
  2. Prisma Configuration
  3. Known Issues with Prisma
  4. Functions

Getting Started

To get started, you'll need to run the development server. Use one of the following commands:

yarn dev

After running the server, open http://localhost:3000 in your browser to see the result.


Prisma Configuration

We use Prisma connected to Supabase. If you make changes to the schema.prisma file, make sure to run the following command:

yarn run migrate-dev

Known Issues with Prisma

โš ๏ธ Important: If in any doubt, contact Komang!

Recovery Steps

If you encounter the missing grants issue, follow these steps:

  1. Create a draft migration:
prisma migrate dev --create-only
  1. Add the following SQL to supabe SQL editor Supabase SQL Editor:
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;

grant all privileges on all tables in schema auth to postgres, service_role;
grant all privileges on all functions in schema auth to postgres, service_role;
grant all privileges on all sequences in schema auth to postgres, service_role;

alter default privileges in schema auth grant all on tables to postgres, service_role;
alter default privileges in schema auth grant all on functions to postgres, service_role;
alter default privileges in schema auth grant all on sequences to postgres, service_role;
  1. Apply the draft migration:
prisma migrate dev
  1. Re-add the triggers to update the update_at fields on document and document_versions using Supabase SQL Editor:
-- Enable MODDATETIME extension
CREATE EXTENSION IF NOT EXISTS moddatetime SCHEMA extensions;

-- Trigger to update `updated_at` in documents
CREATE TRIGGER handle_updated_at BEFORE UPDATE ON documents
FOR EACH ROW EXECUTE PROCEDURE moddatetime (updated_at);

-- Trigger to update `updated_at` in document_versions
CREATE TRIGGER handle_updated_at BEFORE UPDATE ON document_versions
FOR EACH ROW EXECUTE PROCEDURE moddatetime (updated_at);

-- Function to update documents.updated_at when document_versions is updated
CREATE OR REPLACE FUNCTION update_document_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  UPDATE documents SET updated_at = NOW() WHERE id = NEW.document_id;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Trigger to call update_document_timestamp when document_versions is updated
CREATE TRIGGER handle_document_version_updated
AFTER UPDATE ON document_versions
FOR EACH ROW EXECUTE FUNCTION update_document_timestamp();
  1. [UNTESTED] Re-add the triggers to create profile documents and profile projects when a document or a profile is created using the Supabase SQL Editor:
-- Row Level Security for profiles
ALTER TABLE profiles
  ENABLE ROW LEVEL SECURITY;

-- Policies for profiles
CREATE POLICY "Public profiles are viewable by everyone." ON profiles
  FOR SELECT USING (true);

CREATE POLICY "Users can insert their own profile." ON profiles
  FOR INSERT WITH CHECK (auth.uid() = id);

CREATE POLICY "Users can update own profile." ON profiles
  FOR UPDATE USING (auth.uid() = id);

CREATE POLICY "Users can delete own profile." ON profiles
  FOR DELETE USING (auth.uid() = id);

-- Trigger Function for New User Created
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO public.profiles (id, auth_user_id, username, full_name, avatar_url, email)
  VALUES (
    NEW.id,
    NEW.id,
    COALESCE(NEW.raw_user_meta_data->>'username', COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.email)),
    COALESCE(NEW.raw_user_meta_data->>'full_name', 'Anonymous'),
    COALESCE(NEW.raw_user_meta_data->>'avatar_url', ''),
    COALESCE(NEW.raw_user_meta_data->>'email', NEW.email)
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Trigger for New User Created
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_user();

-- Trigger Function for Project Created
CREATE OR REPLACE FUNCTION public.handle_new_project()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO public.profile_projects (project_id, profile_id)
  VALUES (
    NEW.id,
    auth.uid()
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Trigger for Project Created
CREATE TRIGGER on_project_created
AFTER INSERT ON public.projects
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_project();

-- Trigger Function for Document Created
CREATE OR REPLACE FUNCTION public.handle_new_document()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO public.profile_documents (document_id, profile_id)
  VALUES (
    NEW.id,
    auth.uid()
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Trigger for Document Created
CREATE TRIGGER on_document_created
AFTER INSERT ON public.documents
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_document();

Functions

Trigger Functions

TODO - MOVE THESE INTO THE SQL EDITOR QUERY INSTEAD OF ADDING ONE BY ONE

Done here Supabase Function Dashboard Then triggered from here Supabase Trigger Dashboard

  1. on_auth_user_created
BEGIN
  INSERT INTO public.profiles (id, auth_user_id, username, full_name, avatar_url, email)
  VALUES (
    NEW.id,
    NEW.id,
    COALESCE(NEW.raw_user_meta_data->>'username', COALESCE(NEW.raw_user_meta_data->>'full_name', NEW.email)),
    COALESCE(NEW.raw_user_meta_data->>'full_name', 'Anonymous'),
    COALESCE(NEW.raw_user_meta_data->>'avatar_url', ''),
    COALESCE(NEW.raw_user_meta_data->>'email', NEW.email)
  );
  RETURN NEW;
END;
  1. on_project_created
BEGIN
INSERT INTO public.profile_projects (project_id, profile_id, scope_access)
VALUES (
  NEW.id,
  auth.uid(),
  'admin'
);
RETURN NEW;
END;
  1. on_document_created
BEGIN
INSERT INTO public.profile_documents (document_id, profile_id, scope_access)
VALUES (
  NEW.id,
  auth.uid(),
  'admin'
);
RETURN NEW;
END;
  1. on_profile_created
BEGIN
INSERT INTO public.profile_organizations (profile_id, scope_access)
VALUES (
  auth.uid(),
  'write'
);
RETURN NEW;
END;

follicle's People

Contributors

cosmocochrane1 avatar kopisusu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.