From 9685f117157d52c7e9db73bf1079513aa84595f5 Mon Sep 17 00:00:00 2001 From: David Day Date: Wed, 11 Sep 2024 15:00:25 -0700 Subject: [PATCH] Pull database schema. --- README.md | 7 ++++ src/utils/database.types.ts | 39 ++++++++++++++++++- .../20240911215912_remote_schema.sql | 23 +++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 supabase/migrations/20240911215912_remote_schema.sql diff --git a/README.md b/README.md index 91dfb43..8e1b164 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,13 @@ Supabase 提供 **Edge Functions**,這些是基於 [Deno](https://deno.land/) 如果你想修改資料庫欄位,請在本地端先進行開發測試,新增 migration 後再提 PR 請求更動正式伺服器。 請參考 Supabase 官方文件的 [Database Migration](https://supabase.com/docs/guides/cli/local-development#database-migrations) 部分 +開發完成後,請將變更記錄到 Repository 裡面 + +```sh +npx supabase gen types typescript --local > src/utils/database.types.ts +npx supabase db pull --local +``` + ## 開發 我們使用 **ESLint** 來維護程式碼品質,並使用 **TypeScript** 進行型別檢查。這有助於提前發現錯誤,並使程式碼更容易理解。 diff --git a/src/utils/database.types.ts b/src/utils/database.types.ts index 5dbcf28..fb4cc72 100644 --- a/src/utils/database.types.ts +++ b/src/utils/database.types.ts @@ -7,6 +7,31 @@ export type Json = | Json[] export type Database = { + graphql_public: { + Tables: { + [_ in never]: never + } + Views: { + [_ in never]: never + } + Functions: { + graphql: { + Args: { + operationName?: string + query?: string + variables?: Json + extensions?: Json + } + Returns: Json + } + } + Enums: { + [_ in never]: never + } + CompositeTypes: { + [_ in never]: never + } + } public: { Tables: { events: { @@ -18,6 +43,7 @@ export type Database = { id: number name: string | null start_time: string | null + user_id: string } Insert: { created_at?: string @@ -27,6 +53,7 @@ export type Database = { id?: number name?: string | null start_time?: string | null + user_id: string } Update: { created_at?: string @@ -36,8 +63,17 @@ export type Database = { id?: number name?: string | null start_time?: string | null + user_id?: string } - Relationships: [] + Relationships: [ + { + foreignKeyName: "events_user_id_fkey" + columns: ["user_id"] + isOneToOne: false + referencedRelation: "users" + referencedColumns: ["id"] + }, + ] } sales: { Row: { @@ -168,3 +204,4 @@ export type Enums< : PublicEnumNameOrOptions extends keyof PublicSchema["Enums"] ? PublicSchema["Enums"][PublicEnumNameOrOptions] : never + diff --git a/supabase/migrations/20240911215912_remote_schema.sql b/supabase/migrations/20240911215912_remote_schema.sql new file mode 100644 index 0000000..1f3d893 --- /dev/null +++ b/supabase/migrations/20240911215912_remote_schema.sql @@ -0,0 +1,23 @@ +alter table "public"."events" add column "user_id" uuid not null; + +alter table "public"."events" add constraint "events_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON UPDATE CASCADE ON DELETE CASCADE not valid; + +alter table "public"."events" validate constraint "events_user_id_fkey"; + +create policy "Enable insert for authenticated users only" +on "public"."events" +as permissive +for insert +to authenticated +with check (true); + + +create policy "Enable update for users based on user_id" +on "public"."events" +as permissive +for update +to public +using ((( SELECT auth.uid() AS uid) = user_id)); + + +