Skip to content

Instantly share code, notes, and snippets.

@selfcontained
Last active February 1, 2017 16:19
Show Gist options
  • Save selfcontained/e3b5dc080008e1127341cc9ec3d0c3ac to your computer and use it in GitHub Desktop.
Save selfcontained/e3b5dc080008e1127341cc9ec3d0c3ac to your computer and use it in GitHub Desktop.
Slapp Firebase wrapper
'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