Skip to content

Commit

Permalink
feat: create supabase lib and actions files
Browse files Browse the repository at this point in the history
  • Loading branch information
andostronaut committed Sep 10, 2024
1 parent 7c28d72 commit 543e81b
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/(dashboard)/actions.ts
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.
1 change: 1 addition & 0 deletions app/(root)/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use server'
52 changes: 52 additions & 0 deletions lib/supabase/client.ts
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 }
64 changes: 64 additions & 0 deletions lib/supabase/server.ts
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 }
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@supabase/ssr": "^0.5.1",
"@supabase/supabase-js": "^2.45.3",
"chart.js": "^4.4.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand Down
106 changes: 106 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 543e81b

Please sign in to comment.