Skip to content

Instantly share code, notes, and snippets.

@llucasshenrique
Last active September 13, 2017 14:08
Show Gist options
  • Save llucasshenrique/a802246d52808bfc865dc53a1fe406ae to your computer and use it in GitHub Desktop.
Save llucasshenrique/a802246d52808bfc865dc53a1fe406ae to your computer and use it in GitHub Desktop.
// Estrutura do Firebase Database
// {
// "formulario": { // Aqui ficam salvo as entradas que gerarão o grafico
// "formularioID":{
// "uid": "464165dsada",
// "contador1": "dado 1",
// "contador2": "dado 2"
// }
// },
// "grafico": { // Aqui fica salvo os dados para o grafico
// "uid": {
// "contador1": {
// "dado1": 0,
// "dado2": 2
// ...
// }
// },
// "contador2": {
// ...
// }
// }
// }
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.dataCounter = functions.database.ref('/relato/{postID}').onWrite( event => {
const pacikey = event.data.val().paciente;
const gatilhos = event.data.val().gatilhos;
console.log(gatilhos);
console.log(pacikey);
admin.database().ref(`/grafico/${pacikey}/gatilho/${gatilhos}`)
.transaction(count => {
if(event.data.exists() && !event.data.previous.exists()) {
return (count || 0) + 1;
} else if (!event.data.exists() && event.data.previous.exists()) {
return (count || 0) - 1;
}
}).then(() => {
console.log('Atualizado');
}).catch(error => {
console.log(error);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment