Skip to content

Commit

Permalink
Enable supabase-cli types gen (#10)
Browse files Browse the repository at this point in the history
* Add supabase-cli types generation

* Bump cli 1.5.3 - setup-cli 1.0.2

* Update Readme example for use with supabase-cli

* chore: update docs for autogen on db update

* update docs for migration from v0.1.0 to v0.2.0

* update indent in yml examples

* update docs to be explicit in affected types

* add doc for how to get sb access token

* add description for migration script use case

* update checkout action to v3

* update action to run supabase init and start to create config.toml

* init and start only for the first run

Co-authored-by: lyqht <[email protected]>
  • Loading branch information
alex-galey and lyqht committed Oct 12, 2022
1 parent 60721a1 commit 86b729b
Show file tree
Hide file tree
Showing 5 changed files with 508 additions and 27 deletions.
81 changes: 67 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@ This GitHub action workflow aims to help you to create Supabase database definit

This workflow is a composite action:

- To generate database types based on the Swagger OpenAPI specification of your Supabase project REST data endpoint, [openapi-typescript](https://github.com/drwpow/openapi-typescript) is used.
- To generate database types, we use [supabase/setup-cli](https://github.com/supabase/setup-cli) action is used to run supabase CLI built-in command.
- Commit and push github actions to your repo are performed by the [git-auto-commit action](https://github.com/stefanzweifel/git-auto-commit-action).
- Creating the pull request is performed by [pull-request action](https://github.com/repo-sync/pull-request).

## How to use

If you are new to GitHub Actions, refer to [this section](#if-you-dont-have-an-existing-github-action-workflow-for-your-repository). Otherwise, you can get started by referring to to the example given and the input options available.
<details><summary>If you don't have an existing GitHub Action workflow for your repository</summary>

### Example
1. Create a folder `.github/workflows` if you don't have it already
2. Inside that folder, create a YAML file say `update-types.yml`
3. In the `update-types.yml` file, you can copy the example above and modify it to your usage.
4. You can choose to declare the `schedule` with a cron expression to run the job at a specified frequency e.g. every day once.
</details>

<details><summary>How to get Supabase Access Token</summary>

Go to https://app.supabase.com/account/tokens and get a token there.

![](access_token_preview.png)

</details>


Otherwise, you can get started by referring to to the example given and the input options available.

### Simple Example

```yml
name: Update database types
Expand All @@ -27,22 +44,58 @@ on:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: lyqht/generate-supabase-db-types-github-action@main
with:
SUPABASE_REF_ID: ${{ secrets.SUPABASE_REF_ID }}
SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
OUTPUT_PATH: db.types.ts
```
### Example based on migration scripts
If your DB schema is kept up to date based on the migration SQL scripts within your project itself, you can configure the workflow to run based on any new SQL files pushed to your branch.
```yml
name: Update database types
on:
push:
branches: [ main ]
paths:
- '*.sql'

jobs:
build:
runs-on: ubuntu-latest
if: github.head_ref != 'supabot**'
steps:
- uses: lyqht/generate-supabase-db-types-github-action@main
with:
SUPABASE_URL: ${{secrets.SUPABASE_URL }} # e.g. https://interestingproject.supabase.co
SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }}
OUTPUT_PATH: src/types/supabase.ts
- uses: lyqht/generate-supabase-db-types-github-action@main
with:
SUPABASE_REF_ID: ${{ secrets.SUPABASE_REF_ID }}
SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
OUTPUT_PATH: db.types.ts
```
### If you don't have an existing GitHub Action workflow for your repository
## Migrating from `v0.1.0` to `v0.2.0`

Please note that this will be a breaking change. The types generated from v0.1.0 could be different from that of v0.2.0, so you may need to modify your code quite a bit if you choose to migrate for fields of data types such as `JSON`, `Date`. However, `v0.2.0` types follows what supabase recommends.

### Why and how the types are different?

v0.1.0 relies on the `openapi-typescript` library to generate types based on the OpenAPI specs that the Supabase endpoint offers.
v0.2.0 relies on `supabase-cli` to generate the types using supabase, and these types are much more compatible to the `supabasejs-v2` library.

- Types generated by v0.1.0: [Example](./exampleV010.ts)
- Types generated by v0.2.0: [Example](./exampleV020.ts)

### Changes to make

You need to make the following changes in variables:
- No longer used: `SUPABASE_URL: ${{secrets.SUPABASE_URL }}` and `SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }}`
- You have to add `SUPABASE_REF_ID: ${{ secrets.SUPABASE_REF_ID }}`, and `SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}`

1. Create a folder `.github/workflows` if you don't have it already
2. Inside that folder, create a YAML file say `update-types.yml`
3. In the `update-types.yml` file, you can copy the example above and modify it to your usage.
4. You can choose to declare the `schedule` with a cron expression to run the job at a specified frequency e.g. every day once.

---
## Cavaets

> Note that if your Supabase project is paused or deleted, this bot will only result in failed jobs.
Binary file added access_token_preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 22 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ author: 'Estee Tey'
description: 'Automatically updates your type definitions to match your table schemas'

inputs:
SUPABASE_ANON_KEY:
description: "Anonymous API key of your Supabase project"
SUPABASE_REF_ID:
description: "Reference id of your Supabase project"
required: true
SUPABASE_URL:
description: "Endpoint of your Supabase project"
SUPABASE_ACCESS_TOKEN:
description: "Access token https://app.supabase.com/account/tokens"
required: true
DB_PASSWORD:
description: "Database password"
required: true
OUTPUT_PATH:
description: "Path where you want the definitions file to be saved. Any changes will be committed and override existing definition files. Default value is 'src/types/supabase.ts'"
Expand All @@ -18,21 +21,27 @@ runs:
using: composite
steps:
- name: Checkout project
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
persist-credentials: true
fetch-depth: 0
- name: Set up node
uses: actions/[email protected]
- name: "Retrieve OpenAPI specifications of Supabase project"
- name: Set up supabase cli
uses: supabase/[email protected]
with:
version: 1.5.3
- name: "Generate database typescript types"
shell: bash
env:
SUPABASE_URL: ${{ inputs.SUPABASE_URL }}
SUPABASE_ANON_KEY: ${{ inputs.SUPABASE_ANON_KEY }}
SUPABASE_REF_ID: ${{ inputs.SUPABASE_REF_ID }}
SUPABASE_ACCESS_TOKEN: ${{ inputs.SUPABASE_ACCESS_TOKEN }}
DB_PASSWORD: ${{ inputs.DB_PASSWORD }}
OUTPUT_PATH: ${{ inputs.OUTPUT_PATH }}
run: |
npx openapi-typescript ${SUPABASE_URL}/rest/v1/?apikey=${SUPABASE_ANON_KEY} --version=2 --output ${OUTPUT_PATH}
echo "$(cat src/types/supabase.ts)"
if [ ! -f supabase/config.toml ]; then
supabase init && supabase start
fi
supabase link --project-ref ${SUPABASE_REF_ID} --password ${DB_PASSWORD}
supabase gen types typescript --db-url "postgresql://postgres:${DB_PASSWORD}@db.${SUPABASE_REF_ID}.supabase.co:6543/postgres" > ${OUTPUT_PATH}
- name: Check for file changes & commit files
uses: stefanzweifel/git-auto-commit-action@v4
with:
Expand All @@ -56,4 +65,4 @@ runs:
branding:
icon: 'activity'
color: 'blue'
color: 'blue'
Loading

0 comments on commit 86b729b

Please sign in to comment.