Skip to content
Merged
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
5 changes: 4 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ dart pub add dev:supabase_typegen
supabase gen types --lang dart > lib/supabase_schema.g.dart
```

`--remote` generates from the linked project and `--remote=<ref>` from any hosted project; see the
`--remote` generates from the linked project, `--remote=<ref>` from any hosted project and
`--remote=<db-url>` from any Postgres database. The CLI finds the `supabase/` directory in the
current directory or its parents; pass `--workdir` with the directory that contains it when it is
elsewhere. See the
[`supabase_typegen` README](https://github.com/supabase/supabase-flutter/blob/main/packages/supabase_typegen/README.md)
for details.

Expand Down
17 changes: 17 additions & 0 deletions packages/supabase_typegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,25 @@ supabase gen types --lang dart > lib/supabase_schema.g.dart
# The project linked with `supabase link`, or any project by ref.
supabase gen types --lang dart --remote > lib/supabase_schema.g.dart
supabase gen types --lang dart --remote=abcdefghijklmnopqrst > lib/supabase_schema.g.dart

# Any Postgres database, by its postgresql:// URL with the password in it.
supabase gen types --lang dart --remote="$DATABASE_URL" > lib/supabase_schema.g.dart
```

The CLI looks for the `supabase/` directory in the current directory and its
parents only. When it is elsewhere, such as in a sibling directory of the
app, keep running the command from the app and pass the directory that
contains `supabase/` with `--workdir`, also to the commands that change the
local stack:

```sh
supabase migration up --workdir ../backend
supabase gen types --lang dart --workdir ../backend > lib/supabase_schema.g.dart
```

The `--local` and `--db-url` flags of older CLI versions are deprecated: the
local stack is the default, and `--remote=<db-url>` replaces `--db-url`.

