addSupabaseRelated method
void
addSupabaseRelated({
- required String jsonKey,
- required String fkConstraintName,
- required SelectBuilderBase nestedBuilder,
- bool innerJoin = false,
Adds a related table to the select query.
jsonKey
: The key that will be used in the resulting JSON for the nested object or array of related items (e.g., "author", "posts").fkConstraintName
: The exact name of the foreign key constraint in the database that defines the relationship between the current table and the nested table. This is crucial for PostgREST to correctly identify and join the tables.nestedBuilder
: A SelectBuilderBase instance configured for the related table (e.g., anAuthorsBuilder
orPostsBuilder
). This builder defines which columns are selected from the related table and any further nested relationships.innerJoin
: (Optional) Iftrue
, instructs PostgREST to perform an inner join (e.g.,table!fk_constraint!inner
). This means rows from the parent table will only be returned if there's at least one matching row in the related table. Defaults tofalse
(a left join behavior).
Example:
// In a PostsBuilder:
addSupabaseRelated(
jsonKey: 'authorDetails',
fkConstraintName: 'posts_author_id_fkey',
nestedBuilder: AuthorsBuilder().selectColumns([AuthorsColumn.name, AuthorsColumn.bio]),
innerJoin: true,
);
Implementation
void addSupabaseRelated({
required String jsonKey,
required String fkConstraintName,
required SelectBuilderBase nestedBuilder,
bool innerJoin = false,
}) {
if (supabaseRelatedBuilders.containsKey(jsonKey)) {
_logger.warning(
'Relationship for JSON key "$jsonKey" already configured for table "$primaryTableName". It will be overwritten.',
);
}
supabaseRelatedBuilders[jsonKey] = RelatedBuilderLink(
builder: nestedBuilder,
fkConstraintName: fkConstraintName,
innerJoin: innerJoin,
);
}