FroggyPanda / better-supabase-types

72 stars 13 forks source link

npm version npm License: MIT

Better Supabase Types

A CLI tool to add exports for your supabase tables. It will add type exports at the bottom of a new typescript file of every table you have. This tool can help remove the need to make a type for their rows manually.

Before πŸ“‰

import { Database } from './src/schema.ts';

type Todo = Database['public']['Tables']['Todo']['Row'];

const todos: Todo[] = [];

After πŸ“ˆ

import { Todo } from './src/newSchema.ts';

const todos: Todo[] = [];

Usage πŸ”¨

  1. First have your supabase typescript file generated Supabase Docs
npx supabase gen types typescript --linked --schema public > ./src/schema.ts
  1. Run the better-supabase-types command (you can also use a config file):
npx better-supabase-types -i ./src/schema.ts -o ./src/newSchema.ts

Commands πŸ’»

Options:
      --version         Show version number                            [boolean]
      --help            Show help                                      [boolean]
  -i, --input           Path to the input file               [string] [required]
  -o, --output          Path to the output file                         [string]
  -p, --prettier        Path to the prettier config file
                                               [string] [default: ".prettierrc"]
  -f, --force           Force the overwrite of the input file          [boolean]
  -s, --singular        Convert table names to singular form instead of plural form
                                                      [boolean] [default: false]
      --enumAsType      Have converted enums defined as types and not enums
                                                      [boolean] [default: false]
      --enumPascalCase  Enums format to pascal case   [boolean] [default: false]

Config file βš™

You can also use a config named .betterrc.json:

{
  "input": "./src/schema.ts",
  "force": true,
  "singular": true
}

package.json πŸ“¦

You can also put your config file in your package.json file:

{
  "name": "better-supabase-types",
  "...": "...",
  "betterConfig": {
    "input": "./schema.ts",
    "output": "./newSchema.ts"
  }
}

Contributions βž•

Please contribute to this if you find any bugs or want any additions. This is my first public package so please bear with me if there are any issues.

Thanks πŸ™

Big thanks to Barry for making the Supabase React Query Codegen tool to help me understand on how to read the supabase type file.