Created
April 5, 2017 23:43
-
-
Save robconery/a0ae70e675864428204e6d109e23f02c to your computer and use it in GitHub Desktop.
A Firebase Wrapper for Node
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
const admin = require("firebase-admin"); | |
const Promise = require("bluebird"); | |
const cert = require("./cert.json"); | |
const Emitter = require("events").EventEmitter; | |
const assert = require('assert'); | |
//using this for convenience | |
const shortid = require("shortid"); | |
shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@'); | |
admin.initializeApp({ | |
credential: admin.credential.cert(cert), | |
databaseURL: "YOUR DB" | |
}); | |
class DB extends Emitter{ | |
constructor(args){ | |
super(); | |
assert(args.path, "Need a path set at least"); | |
this.path = args.path; | |
if(args.listen) this.listen(); | |
} | |
db(){ | |
return admin.database().ref(`/${this.path}`); | |
} | |
last(limit=100){ | |
return admin.database().ref(`/${this.path}`) | |
.orderByKey() | |
.limitToLast(limit) | |
.once("value") | |
.then(snap => { | |
const out = []; | |
snap.forEach(item => { | |
out.push(item.val()) | |
}); | |
return Promise.resolve(out); | |
}) | |
.catch(err => Promise.reject(err)); | |
} | |
find(key, val, opts={}){ | |
console.log(key) | |
console.log(val) | |
return admin.database().ref(`/${this.path}`) | |
.orderByChild(key) | |
.equalTo(val) | |
.once("value") | |
.then(snap => { | |
let out = []; | |
snap.forEach(x => { | |
out.push(x.val()) | |
}); | |
if(opts.single){ | |
return Promise.resolve(out[0]); | |
}else{ | |
return Promise.resolve(out); | |
} | |
}) | |
.catch(err => Promise.reject(err)); | |
} | |
filter(key, args={}, opts={}){ | |
assert(args.startsWith || args.endsWith, "Need a startsWith and/or endsWith"); | |
const query = admin.database().ref(`/${this.path}`) | |
.orderByChild(key); | |
if(args.startsWith) query.startAt(args.startsWith); | |
if(args.endsWith) query.endAt(args.endsWith); | |
return query.once("value") | |
.then(snap => { | |
const out = []; | |
snap.forEach(item => out.push(item.val())); | |
if(opts.single){ | |
return Promise.resolve(out[0]); | |
}else{ | |
return Promise.resolve(out); | |
} | |
}) | |
.catch(err => Promise.reject(err)); | |
} | |
get(id){ | |
return admin.database().ref(`/${this.path}/${id}`).once("value") | |
.then(snap => Promise.resolve(snap.val())) | |
.catch(err => Promise.reject(err)); | |
} | |
update(id, args){ | |
return admin.database().ref(`/${this.path}/${id}`).update(args); | |
} | |
delete(id){ | |
return admin.database().ref(`/${this.path}/${id}`).remove(); | |
} | |
save(data){ | |
if(!data.id) data.id = shortid.generate(); | |
return admin.database().ref(`/${this.path}/${data.id}`).set(data); | |
} | |
listen(){ | |
admin.database().ref(`/${this.path}`).on("child_added", snap => { | |
this.emit("add", snap.val()) | |
}); | |
admin.database().ref(`/${this.path}`).on("child_changed", snap => this.emit("change", snap.val())); | |
admin.database().ref(`/${this.path}`).on("child_removed", snap => this.emit("remove", snap.val())); | |
} | |
} | |
module.exports = DB; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment