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

Add support for database types on different schemas. #392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ export default defineNuxtModule<ModuleOptions>({
filename: 'types/supabase-database.d.ts',
getContents: async () => {
if (!!options.types && fs.existsSync(await resolvePath(options.types))) {
return `export * from '${await resolvePath(options.types)}'`
return `export * from '${await resolvePath(options.types)}'; export type Schema = ${JSON.stringify(options?.clientOptions?.db?.schema || 'public')};`
}

return `export type Database = unknown`
return `export type Database = unknown; export type Schema = ${JSON.stringify(options?.clientOptions?.db?.schema || 'public')};`
},
})

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/composables/useSupabaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { useNuxtApp } from '#imports'
import type { Database } from '#build/types/supabase-database'
import type { Database, Schema } from '#build/types/supabase-database'

export const useSupabaseClient = <T = Database>() => {
return useNuxtApp().$supabase?.client as SupabaseClient<T>
export const useSupabaseClient = <T = Database, S extends string & keyof T = Schema>() => {
return useNuxtApp().$supabase?.client as SupabaseClient<T, S>
}
4 changes: 2 additions & 2 deletions src/runtime/server/services/serverSupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { SupabaseClient } from '@supabase/supabase-js'
import { createServerClient, parseCookieHeader, type CookieOptions } from '@supabase/ssr'
import { getHeader, setCookie, type H3Event } from 'h3'
import { useRuntimeConfig } from '#imports'
import type { Database } from '#build/types/supabase-database'
import type { Database, Schema } from '#build/types/supabase-database'

export const serverSupabaseClient = async <T = Database>(event: H3Event): Promise<SupabaseClient<T>> => {
export const serverSupabaseClient = async <T = Database, S extends string & keyof T = Schema>(event: H3Event): Promise<SupabaseClient<T, S>> => {
// No need to recreate client if exists in request context
if (!event.context._supabaseClient) {
// get settings from runtime config
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/services/serverSupabaseServiceRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { SupabaseClient } from '@supabase/supabase-js'
import { createClient } from '@supabase/supabase-js'
import type { H3Event } from 'h3'
import { useRuntimeConfig } from '#imports'
import type { Database } from '#build/types/supabase-database'
import type { Database, Schema } from '#build/types/supabase-database'

export const serverSupabaseServiceRole = <T = Database>(event: H3Event): SupabaseClient<T> => {
export const serverSupabaseServiceRole = <T = Database, S extends string & keyof T = Schema>(event: H3Event): SupabaseClient<T, S> => {
const {
supabase: { serviceKey },
public: {
Expand Down