From 24ff58c9a02e67635e36007bfed9df49829a2a59 Mon Sep 17 00:00:00 2001 From: Dogan AY Date: Fri, 23 Jan 2026 11:09:24 +0100 Subject: [PATCH] feat(rpc): mark collection as rpc handles regex --- .../lib/forest_admin_rpc_agent/agent.rb | 12 ++++-- .../lib/forest_admin_rpc_agent/agent_spec.rb | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb index a99eccced..d6038683a 100644 --- a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb +++ b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/agent.rb @@ -54,6 +54,12 @@ def schema_hash_matches?(provided_hash) private + def rpc_collection?(name) + @rpc_collections.any? do |pattern| + pattern.is_a?(Regexp) ? pattern.match?(name) : pattern == name + end + end + def should_skip_schema_update? ForestAdminRpcAgent::Facades::Container.cache(:skip_schema_update) == true end @@ -84,11 +90,11 @@ def build_rpc_schema_from_datasource(datasource) datasource.collections.each_value do |collection| relations = {} - if @rpc_collections.include?(collection.name) + if rpc_collection?(collection.name) # RPC collection → extract relations to non-RPC collections collection.schema[:fields].each do |field_name, field| next if field.type == 'Column' - next if @rpc_collections.include?(field.foreign_collection) + next if rpc_collection?(field.foreign_collection) relations[field_name] = field end @@ -96,7 +102,7 @@ def build_rpc_schema_from_datasource(datasource) fields = {} collection.schema[:fields].each do |field_name, field| - if field.type != 'Column' && @rpc_collections.include?(field.foreign_collection) + if field.type != 'Column' && rpc_collection?(field.foreign_collection) relations[field_name] = field else if field.type == 'Column' diff --git a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/agent_spec.rb b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/agent_spec.rb index f79322aec..902dcfc02 100644 --- a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/agent_spec.rb +++ b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/agent_spec.rb @@ -194,6 +194,18 @@ module ForestAdminRpcAgent expect(instance.rpc_collections).to include('users', 'orders') end + it 'accepts regex patterns' do + instance.mark_collections_as_rpc(/^rpc_/, /.*_private$/) + + expect(instance.rpc_collections).to include(/^rpc_/, /.*_private$/) + end + + it 'accepts mixed strings and regex patterns' do + instance.mark_collections_as_rpc('users', /^admin_/) + + expect(instance.rpc_collections).to include('users', /^admin_/) + end + it 'returns self for method chaining' do result = instance.mark_collections_as_rpc('products') @@ -268,6 +280,31 @@ module ForestAdminRpcAgent expect(collection_names).not_to include('RpcCollection') end + it 'excludes collections matching regex patterns from schema collections' do + rpc_collection1 = instance_double(ForestAdminDatasourceToolkit::Collection) + rpc_collection2 = instance_double(ForestAdminDatasourceToolkit::Collection) + normal_collection = instance_double(ForestAdminDatasourceToolkit::Collection) + + allow(rpc_collection1).to receive_messages(name: 'rpc_users', schema: { fields: {} }) + allow(rpc_collection2).to receive_messages(name: 'rpc_orders', schema: { fields: {} }) + allow(normal_collection).to receive_messages(name: 'products', schema: { fields: {} }) + allow(datasource).to receive_messages( + collections: { + 'rpc_users' => rpc_collection1, + 'rpc_orders' => rpc_collection2, + 'products' => normal_collection + }, + live_query_connections: {} + ) + + instance.mark_collections_as_rpc(/^rpc_/) + instance.send_schema + + collection_names = instance.cached_schema[:collections].map { |c| c[:name] } + expect(collection_names).to include('products') + expect(collection_names).not_to include('rpc_users', 'rpc_orders') + end + it 'extracts relations from RPC collections to non-RPC collections into rpc_relations' do rpc_collection = instance_double(ForestAdminDatasourceToolkit::Collection) normal_collection = instance_double(ForestAdminDatasourceToolkit::Collection)