Skip to content

Commit 211dcea

Browse files
committed
feat: Add rpc support
1 parent b0751c6 commit 211dcea

File tree

4 files changed

+295
-533
lines changed

4 files changed

+295
-533
lines changed

lib/src/mock_supabase_database.dart

Lines changed: 0 additions & 229 deletions
This file was deleted.

lib/src/mock_supabase_http_client.dart

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import 'dart:convert';
22

33
import 'package:http/http.dart';
44

5-
import 'mock_supabase_database.dart';
6-
75
class MockSupabaseHttpClient extends BaseClient {
86
final Map<String, List<Map<String, dynamic>>> _database = {};
97
final Map<
10-
String,
11-
dynamic Function(
12-
MockSupabaseDatabase database, Map<String, dynamic>? params)>
13-
_rpcFunctions = {};
8+
String,
9+
dynamic Function(Map<String, dynamic>? params,
10+
Map<String, List<Map<String, dynamic>>> tables)> _rpcFunctions = {};
1411

1512
MockSupabaseHttpClient();
1613

@@ -20,10 +17,82 @@ class MockSupabaseHttpClient extends BaseClient {
2017
_rpcFunctions.clear();
2118
}
2219

20+
/// Registers a RPC function that can be called using the `rpc` method on a `Postgrest` client.
21+
///
22+
/// [name] is the name of the RPC function.
23+
///
24+
/// Pass the function definition of the RPC to [function]. Use the following parameters:
25+
///
26+
/// [params] contains the parameters passed to the RPC function.
27+
///
28+
/// [tables] contains the mock database. It's a `Map<String, List<Map<String, dynamic>>>`
29+
/// where the key is `[schema].[table]` and the value is a list of rows in the table.
30+
/// Use it when you need to mock a RPC function that needs to modify the data in your database.
31+
///
32+
/// Example value of `tables`:
33+
/// ```dart
34+
/// {
35+
/// 'public.users': [
36+
/// {'id': 1, 'name': 'Alice', 'email': '[email protected]'},
37+
/// {'id': 2, 'name': 'Bob', 'email': '[email protected]'},
38+
/// ],
39+
/// 'public.posts': [
40+
/// {'id': 1, 'title': 'First post', 'user_id': 1},
41+
/// {'id': 2, 'title': 'Second post', 'user_id': 2},
42+
/// ],
43+
/// }
44+
/// ```
45+
///
46+
/// Example of registering a RPC function:
47+
/// ```dart
48+
/// mockSupabaseHttpClient.registerRpcFunction(
49+
/// 'get_status',
50+
/// (params, tables) => {'status': 'ok'},
51+
/// );
52+
///
53+
/// final mockSupabase = SupabaseClient(
54+
/// 'https://mock.supabase.co',
55+
/// 'fakeAnonKey',
56+
/// httpClient: mockSupabaseHttpClient,
57+
/// );
58+
///
59+
/// mockSupabase.rpc('get_status').select(); // returns {'status': 'ok'}
60+
/// ```
61+
///
62+
/// Example of an RPC function that modifies the data in the database:
63+
/// ```dart
64+
/// mockSupabaseHttpClient.registerRpcFunction(
65+
/// 'update_post_title',
66+
/// (params, tables) {
67+
/// final postId = params!['id'] as int;
68+
/// final newTitle = params!['title'] as String;
69+
/// final post = tables['public.posts']!.firstWhere((post) => post['id'] == postId);
70+
/// post['title'] = newTitle;
71+
/// },
72+
/// );
73+
///
74+
/// final mockSupabase = SupabaseClient(
75+
/// 'https://mock.supabase.co',
76+
/// 'fakeAnonKey',
77+
/// httpClient: mockSupabaseHttpClient,
78+
/// );
79+
///
80+
/// // Insert initial data
81+
/// await mockSupabase.from('posts').insert([
82+
/// {'id': 1, 'title': 'Old title'},
83+
/// ]);
84+
///
85+
/// // Call the RPC function
86+
/// await mockSupabase.rpc('update_post_title', params: {'id': 1, 'title': 'New title'});
87+
///
88+
/// // Verify that the post was modified
89+
/// final posts = await mockSupabase.from('posts').select().eq('id', 1);
90+
/// expect(posts.first['title'], 'New title');
91+
/// ```
2392
void registerRpcFunction(
2493
String name,
25-
dynamic Function(
26-
MockSupabaseDatabase database, Map<String, dynamic>? params)
94+
dynamic Function(Map<String, dynamic>? params,
95+
Map<String, List<Map<String, dynamic>>> tables)
2796
function) {
2897
_rpcFunctions[name] = function;
2998
}
@@ -758,8 +827,7 @@ class MockSupabaseHttpClient extends BaseClient {
758827
final function = _rpcFunctions[functionName]!;
759828

760829
try {
761-
final mockDatabase = MockSupabaseDatabase(_database);
762-
final result = function(mockDatabase, body);
830+
final result = function(body, _database);
763831
return _createResponse(result, request: request);
764832
} catch (e) {
765833
return _createResponse({'error': 'RPC function execution failed: $e'},

0 commit comments

Comments
 (0)