Skip to content

Commit

Permalink
refactor!(supabase): disallow multiple stream filter (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 authored Sep 27, 2023
1 parent 1cb7243 commit c9ec6bf
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 143 deletions.
5 changes: 2 additions & 3 deletions packages/supabase/lib/src/supabase_query_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:http/http.dart';
import 'package:supabase/src/supabase_stream_builder.dart';
import 'package:supabase/supabase.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';

Expand Down Expand Up @@ -45,9 +44,9 @@ class SupabaseQueryBuilder extends PostgrestQueryBuilder {
/// ```dart
/// supabase.from('chats').stream(primaryKey: ['id']).eq('room_id','123').order('created_at').limit(20).listen(_onChatsReceived);
/// ```
SupabaseStreamBuilder stream({required List<String> primaryKey}) {
SupabaseStreamFilterBuilder stream({required List<String> primaryKey}) {
assert(primaryKey.isNotEmpty, 'Please specify primary key column(s).');
return SupabaseStreamBuilder(
return SupabaseStreamFilterBuilder(
queryBuilder: this,
realtimeClient: _realtime,
realtimeTopic: '$_schema:$_table:$_incrementId',
Expand Down
142 changes: 2 additions & 140 deletions packages/supabase/lib/src/supabase_stream_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:async';
import 'package:rxdart/rxdart.dart';
import 'package:supabase/supabase.dart';

part 'supabase_stream_filter_builder.dart';

enum _FilterType { eq, neq, lt, lte, gt, gte, inFilter }

class _StreamPostgrestFilter {
Expand Down Expand Up @@ -78,146 +80,6 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
_table = table,
_uniqueColumns = primaryKey;

/// Filters the results where [column] equals [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).eq('name', 'Supabase');
/// ```
SupabaseStreamBuilder eq(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.eq,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] does not equal [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).neq('name', 'Supabase');
/// ```
SupabaseStreamBuilder neq(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.neq,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is less than [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).lt('likes', 100);
/// ```
SupabaseStreamBuilder lt(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.lt,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is less than or equal to [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).lte('likes', 100);
/// ```
SupabaseStreamBuilder lte(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.lte,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is greater than [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).gt('likes', '100');
/// ```
SupabaseStreamBuilder gt(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.gt,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is greater than or equal to [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).gte('likes', 100);
/// ```
SupabaseStreamBuilder gte(String column, Object value) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.gte,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is included in [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).inFilter('name', ['Andy', 'Amy', 'Terry']);
/// ```
SupabaseStreamBuilder inFilter(String column, List<Object> values) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.inFilter,
column: column,
value: values,
);
return this;
}

/// Orders the result with the specified [column].
///
/// When `ascending` value is true, the result will be in ascending order.
Expand Down
124 changes: 124 additions & 0 deletions packages/supabase/lib/src/supabase_stream_filter_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
part of './supabase_stream_builder.dart';

class SupabaseStreamFilterBuilder extends SupabaseStreamBuilder {
SupabaseStreamFilterBuilder({
required super.queryBuilder,
required super.realtimeTopic,
required super.realtimeClient,
required super.schema,
required super.table,
required super.primaryKey,
});

/// Filters the results where [column] equals [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).eq('name', 'Supabase');
/// ```
SupabaseStreamBuilder eq(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.eq,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] does not equal [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).neq('name', 'Supabase');
/// ```
SupabaseStreamBuilder neq(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.neq,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is less than [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).lt('likes', 100);
/// ```
SupabaseStreamBuilder lt(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.lt,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is less than or equal to [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).lte('likes', 100);
/// ```
SupabaseStreamBuilder lte(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.lte,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is greater than [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).gt('likes', '100');
/// ```
SupabaseStreamBuilder gt(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.gt,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is greater than or equal to [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).gte('likes', 100);
/// ```
SupabaseStreamBuilder gte(String column, Object value) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.gte,
column: column,
value: value,
);
return this;
}

/// Filters the results where [column] is included in [value].
///
/// Only one filter can be applied to `.stream()`.
///
/// ```dart
/// supabase.from('users').stream(primaryKey: ['id']).inFilter('name', ['Andy', 'Amy', 'Terry']);
/// ```
SupabaseStreamBuilder inFilter(String column, List<Object> values) {
_streamFilter = _StreamPostgrestFilter(
type: _FilterType.inFilter,
column: column,
value: values,
);
return this;
}
}
1 change: 1 addition & 0 deletions packages/supabase/lib/supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export 'src/supabase_client.dart';
export 'src/supabase_event_types.dart';
export 'src/supabase_query_builder.dart';
export 'src/supabase_realtime_error.dart';
export 'src/supabase_stream_builder.dart';

0 comments on commit c9ec6bf

Please sign in to comment.