Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve developing docs and bring in timeout migration #256

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/.CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main/maintainers
48 changes: 46 additions & 2 deletions docs/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,53 @@

Local development of Playabl uses Docker to run a containerized version of the backend alongside a local copy of the UI. The Netlify service is also supported locally and can be run to test serverless functions. There is also some AWS in the codebase, but Netlify is typically preferred for serverless functions.

## Supabase setup
## Environment setup

The local workflow is based on Supabase's guide for [local development](https://supabase.com/docs/guides/cli/local-development) with supabase CLI.
_Note: these steps have only been tested on M1 Mac. If you want to develop on Linux or Windows you may need to alter these commands_

**Prereqs**

- Docker
- Node v20 or greater

The local workflow is based on Supabase's guide for [local development](https://supabase.com/docs/guides/cli/local-development) with supabase CLI. It is **highly** recommended you read this guide and at least be familiar with its contents before starting. This guide has all the info you need for how to do various tasks like starting services, stopping services, resetting the DB, creating a migration, etc.

**Install packages**

`npm install`

**Start supabase**

`npx supabase start`

This will pull the docker images and start the containers. At the end of it, you will be given an output of URLs and keys to use for local development. Some of these will need to be set in a `.env` file. Here is a sample output:

```
Started supabase local development setup.

API URL: http://127.0.0.1:54321
GraphQL URL: http://127.0.0.1:54321/graphql/v1
DB URL: postgresql://postgres:[email protected]:54322/postgres
Studio URL: http://127.0.0.1:54323
Inbucket URL: http://127.0.0.1:54324
JWT secret: super-secret-jwt-token-with-at-least-32-characters-long
anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
```

**Set env variables**

Set these values in a `.env` file at the root of the project:

```
VITE_SUPABASE_URL=<API_URL>
VITE_SUPABASE_ANON_KEY=<anon_key>
SUPABASE_URL=<API_URL>
SUPABASE_SERVICE_ROLE=<service_role_key>
VITE_PLAYABL_URL=http://localhost:8888
```

You may see other environement variables in the code, but these aren't typically necessary for normal development. If you want to work on something that does require it, like AWS, I'll work with you to figure out the right values.

### Storage (optional)

Expand Down
76 changes: 38 additions & 38 deletions src/typings/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Json =
| { [key: string]: Json | undefined }
| Json[]

export interface Database {
export type Database = {
public: {
Tables: {
access_levels: {
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "communities"
referencedColumns: ["id"]
}
},
]
}
communities: {
Expand Down Expand Up @@ -148,7 +148,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
community_access: {
Expand Down Expand Up @@ -194,7 +194,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
community_events: {
Expand Down Expand Up @@ -247,7 +247,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "communities"
referencedColumns: ["id"]
}
},
]
}
community_invites: {
Expand Down Expand Up @@ -276,7 +276,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "communities"
referencedColumns: ["id"]
}
},
]
}
community_membership_requests: {
Expand Down Expand Up @@ -315,7 +315,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
community_memberships: {
Expand Down Expand Up @@ -361,7 +361,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
draft_games: {
Expand Down Expand Up @@ -402,7 +402,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
flags: {
Expand Down Expand Up @@ -455,7 +455,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "games"
referencedColumns: ["id"]
}
},
]
}
games: {
Expand Down Expand Up @@ -534,7 +534,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "community_events"
referencedColumns: ["id"]
}
},
]
}
integrations: {
Expand Down Expand Up @@ -584,7 +584,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "communities"
referencedColumns: ["id"]
}
},
]
}
messages: {
Expand Down Expand Up @@ -664,7 +664,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "profiles"
referencedColumns: ["id"]
}
},
]
}
profiles: {
Expand Down Expand Up @@ -794,7 +794,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "games"
referencedColumns: ["id"]
}
},
]
}
}
Expand Down Expand Up @@ -978,7 +978,7 @@ export interface Database {
isOneToOne: false
referencedRelation: "buckets"
referencedColumns: ["id"]
}
},
]
}
}
Expand Down Expand Up @@ -1057,7 +1057,7 @@ export type Tables<
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
Database[PublicTableNameOrOptions["schema"]]["Views"])
: never = never
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends {
Expand All @@ -1066,67 +1066,67 @@ export type Tables<
? R
: never
: PublicTableNameOrOptions extends keyof (Database["public"]["Tables"] &
Database["public"]["Views"])
? (Database["public"]["Tables"] &
Database["public"]["Views"])[PublicTableNameOrOptions] extends {
Row: infer R
}
? R
Database["public"]["Views"])
? (Database["public"]["Tables"] &
Database["public"]["Views"])[PublicTableNameOrOptions] extends {
Row: infer R
}
? R
: never
: never
: never

