Skip to content

Instantly share code, notes, and snippets.

@evanshortiss
Created November 2, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanshortiss/b309abf8857f1c0442f9e5416b074452 to your computer and use it in GitHub Desktop.
Save evanshortiss/b309abf8857f1c0442f9e5416b074452 to your computer and use it in GitHub Desktop.
Example of a custom list handler for use with FeedHenry Node.js Data Sync
const sync = require('fh-mbaas-api').sync
const workorders = require('lib/sql/workorders')
// Typical init code etc...
// Example of getting work orders for a specific user
sync.handleList('workorders', function(datasetId, query, metadata, done) {
// Getting by the userId passed to the sync framework and the region they are in
workorders.getDataForUserInRegion(query.userId, query.region, (err, specificUserWorkOrders) => {
if (err) {
done(err)
} else {
// Assuming the database driver returns an array, we need to convert into an
// object (hash map) for sync to utilise it
// [{
// id: 12345
// columns: {
// customerId: 54321,
// address: 'Twin Peaks, WA',
// name: 'Packard Sawmill'
// }
// }]
const ret = {}
// Reformat using a loop, could also use specificUserWorkOrders.reduce
specificUserWorkOrders.forEach((wo) => {
ret[wo.id] = wo.columns;
})
// Return data is of format:
// {
// 12345: {
// customerId: 54321,
// address: 'Twin Peaks, WA',
// name: 'Packard Sawmill'
// }
// }
done(null, ret)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment