diff --git a/MIGRATION.md b/MIGRATION.md index 684e844f3..61cc25153 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -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=` from any hosted project; see the +`--remote` generates from the linked project, `--remote=` from any hosted project and +`--remote=` 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. diff --git a/packages/supabase_typegen/README.md b/packages/supabase_typegen/README.md index 288c9d8e7..43fd090d1 100644 --- a/packages/supabase_typegen/README.md +++ b/packages/supabase_typegen/README.md @@ -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=` 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 diff --git a/packages/supabase_typegen/skills/supabase-typegen-generate/SKILL.md b/packages/supabase_typegen/skills/supabase-typegen-generate/SKILL.md index a2571f307..3b47c8bbf 100644 --- a/packages/supabase_typegen/skills/supabase-typegen-generate/SKILL.md +++ b/packages/supabase_typegen/skills/supabase-typegen-generate/SKILL.md @@ -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 @@ -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= > 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 > 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= > lib/supabase_schema.g.dart` | +| Any Postgres database, by connection string | `supabase gen types --lang dart --remote= > 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. +`` 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. `` 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=` 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: @@ -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. | diff --git a/skills_src/generate/SKILL.md.tmpl b/skills_src/generate/SKILL.md.tmpl index 24d74a110..1df2d42b5 100644 --- a/skills_src/generate/SKILL.md.tmpl +++ b/skills_src/generate/SKILL.md.tmpl @@ -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 @@ -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= > 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 > 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= > lib/supabase_schema.g.dart` | +| Any Postgres database, by connection string | `supabase gen types --lang dart --remote= > 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. +`` 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. `` 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=` 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: @@ -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. |