export type TablesInsert<
PublicTableNameOrOptions extends
| keyof Database["public"]["Tables"]
| { schema: keyof Database },
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
Insert: infer I
}
? I
: never
: PublicTableNameOrOptions extends keyof Database["public"]["Tables"]
? Database["public"]["Tables"][PublicTableNameOrOptions] extends {
Insert: infer I
}
? I
? Database["public"]["Tables"][PublicTableNameOrOptions] extends {
Insert: infer I
}
? I
: never
: never
: never

export type TablesUpdate<
PublicTableNameOrOptions extends
| keyof Database["public"]["Tables"]
| { schema: keyof Database },
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
: never = never
: never = never,
> = PublicTableNameOrOptions extends { schema: keyof Database }
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
Update: infer U
}
? U
: never
: PublicTableNameOrOptions extends keyof Database["public"]["Tables"]
? Database["public"]["Tables"][PublicTableNameOrOptions] extends {
Update: infer U
}
? U
? Database["public"]["Tables"][PublicTableNameOrOptions] extends {
Update: infer U
}
? U
: never
: never
: never

export type Enums<
PublicEnumNameOrOptions extends
| keyof Database["public"]["Enums"]
| { schema: keyof Database },
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
: never = never
: never = never,
> = PublicEnumNameOrOptions extends { schema: keyof Database }
? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName]
: PublicEnumNameOrOptions extends keyof Database["public"]["Enums"]
? Database["public"]["Enums"][PublicEnumNameOrOptions]
: never
? Database["public"]["Enums"][PublicEnumNameOrOptions]
: never

47 changes: 47 additions & 0 deletions supabase/migrations/20240223203511_remote_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
drop trigger if exists "send_notification" on "public"."notifications";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.dispatch_unread_notifications_emails()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$declare
row record;
begin
for row in select p.id, n.email, p.username, count(n.email)
from notifications n
inner join profiles p
on p.id = n.user_id
and (p.email_preferences->>'email_enabled')::boolean is true
and (p.email_preferences->>'unread_notifications_enabled')::boolean is true
and n.read = false
and n.created_at > current_timestamp - interval '1 day'
group by (p.id, n.email, p.username)
loop
perform net.http_post(
'https://app.playabl.io/.netlify/functions/unreadNotificationsEmail',
jsonb_build_object(
'user_id',
row.id,
'email',
row.email,
'username',
row.username,
'count',
row.count
),
'{}',
jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', '2gCvGM1WXv2i5Y3LzDT6kZfUqzimkEzvmwn31CtiyWg2'
),
5000
);
end loop;
end;$function$
;

CREATE TRIGGER send_notification AFTER INSERT ON public.notifications FOR EACH ROW EXECUTE FUNCTION supabase_functions.http_request('https://app.playabl.io/.netlify/functions/notify', 'POST', '{"Content-type":"application/json","Authorization":"2gCvGM1WXv2i5Y3LzDT6kZfUqzimkEzvmwn31CtiyWg2"}', '{}', '5000');


Loading