The CLI introspects the database into the `GeneratorMetadata` document of
[`@supabase/postgrest-typegen`](https://github.com/supabase/sdk/tree/main/packages/postgrest-typegen),
the intermediate representation its TypeScript, Go, Swift and Python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ schema on stdin, and the generated code arrives on stdout.
- Regenerate after upgrading `supabase_typegen` or the Supabase CLI, since
the generated code can change between versions.
- Commit the generated file, so the app builds without a database.
- Run the command from the directory of the app's `pubspec.yaml`. The CLI
finds the `supabase/` folder in that directory or any parent, and runs the
generator where it was started.
- Run the command from the directory of the app's `pubspec.yaml`, since the
CLI runs the generator where it was started. The CLI looks for the
`supabase/` folder in that directory and its parents only. When the folder
is elsewhere, such as in a sibling directory of the app, pass the directory
that contains it with `--workdir`.

## Setup

Expand All @@ -61,16 +63,31 @@ of the package the file is written into, since the CLI runs it with

Pick the database to read from:

| Source | Command |
| -------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| Local stack from the `supabase/` migrations (after `supabase start`) | `supabase gen types --lang dart > lib/supabase_schema.g.dart` |
| The project linked with `supabase link` | `supabase gen types --lang dart --remote > lib/supabase_schema.g.dart` |
| Any hosted project | `supabase gen types --lang dart --remote=<ref> > lib/supabase_schema.g.dart` |
| Source | Command |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| Local stack from the `supabase/` migrations (after `supabase start`) | `supabase gen types --lang dart > lib/supabase_schema.g.dart` |
| Local stack whose `supabase/` folder is not in a parent directory | `supabase gen types --lang dart --workdir <dir> > lib/supabase_schema.g.dart` |
Comment thread
spydon marked this conversation as resolved.
Resolved
| The project linked with `supabase link` | `supabase gen types --lang dart --remote > lib/supabase_schema.g.dart` |
| Any hosted project | `supabase gen types --lang dart --remote=<ref> > lib/supabase_schema.g.dart` |
| Any Postgres database, by connection string | `supabase gen types --lang dart --remote=<db-url> > lib/supabase_schema.g.dart` |

Prefer the local stack when the project keeps its schema as migrations, so the
SQL in `supabase/migrations` stays the source of truth. It needs Docker and a
running stack.

`<dir>` is the directory that contains `supabase/`, not the `supabase/` folder
itself. Pass the same `--workdir` to `supabase start`, `supabase migration up`
and `supabase db reset`, or they act on a different stack than the one the
types are generated from. `<db-url>` is a `postgresql://` URL with the password in
it, such as `postgresql://postgres:postgres@127.0.0.1:54322/postgres` for a
local stack on the default ports. Quote it, since an `&` in its query string
splits the command. For any other database, ask the user to put the URL in an
environment variable and pass `--remote="$DATABASE_URL"`, so the password is
not written into the command you run or printed in its output. The shell
still expands it into the arguments of the `supabase` process. The `--local`
and `--db-url` flags of older CLI versions are deprecated: the local stack is
the default, and `--remote=<db-url>` replaces `--db-url`.

Without `--schema`, the CLI generates `public` plus the schemas listed in
`api.schemas` of `supabase/config.toml`. Pass `--schema` once per schema to
choose them yourself:
Expand Down Expand Up @@ -145,13 +162,13 @@ The CLI reports a generator failure as
`failed to generate dart types: dart run supabase_typegen exited with code N: …`,
followed by the generator's own message.

| Symptom | Fix |
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| `dart run` cannot find `supabase_typegen` | Add the dev dependency and run `pub get` in the directory the command runs in. |
| `The project is on Dart X, but the generated code needs Dart Y or newer` | Raise the lower bound of `environment.sdk` in `pubspec.yaml`. |
| `The database has no schema "x"` | Check the schema name, or that the stack has run the migration that creates it. |
| `The project resolves supabase_flutter 2.x, but the generated code needs version 3 of supabase_flutter` | Upgrade the package to version 3 following `MIGRATION.md`, then run `pub get`. |
| `The document describes no tables, views, enums or functions` | Check the schema names, or that the stack has run the migrations. |
| `The document has no "rpcFunctions" collection, so no RPC methods are generated` | Upgrade the Supabase CLI to the latest release and regenerate. |
| `--lang dart` is rejected | Upgrade the Supabase CLI to 3.0.0 or newer. |
| The CLI cannot reach the local database | Run `supabase start` first. |
| Symptom | Fix |
| ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `dart run` cannot find `supabase_typegen` | Add the dev dependency and run `pub get` in the directory the command runs in. |
| `The project is on Dart X, but the generated code needs Dart Y or newer` | Raise the lower bound of `environment.sdk` in `pubspec.yaml`. |
| `The database has no schema "x"` | Check the schema name, or that the stack has run the migration that creates it. |
| `The project resolves supabase_flutter 2.x, but the generated code needs version 3 of supabase_flutter` | Upgrade the package to version 3 following `MIGRATION.md`, then run `pub get`. |
| `The document describes no tables, views, enums or functions` | Check the schema names, or that the stack has run the migrations. |
| `The document has no "rpcFunctions" collection, so no RPC methods are generated` | Upgrade the Supabase CLI to the latest release and regenerate. |
| `--lang dart` is rejected | Upgrade the Supabase CLI to 3.0.0 or newer. |
| The CLI cannot reach the local database | Run `supabase start` first, with the same `--workdir` as the generation command. |
53 changes: 35 additions & 18 deletions skills_src/generate/SKILL.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ schema on stdin, and the generated code arrives on stdout.
- Regenerate after upgrading `supabase_typegen` or the Supabase CLI, since
the generated code can change between versions.
- Commit the generated file, so the app builds without a database.
- Run the command from the directory of the app's `pubspec.yaml`. The CLI
finds the `supabase/` folder in that directory or any parent, and runs the
generator where it was started.
- Run the command from the directory of the app's `pubspec.yaml`, since the
CLI runs the generator where it was started. The CLI looks for the
`supabase/` folder in that directory and its parents only. When the folder
is elsewhere, such as in a sibling directory of the app, pass the directory
that contains it with `--workdir`.

## Setup

Expand All @@ -61,16 +63,31 @@ of the package the file is written into, since the CLI runs it with

Pick the database to read from:

| Source | Command |
| -------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| Local stack from the `supabase/` migrations (after `supabase start`) | `supabase gen types --lang dart > lib/supabase_schema.g.dart` |
| The project linked with `supabase link` | `supabase gen types --lang dart --remote > lib/supabase_schema.g.dart` |
| Any hosted project | `supabase gen types --lang dart --remote=<ref> > lib/supabase_schema.g.dart` |
| Source | Command |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| Local stack from the `supabase/` migrations (after `supabase start`) | `supabase gen types --lang dart > lib/supabase_schema.g.dart` |
| Local stack whose `supabase/` folder is not in a parent directory | `supabase gen types --lang dart --workdir <dir> > lib/supabase_schema.g.dart` |
| The project linked with `supabase link` | `supabase gen types --lang dart --remote > lib/supabase_schema.g.dart` |
| Any hosted project | `supabase gen types --lang dart --remote=<ref> > lib/supabase_schema.g.dart` |
| Any Postgres database, by connection string | `supabase gen types --lang dart --remote=<db-url> > lib/supabase_schema.g.dart` |

Prefer the local stack when the project keeps its schema as migrations, so the
SQL in `supabase/migrations` stays the source of truth. It needs Docker and a
running stack.

`<dir>` is the directory that contains `supabase/`, not the `supabase/` folder
itself. Pass the same `--workdir` to `supabase start`, `supabase migration up`
and `supabase db reset`, or they act on a different stack than the one the
types are generated from. `<db-url>` is a `postgresql://` URL with the password in
it, such as `postgresql://postgres:postgres@127.0.0.1:54322/postgres` for a
local stack on the default ports. Quote it, since an `&` in its query string
splits the command. For any other database, ask the user to put the URL in an
environment variable and pass `--remote="$DATABASE_URL"`, so the password is
not written into the command you run or printed in its output. The shell
still expands it into the arguments of the `supabase` process. The `--local`
and `--db-url` flags of older CLI versions are deprecated: the local stack is
the default, and `--remote=<db-url>` replaces `--db-url`.

Without `--schema`, the CLI generates `public` plus the schemas listed in
`api.schemas` of `supabase/config.toml`. Pass `--schema` once per schema to
choose them yourself:
Expand Down Expand Up @@ -145,13 +162,13 @@ The CLI reports a generator failure as
`failed to generate dart types: dart run supabase_typegen exited with code N: …`,
followed by the generator's own message.

| Symptom | Fix |
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| `dart run` cannot find `supabase_typegen` | Add the dev dependency and run `pub get` in the directory the command runs in. |
| `The project is on Dart X, but the generated code needs Dart Y or newer` | Raise the lower bound of `environment.sdk` in `pubspec.yaml`. |
| `The database has no schema "x"` | Check the schema name, or that the stack has run the migration that creates it. |
| `The project resolves supabase_flutter 2.x, but the generated code needs version 3 of supabase_flutter` | Upgrade the package to version 3 following `MIGRATION.md`, then run `pub get`. |
| `The document describes no tables, views, enums or functions` | Check the schema names, or that the stack has run the migrations. |
| `The document has no "rpcFunctions" collection, so no RPC methods are generated` | Upgrade the Supabase CLI to the latest release and regenerate. |
| `--lang dart` is rejected | Upgrade the Supabase CLI to 3.0.0 or newer. |
| The CLI cannot reach the local database | Run `supabase start` first. |
| Symptom | Fix |
| ------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `dart run` cannot find `supabase_typegen` | Add the dev dependency and run `pub get` in the directory the command runs in. |
| `The project is on Dart X, but the generated code needs Dart Y or newer` | Raise the lower bound of `environment.sdk` in `pubspec.yaml`. |
| `The database has no schema "x"` | Check the schema name, or that the stack has run the migration that creates it. |
| `The project resolves supabase_flutter 2.x, but the generated code needs version 3 of supabase_flutter` | Upgrade the package to version 3 following `MIGRATION.md`, then run `pub get`. |
| `The document describes no tables, views, enums or functions` | Check the schema names, or that the stack has run the migrations. |
| `The document has no "rpcFunctions" collection, so no RPC methods are generated` | Upgrade the Supabase CLI to the latest release and regenerate. |
| `--lang dart` is rejected | Upgrade the Supabase CLI to 3.0.0 or newer. |
| The CLI cannot reach the local database | Run `supabase start` first, with the same `--workdir` as the generation command. |
Loading