Skip to content

Commit

Permalink
fix: stream with a different schema (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Oct 7, 2023
1 parent a5ef8b7 commit c8fc248
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/supabase/lib/src/counter.dart
@@ -0,0 +1,9 @@
class Counter {
int _value = 0;

int get value => _value;

int increment() {
return _value++;
}
}
20 changes: 15 additions & 5 deletions packages/supabase/lib/src/supabase_client.dart
Expand Up @@ -6,6 +6,7 @@ import 'package:supabase/supabase.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';

import 'auth_http_client.dart';
import 'counter.dart';

/// {@template supabase_client}
/// Creates a Supabase client to interact with your Supabase instance.
Expand Down Expand Up @@ -56,7 +57,7 @@ class SupabaseClient {
late final YAJsonIsolate _isolate;

/// Increment ID of the stream to create different realtime topic for each stream
int _incrementId = 0;
final _incrementId = Counter();

/// Getter for the HTTP headers
Map<String, String> get headers {
Expand Down Expand Up @@ -139,24 +140,33 @@ class SupabaseClient {
/// Perform a table operation.
SupabaseQueryBuilder from(String table) {
final url = '$_restUrl/$table';
_incrementId++;
return SupabaseQueryBuilder(
url,
realtime,
headers: {...rest.headers, ...headers},
schema: _postgrestOptions.schema,
table: table,
httpClient: _authHttpClient,
incrementId: _incrementId,
incrementId: _incrementId.increment(),
isolate: _isolate,
);
}

/// 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);
SupabaseQuerySchema useSchema(String schema) {
final newRest = rest.useSchema(schema);
return SupabaseQuerySchema(
counter: _incrementId,
restUrl: _restUrl,
headers: headers,
schema: schema,
isolate: _isolate,
authHttpClient: _authHttpClient,
realtime: realtime,
rest: newRest,
);
}

/// Perform a stored procedure call.
Expand Down
73 changes: 73 additions & 0 deletions packages/supabase/lib/src/supabase_query_schema.dart
@@ -0,0 +1,73 @@
import 'package:http/http.dart';
import 'package:supabase/supabase.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';

import 'counter.dart';

/// Used to perform [rpc] and [from] operations with a different schema than in [SupabaseClient].
class SupabaseQuerySchema {
final Counter _counter;
final String _restUrl;
final Map<String, String> _headers;
final String _schema;
final YAJsonIsolate _isolate;
final Client? _authHttpClient;
final RealtimeClient _realtime;
final PostgrestClient _rest;

SupabaseQuerySchema({
required Counter counter,
required String restUrl,
required Map<String, String> headers,
required String schema,
required YAJsonIsolate isolate,
required Client? authHttpClient,
required RealtimeClient realtime,
required PostgrestClient rest,
}) : _counter = counter,
_restUrl = restUrl,
_headers = headers,
_schema = schema,
_isolate = isolate,
_authHttpClient = authHttpClient,
_realtime = realtime,
_rest = rest;

/// Perform a table operation.
SupabaseQueryBuilder from(String table) {
final url = '$_restUrl/$table';
return SupabaseQueryBuilder(
url,
_realtime,
headers: {..._rest.headers, ..._headers},
schema: _schema,
table: table,
httpClient: _authHttpClient,
incrementId: _counter.increment(),
isolate: _isolate,
);
}

/// Perform a stored procedure call.
PostgrestFilterBuilder<T> rpc<T>(
String fn, {
Map<String, dynamic>? params,
}) {
_rest.headers.addAll({..._rest.headers, ..._headers});
return _rest.rpc(fn, params: params);
}

SupabaseQuerySchema useSchema(String schema) {
final newRest = _rest.useSchema(schema);
return SupabaseQuerySchema(
counter: _counter,
restUrl: _restUrl,
headers: _headers,
schema: schema,
isolate: _isolate,
authHttpClient: _authHttpClient,
realtime: _realtime,
rest: newRest,
);
}
}
1 change: 1 addition & 0 deletions packages/supabase/lib/supabase.dart
Expand Up @@ -17,5 +17,6 @@ export 'src/supabase_client.dart';
export 'src/supabase_client_options.dart';
export 'src/supabase_event_types.dart';
export 'src/supabase_query_builder.dart';
export 'src/supabase_query_schema.dart';
export 'src/supabase_realtime_error.dart';
export 'src/supabase_stream_builder.dart';

0 comments on commit c8fc248

Please sign in to comment.