Skip to content

Conversation

@dshukertjr
Copy link
Member

What kind of change does this PR introduce?

Add RPC support.

What is the current behavior?

No RPC support

What is the new behavior?

Users can now mock RPC functions through the registerRpcFunction function.

 mockSupabaseHttpClient.registerRpcFunction(
   'get_status',
   (params, tables) => {'status': 'ok'},
 );

 final mockSupabase = SupabaseClient(
   'https://mock.supabase.co',
   'fakeAnonKey',
   httpClient: mockSupabaseHttpClient,
 );

 mockSupabase.rpc('get_status').select(); // returns {'status': 'ok'}

Example of an RPC function that modifies the data in the database:

mockSupabaseHttpClient.registerRpcFunction(
  'update_post_title',
  (params, tables) {
    final postId = params!['id'] as int;
    final newTitle = params!['title'] as String;
    final post = tables['public.posts']!.firstWhere((post) => post['id'] == postId);
    post['title'] = newTitle;
  },
);

final mockSupabase = SupabaseClient(
  'https://mock.supabase.co',
  'fakeAnonKey',
  httpClient: mockSupabaseHttpClient,
);

// Insert initial data
await mockSupabase.from('posts').insert([
  {'id': 1, 'title': 'Old title'},
]);

// Call the RPC function
await mockSupabase.rpc('update_post_title', params: {'id': 1, 'title': 'New title'});

// Verify that the post was modified
final posts = await mockSupabase.from('posts').select().eq('id', 1);
expect(posts.first['title'], 'New title');

@dshukertjr dshukertjr merged commit 8b0cbe5 into main Oct 25, 2024
1 check passed
@dshukertjr dshukertjr deleted the feat/rpc branch October 25, 2024 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants