This is the official documentation of the agent_ruby Ruby agent.
Forest Admin can connect to any data source, as long as it can be represented as a collection of records that have a common structure.
To achieve that, Forest Admin needs to abstract away data source differences: each connector "speaks" the language of a given API on one side and exposes the Forest Admin Query Interface on the other.
This interface is called the Forest Admin Query Interface, it is not a full-featured ORM: its objective is to be "just enough" to fuel Forest Admin.
Choosing how to query your data
The Forest Admin Query Interface is used to implement all native features of the admin panel, however, when writing custom code (creating new actions, fields, ...), you can either access your data using the Forest Admin Query Interface or using the native driver.
The choice is yours, and you will probably use both depending on the situation.
-
Forest Admin Query Interface
Native Driver
Code consistency
👍 Use the same query interface for all data sources
👎 Different API for each database / SaaS
Customizations can be queried (computed field, relationships, ...)
👍 Yes
👎 No
Features
👎 Common denominator is exposed
👍 All features of the underlying API
In-app deployments
👎 Difficult to reuse your existing code
👍 Re-use your existing code
Learning curve
👎 The interface is Forest Admin specific
👍 You already know how to write SQL
Native support for filters from the UI
👍 Yes
👎 No
Total
3 x 👍 + 3 x 👎
3 x 👍 + 3 x 👎
In practice
Querying with the native driver
As the name implies, native drivers have different interfaces for each data source.
moduleForestAdminRailsclassCreateAgentdefself.customize @create_agent.customize_collection('customer') do|collection| collection.add_segment('highPrice') do|context| rows =ActiveRecord::Base.connection.execute('SELECT p.id as product_id, COUNT(o.id) as nb FROM orders o INNER JOIN products p ON o.product_id = p.id GROUP BY p.id ORDER BY nb DESC LIMIT 10;' ) { field: 'id', operator: 'In', value: rows.map { |r| r['product_id'] } }endendendendend
Querying with the Forest Admin Query Interface
Queries can be executed directly, by calling the methods exposed by context.dataSource and context.collection.
classDatasourceContractdefcollectionsraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefchartsraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefget_collection(name)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefadd_collection(collection)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefrender_chart(caller, name)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"endend
Collection Interface
Parameters are explained in depth on the following pages:
classCollectionContractdefdatasourceraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefschemaraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefnameraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefexecute(caller, name, data, filter=nil)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefget_form(caller, name, data=nil, filter=nil, metas= {})raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefcreate(caller, data)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddeflist(caller, filter, projection)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefupdate(caller, filter, data)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefdelete(caller, filter)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefaggregate(caller, filter, aggregation, limit=nil)raiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"enddefrender_chartraiseNotImplementedError,"#{self.class} has not implemented method '#{__method__}'"endend