Read implementation
const { BaseCollection } = require('@forestadmin/datasource-toolkit');
const axios = require('axios');
/**
* This collection will have terrible performance, but is perfect to test that the
* structure declaration is well done.
*/
class MyCollection extends BaseCollection {
// [... Declare structure and capabilities]
async list(caller, filter, projection) {
// Fetch all records on all requests (this is _very_ inefficient)
const response = await axios.get('https://my-api/my-collection');
const result = response.data.items;
// Use "in-process emulation" for everything else.
if (filter.conditionTree)
result = filter.conditionTree.apply(result, this, caller.timezone);
if (filter.sort) result = filter.sort.apply(result);
if (filter.page) result = filter.page.apply(result);
if (filter.segment)
throw new Error('This collection does not implements native segments');
if (filter.search) throw new Error('This collection is not natively searchable');
return projection.apply(result);
}
async aggregate(caller, filter, aggregation, limit) {
// Fetch all records which should be aggregated
const records = await this.list(caller, filter, aggregation.projection);
// Use "in-process emulation" to aggregate the results
return aggregation.apply(records, caller.timezone, limit);
}
}Tips
Count queries
Last updated