Generating Types
Supabase APIs are generated from your database, which means that we can use database introspection to generate type-safe API definitions.
Generating types using Supabase CLI#
The Supabase CLI is a single binary Go application that provides everything you need to setup a local development environment.
You can install the CLI via npm or other supported package managers. The minimum required version of the CLI is v1.8.1.
npm i supabase@">=1.8.1" --save-dev
Login with your Personal Access Token:
npx supabase login
Generate types for your project to produce the types/supabase.ts
file:
npx supabase gen types typescript --project-id "$PROJECT_ID" --schema public > types/supabase.ts
After you have generated your types, you can use them in src/index.ts
1import { NextApiRequest, NextApiResponse } from 'next'
2import { createClient } from '@supabase/supabase-js'
3import { Database } from '../types/supabase'
4
5const supabase = createClient<Database>(
6 process.env.NEXT_PUBLIC_SUPABASE_URL,
7 process.env.SUPABASE_SECRET_KEY
8)
9
10export default async (req: NextApiRequest, res: NextApiResponse) => {
11 const allOnlineUsers = await supabase.from('users').select('*').eq('status', 'ONLINE')
12 res.status(200).json(allOnlineUsers)
13}
Update types automatically with GitHub Actions#
One way to keep your type definitions in sync with your database is to set up a GitHub action that runs on a schedule.
Add the script above to your package.json
to run it using npm run update-types
1"update-types": "npx supabase gen types typescript --project-id \"$PROJECT_ID\" > types/supabase.ts"
Create a file .github/workflows/update-types.yml
with the following snippet to define the action along with the environment variables. This script will commit new type changes to your repo every night.
1name: Update database types
2
3on:
4 schedule:
5 # sets the action to run daily. You can modify this to run the action more or less frequently
6 - cron: '0 0 * * *'
7
8jobs:
9 update:
10 runs-on: ubuntu-latest
11 env:
12 SUPABASE_ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
13 PROJECT_ID: <your-project-id>
14 steps:
15 - uses: actions/checkout@v2
16 with:
17 persist-credentials: false
18 fetch-depth: 0
19 - uses: actions/setup-node@v2.1.5
20 with:
21 node-version: 16
22 - run: npm run update-types
23 - name: check for file changes
24 id: git_status
25 run: |
26 echo "::set-output name=status::$(git status -s)"
27 - name: Commit files
28 if: ${{contains(steps.git_status.outputs.status, ' ')}}
29 run: |
30 git add types/database/index.ts
31 git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
32 git config --local user.name "github-actions[bot]"
33 git commit -m "Update database types" -a
34 - name: Push changes
35 if: ${{contains(steps.git_status.outputs.status, ' ')}}
36 uses: ad-m/github-push-action@master
37 with:
38 github_token: ${{ secrets.GITHUB_TOKEN }}
39 branch: ${{ github.ref }}
Alternatively, you can use a community-supported GitHub action: generate-supabase-db-types-github-action.