Last active
February 1, 2017 16:19
-
-
Save selfcontained/e3b5dc080008e1127341cc9ec3d0c3ac to your computer and use it in GitHub Desktop.
Slapp Firebase wrapper
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
'use strict' | |
const path = require('path') | |
const firebase = require('firebase-admin') | |
module.exports = () => { | |
const firebaseDBUrl = process.env.FIREBASE_DB_URL | |
const serviceAccountBase64 = process.env.FIREBASE_SERVICE_ACCOUNT_BASE64 | |
firebase.initializeApp({ | |
credential: firebase.credential.cert(b64ToObject(serviceAccountBase64)), | |
databaseURL: firebaseDBUrl | |
}) | |
let database = firebase.database() | |
return { | |
saveTeam (id, data, done) { | |
database.ref(`teams/${id}`).set(data, (err) => { | |
if (err) { | |
return done(err) | |
} | |
return done(null) | |
}) | |
}, | |
getTeam (id, done) { | |
database.ref(`teams/${id}`).once('value', (snapshot) => { | |
done(null, snapshot.val()) | |
}, done) | |
}, | |
saveConvo (id, data, done) { | |
database.ref(`convos/${id}`).set(data, (err) => { | |
if (err) { | |
return done(err) | |
} | |
return done(null) | |
}) | |
}, | |
getConvo (id, done) { | |
database.ref(`convos/${id}`).once('value', (snapshot) => { | |
done(null, snapshot.val()) | |
}, done) | |
}, | |
deleteConvo (id, done) { | |
database.ref(`convos/${id}`).remove(done) | |
} | |
} | |
} | |
function b64ToObject (b64) { | |
return !b64 ? {} : JSON.parse(Buffer.from(b64, 'base64').toString('ascii')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment