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
100 changes: 72 additions & 28 deletions packages/postgrest/lib/src/postgrest_embedded_relation.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
part of 'postgrest_typed_builder.dart';

/// A foreign key seen from the table whose rows are [Row], pointing at the
/// table whose rows are [Target].
///
/// [columns] are on this table and [referencedColumns] on [referencedTable],
/// paired by index: the embedded rows are the rows of [referencedTable]
/// whose [referencedColumns] equal this row's [columns]. A to-one relation
/// holds the key itself, a to-many relation is pointed at by it.
///
/// `package:supabase_typegen` generates one relation constant per foreign
/// key on each side and lists them in [PostgrestTable.relations].
@experimental
sealed class PostgrestRelation<Row, Target> {
const PostgrestRelation(
this.name, {
required this.columns,
required this.referencedTable,
required this.referencedColumns,
});

/// The name PostgREST addresses the embed by, including any disambiguating
/// foreign key hint such as `authors!books_author_id_fkey`.
final String name;

/// The columns of this table the relation joins on.
final List<PostgrestColumn<Row, Object>> columns;

/// The name of the table the relation points at.
final String referencedTable;

/// The columns of [referencedTable] the relation joins on, paired with
/// [columns] by index.
final List<PostgrestColumn<Target, Object>> referencedColumns;
}

/// A to-one embedded relation (many-to-one or one-to-one) of the table whose
/// rows are [Row], pointing at the table whose rows are [Target].
///
Expand All @@ -8,7 +42,13 @@ part of 'postgrest_typed_builder.dart';
///
/// ```dart
/// class Orders {
/// static const todo = PostgrestToOneRelation<Order, Todo>('todo');
/// static const todoId = PostgrestColumn<Order, int>('todo_id');
/// static const todo = PostgrestToOneRelation<Order, Todo>(
/// 'todo',
/// columns: [todoId],
/// referencedTable: 'todos',
/// referencedColumns: [Todos.id],
/// );
/// }
///
/// Orders.todo(Todos.title).expression // todo(title)
Expand All @@ -17,18 +57,19 @@ part of 'postgrest_typed_builder.dart';
/// Projections of a to-one embed can be ordered by, unlike those of a
/// [PostgrestToManyRelation].
@experimental
final class PostgrestToOneRelation<Row, Target> {
/// Creates a relation PostgREST addresses as [name], including any
/// disambiguating foreign key hint such as `authors!books_author_id_fkey`.
const PostgrestToOneRelation(this.name);

/// The name PostgREST addresses the embed by.
final String name;
final class PostgrestToOneRelation<Row, Target>
extends PostgrestRelation<Row, Target> {
const PostgrestToOneRelation(
super.name, {
required super.columns,
required super.referencedTable,
required super.referencedColumns,
});

/// Projects [column] of the embedded table into the parent's frame.
PostgrestToOneColumn<Row, Value> call<Value extends Object>(
PostgrestColumnExpression<Target, Value> column,
) => PostgrestToOneColumn._(name, column.expression);
) => PostgrestToOneColumn._(this, column.expression);
}

/// A to-many embedded relation (one-to-many or many-to-many) of the table
Expand All @@ -37,42 +78,45 @@ final class PostgrestToOneRelation<Row, Target> {
/// Its projections are select position only: PostgREST answers
/// `order=children(amount).desc` with PGRST118.
@experimental
final class PostgrestToManyRelation<Row, Target> {
/// Creates a relation PostgREST addresses as [name], including any
/// disambiguating foreign key hint.
const PostgrestToManyRelation(this.name);

/// The name PostgREST addresses the embed by.
final String name;
final class PostgrestToManyRelation<Row, Target>
extends PostgrestRelation<Row, Target> {
const PostgrestToManyRelation(
super.name, {
required super.columns,
required super.referencedTable,
required super.referencedColumns,
});

/// Projects [column] of the embedded table into the parent's frame.
PostgrestToManyColumn<Row, Value> call<Value extends Object>(
PostgrestColumnExpression<Target, Value> column,
) => PostgrestToManyColumn._(name, column.expression);
) => PostgrestToManyColumn._(this, column.expression);
}

/// A column of an embedded relation, seen from the parent.
sealed class _EmbeddedColumn<Row, Value extends Object>
extends PostgrestColumnExpression<Row, Value> {
const _EmbeddedColumn(this._embed, this._inner) : super._();
const _EmbeddedColumn(this.relation, this._inner) : super._();

/// The relation the column is projected through.
final PostgrestRelation<Row, Object?> relation;

final String _embed;
final String _inner;

/// The `select` list form, `parent(title)`.
@override
String get expression => '$_embed($_inner)';
String get expression => '${relation.name}($_inner)';

/// The filter form, `parent.title`.
String get embeddedFilterName => '$_embed.$_inner';
String get embeddedFilterName => '${relation.name}.$_inner';

/// Places the derivation inside the embed's parentheses, where PostgREST
/// applies it.
@override
PostgrestDerivedExpression<Row, Derived> _derive<Derived extends Object>(
String derivation,
) => PostgrestDerivedExpression._(
embed: _embed,
embed: relation.name,
inner: '$_inner$derivation',
);
}
Expand All @@ -84,15 +128,15 @@ sealed class _EmbeddedColumn<Row, Value extends Object>
final class PostgrestToOneColumn<Row, Value extends Object>
extends _EmbeddedColumn<Row, Value>
with PostgrestOrderableExpression<Row, Value> {
const PostgrestToOneColumn._(super.embed, super.inner);
const PostgrestToOneColumn._(super.relation, super.inner);

@override
PostgrestToOneColumn<Row, String> jsonText(String path) =>
PostgrestToOneColumn._(_embed, '$_inner->>$path');
PostgrestToOneColumn._(relation, '$_inner->>$path');

@override
PostgrestToOneColumn<Row, Value> jsonObject(String path) =>
PostgrestToOneColumn._(_embed, '$_inner->$path');
PostgrestToOneColumn._(relation, '$_inner->$path');
}

/// A column of a to-many embedded relation, seen from the parent.
Expand All @@ -102,13 +146,13 @@ final class PostgrestToOneColumn<Row, Value extends Object>
@experimental
final class PostgrestToManyColumn<Row, Value extends Object>
extends _EmbeddedColumn<Row, Value> {
const PostgrestToManyColumn._(super.embed, super.inner);
const PostgrestToManyColumn._(super.relation, super.inner);

@override
PostgrestToManyColumn<Row, String> jsonText(String path) =>
PostgrestToManyColumn._(_embed, '$_inner->>$path');
PostgrestToManyColumn._(relation, '$_inner->>$path');

@override
PostgrestToManyColumn<Row, Value> jsonObject(String path) =>
PostgrestToManyColumn._(_embed, '$_inner->$path');
PostgrestToManyColumn._(relation, '$_inner->$path');
}
21 changes: 19 additions & 2 deletions packages/postgrest/lib/src/postgrest_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef RowConverter<Row> = Row Function(Map<String, dynamic> json);
/// static const table = PostgrestTable<Book, BookInsert, BookUpdate>(
/// 'books',
/// Book.new,
/// primaryKey: [id],
/// );
/// static const id = PostgrestColumn<Book, int>('id');
/// static const title = PostgrestColumn<Book, String>('title');
Expand All @@ -54,15 +55,31 @@ typedef RowConverter<Row> = Row Function(Map<String, dynamic> json);
/// row representation since they carry no conversion cost and tolerate
/// partial selects, but any converter works, for example `Book.fromJson` on a
/// regular data class. `package:supabase_typegen` generates all three types
/// and the table definition from the database schema.
/// and the table definition, including [primaryKey] and [relations], from the
/// database schema.
@experimental
// ignore: avoid-unused-generics
class PostgrestTable<Row, Insert, Update> {
const PostgrestTable(this.name, this.rowFromJson);
const PostgrestTable(
this.name,
this.rowFromJson, {
required this.primaryKey,
this.relations = const [],
});

/// Name of the table in the database.
final String name;

/// Converts a decoded row into [Row].
final RowConverter<Row> rowFromJson;

/// The columns that identify a row, in key order.
///
/// Empty for a view or a table without a primary key, where nothing on
/// the client can tell two rows apart.
final List<PostgrestColumn<Row, Object>> primaryKey;

/// The foreign keys this table holds and the ones pointing at it, each as
/// the relation an embedded select addresses it by.
final List<PostgrestRelation<Row, Object?>> relations;
}
28 changes: 26 additions & 2 deletions packages/postgrest/test/postgrest_embedded_relation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,41 @@ class Orders {
static const shippedAt = PostgrestNullableColumn<Order, DateTime>(
'shipped_at',
);
static const todo = PostgrestToOneRelation<Order, Todo>('todo');
static const todoId = PostgrestColumn<Order, int>('todo_id');
static const todo = PostgrestToOneRelation<Order, Todo>(
'todo',
columns: [todoId],
referencedTable: 'todos',
referencedColumns: [Todos.id],
);
}

class Todos {
static const id = PostgrestColumn<Todo, int>('id');
static const title = PostgrestColumn<Todo, String>('title');
static const amount = PostgrestColumn<Todo, double>('amount');
static const orders = PostgrestToManyRelation<Todo, Order>('orders');
static const orders = PostgrestToManyRelation<Todo, Order>(
'orders',
columns: [id],
referencedTable: 'orders',
referencedColumns: [Orders.todoId],
);
}

void main() {
test('a relation knows the columns of both sides of its key', () {
expect(Orders.todo.columns, [Orders.todoId]);
expect(Orders.todo.referencedTable, 'todos');
expect(Orders.todo.referencedColumns, [Todos.id]);
expect(Todos.orders.columns, [Todos.id]);
expect(Todos.orders.referencedColumns, [Orders.todoId]);
});

test('a projected column keeps its relation', () {
expect(Todos.orders(Orders.amount).relation, same(Todos.orders));
expect(Orders.todo(Todos.title).jsonText('k').relation, same(Orders.todo));
});

test('a projected column wraps in the embed name', () {
expect(Todos.orders(Orders.amount).expression, 'orders(amount)');
expect(Orders.todo(Todos.title).expression, 'todo(title)');
Expand Down
19 changes: 17 additions & 2 deletions packages/postgrest/test/typed_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ class Books {
static const table = PostgrestTable<Book, BookInsert, BookUpdate>(
'books',
Book.new,
primaryKey: [id],
relations: [author],
);
static const id = PostgrestColumn<Book, int>('id');
static const authorId = PostgrestColumn<Book, int>('author_id');
static const title = PostgrestColumn<Book, String>('title');
static const tags = PostgrestColumn<Book, List<String>>('tags');
static const ageRange = PostgrestColumn<Book, PostgrestRange<int>>(
Expand All @@ -44,17 +47,29 @@ class Books {
static const publishedOn = PostgrestNullableColumn<Book, DateTime>(
'published_on',
);
static const author = PostgrestToOneRelation<Book, Author>('author');
static const author = PostgrestToOneRelation<Book, Author>(
'author',
columns: [authorId],
referencedTable: 'authors',
referencedColumns: [Authors.id],
);
}

class Authors {
static const table = PostgrestTable<Author, Never, Never>(
'authors',
Author.new,
primaryKey: [id],
relations: [books],
);
static const id = PostgrestColumn<Author, int>('id');
static const name = PostgrestColumn<Author, String>('name');
static const books = PostgrestToManyRelation<Author, Book>('books');
static const books = PostgrestToManyRelation<Author, Book>(
'books',
columns: [id],
referencedTable: 'books',
referencedColumns: [Books.authorId],
);
}

class BookClass {
Expand Down
6 changes: 5 additions & 1 deletion packages/supabase/test/mock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ extension type const Todo(Map<String, dynamic> _json)
}

class Todos {
static const table = PostgrestTable<Todo, Never, Never>('todos', Todo.new);
static const table = PostgrestTable<Todo, Never, Never>(
'todos',
Todo.new,
primaryKey: [id],
);
static const id = PostgrestColumn<Todo, int>('id');
static const task = PostgrestColumn<Todo, String>('task');
static const status = PostgrestColumn<Todo, bool>('status');
Expand Down
1 change: 1 addition & 0 deletions packages/supabase/test/stream_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class _Users {
static const table = PostgrestTable<_User, Never, Never>(
'users',
_User.new,
primaryKey: [username],
);
static const username = PostgrestColumn<_User, String>('username');
static const status = PostgrestColumn<_User, String>('status');
Expand Down
1 change: 1 addition & 0 deletions packages/supabase_test/test/typed_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Todos {
static const table = PostgrestTable<Todo, TodoInsert, TodoUpdate>(
'todos',
Todo.new,
primaryKey: [id],
);
static const id = PostgrestColumn<Todo, int>('id');
static const task = PostgrestColumn<Todo, String>('task');
Expand Down
Loading
Loading