Skip to content
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

refactor!(supabase): disallow multiple stream filter #644

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
152 changes: 152 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,152 @@
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) {
assert(
_streamFilter == null,
'Only one filter can be applied to `.stream()`',
);
Vinzent03 marked this conversation as resolved.
Show resolved Hide resolved
_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;
}
}
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';