Sample Azure Functionlink
The piece of code included here shows what an Azure Functions implementation looks like in JavaScript.
Generated responselink
The sample function in this document generates the following JSON response:
{
"state": {
"transactionsCursor": "2018-01-01T00:00:00Z"
},
"insert": {
"transactions": [{
"date": "2017-12-31T05:12:05Z",
"order_id": 1001,
"amount": "$1200",
"discount": "$12"
},
{
"date": "2017-12-31T06:12:04Z",
"order_id": 1001,
"amount": "$1200",
"discount": "$12"
}
]
},
"delete": {
"transactions": [{
"date": "2017-12-31T05:12:05Z",
"order_id": 1000,
"amount": "$1200",
"discount": "$12"
},
{
"date": "2017-12-31T06:12:04Z",
"order_id": 1000,
"amount": "$1200",
"discount": "$12"
}
]
},
"schema": {
"transactions": {
"primary_key": [
"order_id",
"date"
]
}
},
"hasMore": "false"
}
For more information about each of the fields in this response, see the Sync Overview section of our Functions documentation.
JavaScriptlink
module.exports = function(context, req) {
if (req.body.state === undefined) {
context.res = {
status: 400,
body: "No state is defined!"
};
}
if (req.body.secrets === undefined) {
context.res = {
status: 400,
body: "No secret is defined!"
};
}
context.res = {
status: 200,
body: update(req.body.state, req.body.secrets)
};
context.done();
};
function update(state, secrets) {
// Fetch records using api calls
let [insertTransactions, deleteTransactions, newTransactionsCursor] = apiResponse(state, secrets);
// Populate records and state
return ({
state: {
transactionsCursor: newTransactionsCursor
},
insert: {
transactions: insertTransactions
},
delete: {
transactions: deleteTransactions
},
schema: {
transactions: {
primary_key: ['order_id', 'date']
}
},
hasMore: false
});
}
function apiResponse(state, secrets) {
var insertTransactions = [{
"date": '2017-12-31T05:12:05Z',
"order_id": 1001,
"amount": '$1200',
"discount": '$12'
},
{
"date": '2017-12-31T06:12:04Z',
"order_id": 1001,
"amount": '$1200',
"discount": '$12'
},
];
var deleteTransactions = [{
"date": '2017-12-31T05:12:05Z',
"order_id": 1000,
"amount": '$1200',
"discount": '$12'
},
{
"date": '2017-12-31T06:12:04Z',
"order_id": 1000,
"amount": '$1200',
"discount": '$12'
},
];
return [insertTransactions, deleteTransactions, '2018-01-01T00:00:00Z'];
}
For more information, read Microsoft Azure Functions' online documentation.