Created
November 2, 2017 18:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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