This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.
Developing your query translation layer is much easier when you can preview your work and have intermediary deliverables.
Emulation comes to the rescue: all features that need to be implemented when making a translating data source can be emulated inside your Node.js, at the cost of performance.
This enables you to be up and running in minutes and then optimize your code as you go.
from typings import Optionalfrom forestadmin.agent_toolkit.utils.context import Userfrom forestadmin.datasource_toolkit.collections import Collectionfrom forestadmin.datasource_toolkit.interfaces.query.aggregation import AggregateResult, Aggregationfrom forestadmin.datasource_toolkit.interfaces.query.filter.paginated import PaginatedFilterfrom forestadmin.datasource_toolkit.interfaces.query.filter.unpaginated import Filterfrom forestadmin.datasource_toolkit.interfaces.query.projections import Projectionfrom forestadmin.datasource_toolkit.interfaces.records import RecordsDataAliasimport requestsclassMyCollection(Collection):""" This collection will have terrible performance, but is perfect to test that the structure declaration is well done."""# [... Declare structure and capabilities]asyncdeflist(self,caller: User,filter_: PaginatedFilter,projection: Projection ) -> List[RecordsDataAlias]:# Fetch all records on all requests (this is _very_ inefficient) response = requests.get('https://my-api/my-collection') result = response.json()["items"]# Use "in-process emulation" for everything else.if filter_.condition_tree: result = filter_.condition_tree.apply(result, self, str(caller.timezone))if filter_.sort: result = filter_.sort.apply(result)if filter_.page: result = filter_.page.apply(result)if filter_.segment:raiseException('This collection does not implements native segments')if filter_.search:raiseException('This collection is not natively searchable')return projection.apply(result)asyncdefaggregate(self,caller: User,filter_: Filter,aggregation: Aggregation,limit: Optional[int] ) -> List[AggregateResult]:# Fetch all records which should be aggregated records =await self.list( caller, PaginatedFilter.from_base_filter(filter_), aggregation.projection )# Use "in-process emulation" to aggregate the resultsreturn aggregation.apply(records, str(caller.timezone), limit)
Tips
Count queries
The aggregate method is used by Forest Admin both to count records and to extract the data which is needed to generate charts.
If the API/Database you are targeting has an efficient API that is made for counting records, you may want to handle this case first.