Skip to content

Instantly share code, notes, and snippets.

@mfurlend
Created February 6, 2018 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfurlend/7dd72134ed1796490b9319d3a2759a10 to your computer and use it in GitHub Desktop.
Save mfurlend/7dd72134ed1796490b9319d3a2759a10 to your computer and use it in GitHub Desktop.
#google #datastore #save #cloud functions
exports.transaction = function transaction (req, res) {
// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');
// Your Google Cloud Platform project ID
const projectId = 'moneypenny-dabc6';
// Instantiates a client
const datastore = Datastore({
projectId: projectId
});
// The kind for the new entity
const kind = 'Transaction';
// The Cloud Datastore key for the new entity
const transactionKey = datastore.key(kind);
// amount currency date recipient reference sender
// Prepares the new entity
const transaction = {
key: transactionKey,
data: {
amount: 0.00,
currency: 'CHF',
date: new Date().toJSON(),
recipient: "Bob",
sender: "Alice",
reference: "Default",
}
};
// Saves the entity
datastore.save(transaction)
.then(() => {
console.log(`Saved ${transaction.key}`);
const response = `Send ${transaction.data.amount}
${transaction.data.currency} to ${transaction.data.recipient}`.
res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type
res.send(JSON.stringify({ "speech": response, "displayText": response
//"speech" is the spoken version of the response, "displayText" is the visual version
}));
})
.catch((err) => {
console.error('ERROR:', err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment