nuxt-modules / supabase

Supabase module for Nuxt.
https://supabase.nuxtjs.org
MIT License
733 stars 129 forks source link

feat: automatically pass database typings to clients #366

Closed IsraelOrtuno closed 4 months ago

IsraelOrtuno commented 6 months ago

Types of changes

Description

This PR aims to automatically pass Supabase database generated types to the clients to get autocompletion and type-safe results when querying the database. By default it will try to locate the database typings ~/types/database.types.ts.

This can be configured:

defineNuxtConfig({
  supabase: {
    types: './path/to/types/file' // or false to disable this behaviour
  }
})

Types could be overwritten too:

import type { Database } from '~/other'
const client = useSupabaseClient<Database>()

What it does under the hood is to basically map type Database to what is found in that file (if exists) as this type is always passed to the clients. If not, it will generate a default type Database = unknown so the clients still get a type. It's my first working with auto-generated TypeScript so not sure if it's the way to go but works pretty nice. Open to any suggestions of how this should be made.

Resolves what motivated @ozum to submit this PR #160 and also this issue #150

Checklist:

netlify[bot] commented 6 months ago

Deploy Preview for n3-supabase canceled.

Name Link
Latest commit b1044e411f1f3a9e1e07c37720ce3c68f3b0b175
Latest deploy log https://app.netlify.com/sites/n3-supabase/deploys/6687cef1b7f2da0008ff05f2
ifzm commented 4 months ago

Looking forward to merging :)