feat(supabase): @faunosteo/supabase package with PGlite harness and typed client
- tests/db: PGlite (PostgreSQL 17 WASM) applies the Supabase shim + migrations and runs supabase/tests/*.sql through pgTAP without Docker - schema-types test pins tables, columns, enums, RPC args, RLS-on, no using(true) - database.types.ts in supabase gen types format; createBrowserClient (PKCE) - Supabase CLI pinned as root devDependency; db:* root scripts
This commit is contained in:
1 parent
2d5ff0c33e
commit
ce76c6597a
15 files changed
+1020
-2
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
|
||||
import type { Database } from './database.types'
|
||||
|
||||
/** Typed Supabase client for the Faunosteo database. */
|
||||
export type FaunosteoClient = SupabaseClient<Database>
|
||||
|
||||
/**
|
||||
* Public connection settings. The anon (publishable) key is safe to ship to
|
||||
* the browser: every table is behind RLS. The service-role key never is.
|
||||
*/
|
||||
export interface SupabaseConnection {
|
||||
url: string
|
||||
anonKey: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Browser client: session persisted in localStorage, tokens refreshed in the
|
||||
* background, PKCE for OAuth and magic-link redirects.
|
||||
*/
|
||||
export function createBrowserClient(connection: SupabaseConnection): FaunosteoClient {
|
||||
return createClient<Database>(connection.url, connection.anonKey, {
|
||||
auth: {
|
||||
persistSession: true,
|
||||
autoRefreshToken: true,
|
||||
detectSessionInUrl: true,
|
||||
flowType: 'pkce',
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
// Database types in the `supabase gen types typescript` format.
|
||||
// Regenerate after every migration: `pnpm db:types` (needs the local stack);
|
||||
// CI runs `pnpm db:types:check`. packages/supabase/tests/db/schema-types.test.ts
|
||||
// checks tables, columns, enums and RPC names against the migrated schema.
|
||||
export type Json =
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| null
|
||||
| { [key: string]: Json | undefined }
|
||||
| Json[]
|
||||
|
||||
export type Database = {
|
||||
// Allows to automatically instantiate createClient with right options
|
||||
// instead of createClient<Database, { PostgrestVersion: 'XX' }>(URL, KEY)
|
||||
__InternalSupabase: {
|
||||
PostgrestVersion: "13"
|
||||
}
|
||||
public: {
|
||||
Tables: {
|
||||
profile_roles: {
|
||||
Row: {
|
||||
granted_at: string
|
||||
granted_by: string | null
|
||||
id: string
|
||||
revoke_reason: string | null
|
||||
revoked_at: string | null
|
||||
revoked_by: string | null
|
||||
role: Database["public"]["Enums"]["platform_role"]
|
||||
user_id: string
|
||||
}
|
||||
Insert: {
|
||||
granted_at?: string
|
||||
granted_by?: string | null
|
||||
id?: string
|
||||
revoke_reason?: string | null
|
||||
revoked_at?: string | null
|
||||
revoked_by?: string | null
|
||||
role: Database["public"]["Enums"]["platform_role"]
|
||||
user_id: string
|
||||
}
|
||||
Update: {
|
||||
granted_at?: string
|
||||
granted_by?: string | null
|
||||
id?: string
|
||||
revoke_reason?: string | null
|
||||
revoked_at?: string | null
|
||||
revoked_by?: string | null
|
||||
role?: Database["public"]["Enums"]["platform_role"]
|
||||
user_id?: string
|
||||
}
|
||||
Relationships: [
|
||||
{
|
||||
foreignKeyName: "profile_roles_granted_by_fkey"
|
||||
columns: ["granted_by"]
|
||||
isOneToOne: false
|
||||
referencedRelation: "profiles"
|
||||
referencedColumns: ["id"]
|
||||
},
|
||||
{
|
||||
foreignKeyName: "profile_roles_revoked_by_fkey"
|
||||
columns: ["revoked_by"]
|
||||
isOneToOne: false
|
||||
referencedRelation: "profiles"
|
||||
referencedColumns: ["id"]
|
||||
},
|
||||
{
|
||||
foreignKeyName: "profile_roles_user_id_fkey"
|
||||
columns: ["user_id"]
|
||||
isOneToOne: false
|
||||
referencedRelation: "profiles"
|
||||
referencedColumns: ["id"]
|
||||
},
|
||||
]
|
||||
}
|
||||
profiles: {
|
||||
Row: {
|
||||
active: boolean
|
||||
created_at: string
|
||||
display_name: string
|
||||
first_name: string | null
|
||||
id: string
|
||||
last_name: string | null
|
||||
locale: string
|
||||
phone: string | null
|
||||
phone_verified_at: string | null
|
||||
timezone: string
|
||||
updated_at: string
|
||||
}
|
||||
Insert: {
|
||||
active?: boolean
|
||||
created_at?: string
|
||||
display_name: string
|
||||
first_name?: string | null
|
||||
id: string
|
||||
last_name?: string | null
|
||||
locale?: string
|
||||
phone?: string | null
|
||||
phone_verified_at?: string | null
|
||||
timezone?: string
|
||||
updated_at?: string
|
||||
}
|
||||
Update: {
|
||||
active?: boolean
|
||||
created_at?: string
|
||||
display_name?: string
|
||||
first_name?: string | null
|
||||
id?: string
|
||||
last_name?: string | null
|
||||
locale?: string
|
||||
phone?: string | null
|
||||
phone_verified_at?: string | null
|
||||
timezone?: string
|
||||
updated_at?: string
|
||||
}
|
||||
Relationships: []
|
||||
}
|
||||
}
|
||||
Views: {
|
||||
[_ in never]: never
|
||||
}
|
||||
Functions: {
|
||||
grant_platform_role: {
|
||||
Args: {
|
||||
role: Database["public"]["Enums"]["platform_role"]
|
||||
target_user_id: string
|
||||
}
|
||||
Returns: string
|
||||
}
|
||||
revoke_platform_role: {
|
||||
Args: {
|
||||
reason?: string
|
||||
role: Database["public"]["Enums"]["platform_role"]
|
||||
target_user_id: string
|
||||
}
|
||||
Returns: string
|
||||
}
|
||||
}
|
||||
Enums: {
|
||||
platform_role: "owner" | "admin" | "moderator" | "support"
|
||||
}
|
||||
CompositeTypes: {
|
||||
[_ in never]: never
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type DatabaseWithoutInternals = Omit<Database, "__InternalSupabase">
|
||||
|
||||
type DefaultSchema = DatabaseWithoutInternals[Extract<keyof DatabaseWithoutInternals, "public">]
|
||||
|
||||
export type Tables<
|
||||
DefaultSchemaTableNameOrOptions extends
|
||||
| keyof (DefaultSchema["Tables"] & DefaultSchema["Views"])
|
||||
| { schema: keyof DatabaseWithoutInternals },
|
||||
TableName extends DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? keyof (DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"] &
|
||||
DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Views"])
|
||||
: never = never,
|
||||
> = DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? (DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"] &
|
||||
DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Views"])[TableName] extends {
|
||||
Row: infer R
|
||||
}
|
||||
? R
|
||||
: never
|
||||
: DefaultSchemaTableNameOrOptions extends keyof (DefaultSchema["Tables"] &
|
||||
DefaultSchema["Views"])
|
||||
? (DefaultSchema["Tables"] &
|
||||
DefaultSchema["Views"])[DefaultSchemaTableNameOrOptions] extends {
|
||||
Row: infer R
|
||||
}
|
||||
? R
|
||||
: never
|
||||
: never
|
||||
|
||||
export type TablesInsert<
|
||||
DefaultSchemaTableNameOrOptions extends
|
||||
| keyof DefaultSchema["Tables"]
|
||||
| { schema: keyof DatabaseWithoutInternals },
|
||||
TableName extends DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? keyof DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"]
|
||||
: never = never,
|
||||
> = DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"][TableName] extends {
|
||||
Insert: infer I
|
||||
}
|
||||
? I
|
||||
: never
|
||||
: DefaultSchemaTableNameOrOptions extends keyof DefaultSchema["Tables"]
|
||||
? DefaultSchema["Tables"][DefaultSchemaTableNameOrOptions] extends {
|
||||
Insert: infer I
|
||||
}
|
||||
? I
|
||||
: never
|
||||
: never
|
||||
|
||||
export type TablesUpdate<
|
||||
DefaultSchemaTableNameOrOptions extends
|
||||
| keyof DefaultSchema["Tables"]
|
||||
| { schema: keyof DatabaseWithoutInternals },
|
||||
TableName extends DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? keyof DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"]
|
||||
: never = never,
|
||||
> = DefaultSchemaTableNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? DatabaseWithoutInternals[DefaultSchemaTableNameOrOptions["schema"]]["Tables"][TableName] extends {
|
||||
Update: infer U
|
||||
}
|
||||
? U
|
||||
: never
|
||||
: DefaultSchemaTableNameOrOptions extends keyof DefaultSchema["Tables"]
|
||||
? DefaultSchema["Tables"][DefaultSchemaTableNameOrOptions] extends {
|
||||
Update: infer U
|
||||
}
|
||||
? U
|
||||
: never
|
||||
: never
|
||||
|
||||
export type Enums<
|
||||
DefaultSchemaEnumNameOrOptions extends
|
||||
| keyof DefaultSchema["Enums"]
|
||||
| { schema: keyof DatabaseWithoutInternals },
|
||||
EnumName extends DefaultSchemaEnumNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? keyof DatabaseWithoutInternals[DefaultSchemaEnumNameOrOptions["schema"]]["Enums"]
|
||||
: never = never,
|
||||
> = DefaultSchemaEnumNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? DatabaseWithoutInternals[DefaultSchemaEnumNameOrOptions["schema"]]["Enums"][EnumName]
|
||||
: DefaultSchemaEnumNameOrOptions extends keyof DefaultSchema["Enums"]
|
||||
? DefaultSchema["Enums"][DefaultSchemaEnumNameOrOptions]
|
||||
: never
|
||||
|
||||
export type CompositeTypes<
|
||||
PublicCompositeTypeNameOrOptions extends
|
||||
| keyof DefaultSchema["CompositeTypes"]
|
||||
| { schema: keyof DatabaseWithoutInternals },
|
||||
CompositeTypeName extends PublicCompositeTypeNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? keyof DatabaseWithoutInternals[PublicCompositeTypeNameOrOptions["schema"]]["CompositeTypes"]
|
||||
: never = never,
|
||||
> = PublicCompositeTypeNameOrOptions extends {
|
||||
schema: keyof DatabaseWithoutInternals
|
||||
}
|
||||
? DatabaseWithoutInternals[PublicCompositeTypeNameOrOptions["schema"]]["CompositeTypes"][CompositeTypeName]
|
||||
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
|
||||
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
|
||||
: never
|
||||
|
||||
export const Constants = {
|
||||
public: {
|
||||
Enums: {
|
||||
platform_role: ["owner", "admin", "moderator", "support"],
|
||||
},
|
||||
},
|
||||
} as const
|
||||
@@ -0,0 +1,23 @@
|
||||
export {
|
||||
createBrowserClient,
|
||||
type FaunosteoClient,
|
||||
type SupabaseConnection,
|
||||
} from './client'
|
||||
export type {
|
||||
Database,
|
||||
Enums,
|
||||
Json,
|
||||
Tables,
|
||||
TablesInsert,
|
||||
TablesUpdate,
|
||||
} from './database.types'
|
||||
export { Constants } from './database.types'
|
||||
|
||||
import type { Enums, Tables } from './database.types'
|
||||
|
||||
/** Row of public.profiles. */
|
||||
export type Profile = Tables<'profiles'>
|
||||
/** Row of public.profile_roles. */
|
||||
export type ProfileRole = Tables<'profile_roles'>
|
||||
/** owner | admin | moderator | support. Not a professional status. */
|
||||
export type PlatformRole = Enums<'platform_role'>
|
||||
Reference in new issue
Block a user