supabase_typegen 0.9.1
supabase_typegen: ^0.9.1 copied to clipboard
Command-line code generator that turns a Supabase database schema into typed Dart table definitions.
supabase_typegen example #
supabase_typegen is a command-line tool. The
Supabase CLI runs it to
turn a database schema into typed Dart table definitions. This example
generates the types of a single table and queries it with them.
The schema #
A migration in the supabase/migrations directory of the project creates the
table:
create table public.authors (
id bigint generated always as identity primary key,
name text not null
);
Generating the types #
Add supabase_typegen as a dev dependency, apply the migration to the local
Supabase stack, and generate the types from the directory of the project's
pubspec.yaml:
dart pub add dev:supabase_typegen
supabase start
supabase migration up
supabase gen types --lang dart > lib/supabase_schema.g.dart
The CLI reads the schema from the database into a GeneratorMetadata
document and runs dart run supabase_typegen --output - with the document on
stdin. generator_metadata.json
is the document of the table above, so the tool also runs without a database.
From the directory of this package:
dart run supabase_typegen --output - < example/generator_metadata.json
dart run supabase_typegen --help lists the options: --schema limits the
generated schemas, --output sets the path of the generated file, and
--import sets the library the generated file imports.
The generated code #
The tool writes the row type AuthorsRow, the AuthorsInsert and
AuthorsUpdate values the typed insert, upsert and update methods
accept, the table definition Authors with a token per column, and the
AuthorsPartialRow getters for rows of a selectOnly. It imports
package:postgrest here, and the library of supabase_flutter or supabase
in a project that depends on one of them.
// Generated by supabase_typegen 0.9.0 from GeneratorMetadata version 1.
// Do not edit by hand.
//
// Source schemas: public
//
// The getters of a partial row are in an extension per table, which a `show`
// import has to name too, such as:
// AuthorsPartialRow for Authors.
// The typed table access API is still experimental.
// ignore_for_file: experimental_member_use
import 'package:postgrest/postgrest.dart';
/// A row of the `authors` table, as `select()` reads it with every column.
extension type const AuthorsRow(Map<String, dynamic> _json) implements Object {
int get id => _json['id'] as int;
String get name => _json['name'] as String;
/// The row as decoded from the response.
Map<String, dynamic> toJson() => _json;
}
/// Values for inserting a row into `authors`. Columns that are nullable,
/// identity, or covered by a database default are optional; a column left out
/// gets its database default, and `null` inserts SQL NULL. Columns the database
/// always generates itself are left out entirely.
extension type const AuthorsInsert._(Map<String, dynamic> _json)
implements Object {
AuthorsInsert({required String name}) : this._({'name': name});
/// Returns a copy with the given columns replaced. A column left out keeps
/// its current value.
AuthorsInsert Function({String name}) get copyWith =>
({Object name = const _Omitted()}) =>
AuthorsInsert._({..._json, if (name is! _Omitted) 'name': name});
/// The values as they are sent, in a map that cannot be changed. Compare two
/// values through this map with deep equality, since `==` compares map
/// identity.
Map<String, dynamic> toJson() => Map.unmodifiable(_json);
}
/// Values for updating rows of `authors`. All columns are optional; a column
/// left out keeps its value, and `null` writes SQL NULL.
extension type const AuthorsUpdate._(Map<String, dynamic> _json)
implements Object {
factory AuthorsUpdate({String name}) = _AuthorsUpdate;
/// Returns a copy with the given columns replaced. A column left out keeps
/// its current value.
AuthorsUpdate Function({String name}) get copyWith =>
({Object name = const _Omitted()}) =>
AuthorsUpdate._({..._json, if (name is! _Omitted) 'name': name});
/// The values as they are sent, in a map that cannot be changed. Compare two
/// values through this map with deep equality, since `==` compares map
/// identity.
Map<String, dynamic> toJson() => Map.unmodifiable(_json);
}
extension type const _AuthorsUpdate._(Map<String, dynamic> _json)
implements AuthorsUpdate {
_AuthorsUpdate({Object name = const _Omitted()})
: _json = {if (name is! _Omitted) 'name': name};
}
/// Typed access to the `authors` table.
class Authors {
const Authors._();
/// Table definition for [PostgrestClient.table].
static const table = PostgrestTable<AuthorsRow, AuthorsInsert, AuthorsUpdate>(
'authors',
AuthorsRow.new,
schema: 'public',
primaryKey: [id],
);
static const id = PostgrestColumn<AuthorsRow, int>('id');
static const name = PostgrestColumn<AuthorsRow, String>('name');
}
/// The values of a partial row of `authors`, as `selectOnly` returns it. A
/// getter throws a [StateError] when the `select` list did not ask for its
/// entry.
extension AuthorsPartialRow on PostgrestPartialRow<AuthorsRow> {
int get id => read(Authors.id);
String get name => read(Authors.name);
}
/// The default of a parameter the caller left out.
final class _Omitted {
const _Omitted();
}
Using the generated code #
Pass the table definition to table(), and the query results are typed:
final List<AuthorsRow> authors = await supabase
.table(Authors.table)
.select()
.where(Authors.name.ilike('u%'))
.order(Authors.name);
for (final author in authors) {
print('${author.id}: ${author.name}');
}
await supabase
.table(Authors.table)
.insert(AuthorsInsert(name: 'Ursula K. Le Guin'));
The README of the package describes everything the tool generates.