-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create supabase lib and actions files
- Loading branch information
1 parent
7c28d72
commit 543e81b
Showing
11 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use server' |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use server' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { createBrowserClient } from '@supabase/ssr' | ||
|
||
const SUPABASE_URL = process.env.SUPABASE_URL | ||
const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY | ||
|
||
if (!SUPABASE_URL || !SUPABASE_ANON_KEY) { | ||
throw new Error('Missing Supabase environment variables') | ||
} | ||
|
||
const createClient = () => { | ||
return createBrowserClient(SUPABASE_URL, SUPABASE_ANON_KEY) | ||
} | ||
|
||
const supabase = createClient() | ||
|
||
type HandleNewValueType = (payload: any) => void | ||
|
||
export const onInsertListener = ({ | ||
tableName, | ||
handleNewValue | ||
}: { | ||
tableName: string | ||
handleNewValue: HandleNewValueType | ||
}) => { | ||
return supabase | ||
.channel(`public:${tableName}`) | ||
.on('postgres_changes', { event: 'INSERT', schema: 'public', table: tableName }, (payload) => | ||
handleNewValue(payload.new) | ||
) | ||
.subscribe() as any | ||
} | ||
|
||
export const onDeleteListener = ({ | ||
tableName, | ||
handleNewValue | ||
}: { | ||
tableName: string | ||
handleNewValue: HandleNewValueType | ||
}) => { | ||
return supabase | ||
.channel(`public:${tableName}`) | ||
.on('postgres_changes', { event: 'DELETE', schema: 'public', table: tableName }, (payload) => | ||
handleNewValue(payload.old) | ||
) | ||
.subscribe() as any | ||
} | ||
|
||
export const removeListener = ({ listener }: { listener: any }) => { | ||
supabase.removeChannel(supabase.channel(listener)) | ||
} | ||
|
||
export { supabase } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { cookies } from 'next/headers' | ||
import { createServerClient } from '@supabase/ssr' | ||
|
||
const SUPABASE_URL = process.env.SUPABASE_URL | ||
const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY | ||
|
||
if (!SUPABASE_URL || !SUPABASE_ANON_KEY) { | ||
throw new Error('Missing Supabase environment variables') | ||
} | ||
|
||
type HandleNewValueType = (payload: any) => void | ||
|
||
const supabase = createServerClient(SUPABASE_URL, SUPABASE_ANON_KEY, { | ||
cookies: { | ||
getAll() { | ||
return cookies().getAll() | ||
}, | ||
setAll(cookiesToSet) { | ||
try { | ||
cookiesToSet.forEach(({ name, value, options }) => cookies().set(name, value, options)) | ||
} catch { | ||
// The `setAll` method was called from a Server Component. | ||
// This can be ignored if you have middleware refreshing | ||
// user sessions. | ||
} | ||
} | ||
} | ||
}) | ||
|
||
export const onInsertListener = ({ | ||
tableName, | ||
handleNewValue | ||
}: { | ||
tableName: string | ||
handleNewValue: HandleNewValueType | ||
}) => { | ||
return supabase | ||
.channel(`public:${tableName}`) | ||
.on('postgres_changes', { event: 'INSERT', schema: 'public', table: tableName }, (payload) => | ||
handleNewValue(payload.new) | ||
) | ||
.subscribe() as any | ||
} | ||
|
||
export const onDeleteListener = ({ | ||
tableName, | ||
handleNewValue | ||
}: { | ||
tableName: string | ||
handleNewValue: HandleNewValueType | ||
}) => { | ||
return supabase | ||
.channel(`public:${tableName}`) | ||
.on('postgres_changes', { event: 'DELETE', schema: 'public', table: tableName }, (payload) => | ||
handleNewValue(payload.old) | ||
) | ||
.subscribe() as any | ||
} | ||
|
||
export const removeListener = ({ listener }: { listener: any }) => { | ||
supabase.removeChannel(supabase.channel(listener)) | ||
} | ||
|
||
export { supabase } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.