Skip to content

feat(postgrest, supabase): add useSchema() method for making rest API calls on custom schema. #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 8, 2023
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
13 changes: 13 additions & 0 deletions packages/postgrest/lib/src/postgrest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ class PostgrestClient {
);
}

/// Select a schema to query or perform an function (rpc) call.
///
/// The schema needs to be on the list of exposed schemas inside Supabase.
PostgrestClient useSchema(String schema) {
return PostgrestClient(
url,
headers: {...headers},
schema: schema,
httpClient: httpClient,
isolate: _isolate,
);
}

/// Perform a stored procedure call.
///
/// ```dart
Expand Down
13 changes: 13 additions & 0 deletions packages/postgrest/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ void main() {
expect(res.length, 5);
});

test('query non-public schema dynamically', () async {
final postgrest = PostgrestClient(rootUrl);
final personalData = await postgrest
.useSchema('personal')
.from('users')
.select<PostgrestList>();
expect(personalData.length, 5);

// confirm that the client defaults to its initialized schema by default.
final publicData = await postgrest.from('users').select<PostgrestList>();
expect(publicData.length, 4);
});

test('on_conflict upsert', () async {
final res = await postgrest.from('users').upsert(
{'username': 'dragarcia', 'status': 'OFFLINE'},
Expand Down
7 changes: 7 additions & 0 deletions packages/supabase/lib/src/supabase_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ class SupabaseClient {
);
}

/// Select a schema to query or perform an function (rpc) call.
///
/// The schema needs to be on the list of exposed schemas inside Supabase.
PostgrestClient useSchema(String schema) {
return rest.useSchema(schema);
}

/// Perform a stored procedure call.
PostgrestFilterBuilder rpc(
String fn, {
Expand Down