Change polling
This is the official documentation of the @forestadmin/agent
Node.js agent.
Fetching all the data from the target API at each update is not always possible, as it may be too slow, too expensive, or over the API rate limit.
Another strategy is to poll for changes on the target API and only fetch the records that changed.
This strategy is called "change polling".
Choosing when to poll for changes
The following events are available:
pullDeltaOnRestart
: When true, your handler will be called on each agent restartpullDeltaOnSchedule
: if set to a cron-like schedule, your handler will be called on that schedule. The syntax is the same as forpullDumpOnSchedule
pullDeltaOnBeforeAccess
: When true, your handler will be called before each access to the data source and the GUI will block until the handler returnspullDeltaOnAfterWrite
: When true, your handler will be called after each write to the data source and the GUI will block until the handler returns
And an extra option is available to allow event grouping: pullDeltaOnBeforeAccessDelay
This value in milliseconds will add a delay to all read requests that are sent to your agent. This delay will allow your agent to group multiple requests that are sent during this delay and call your handler only once.
You can set this value to 0 to disable this feature or find a good balance between the number of calls to the target API and an acceptable delay.
const myCustomDataSource = createReplicaDataSource({
pullDeltaOnRestart: true, // Update cache each time the agent restarts.
pullDeltaOnSchedule: '0 0 * * *', // Update cache each day at midnight.
// Update cache each time data is accessed or written to from the GUI.
pullDeltaOnBeforeAccess: true,
pullDeltaOnAfterWrite: true,
// Delay all read requests by 50ms to allow request grouping.
pullDeltaOnBeforeAccessDelay: 50,
// Handler to the records that changed from the API.
pullDeltaHandler: async request => {
// Implement handler here
},
});
Programming your handler
To be able to fetch only the records that changed, you need to implement a pullDeltaHandler
function.
To know which records may have changed: a state object is preserved between calls, and you are allowed to read from the cache.
Example
const myCustomDataSource = createReplicaDataSource({
// Update cache before each data access
pullDeltaOnBeforeAccess: true,
// Handler to the records that changed from the API.
pullDeltaHandler: async request => {
// Initialize variables
const url = 'https://jsonplaceholder.typicode.com';
const nextDeltaState = { ...request.previousDeltaState };
const newOrUpdatedEntries = [];
const deletedEntries = [];
// Get records that changed on the target API
for (const collection of request.affectedCollections) {
// Extract last timestamp for this collection from previousDeltaState
const mostRecentDate = request.previousDeltaState[collection];
// Request API
const filter = `or($gt(updatedAt,${timestamp}), $gt(deletedAt,${timestamp}))`;
const response = await axios.get(`${url}/${collection}?filter=${filter}`);
const entries = response.data.map(record => ({ collection, record }));
// Append the new records to the lists of entries
newOrUpdatedEntries.push(...entries.filter(entry => !entry.record.deletedAt));
deletedEntries.push(...entries.filter(entry => entry.record.deletedAt));
}
return { more: false, nextDeltaState, newOrUpdatedEntries, deletedEntries };
},
});
Last updated
Was this helpful?