Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active December 30, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidgilbertson/2988945ea2f075d8591613c985cd1c35 to your computer and use it in GitHub Desktop.
Save davidgilbertson/2988945ea2f075d8591613c985cd1c35 to your computer and use it in GitHub Desktop.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const uuid = require('uuid/v4');
admin.initializeApp(functions.config().firebase);
const collection = admin.firestore().collection('all');
module.exports = {
async create(data) {
const id = uuid();
await collection.doc(id).create(data);
return id;
},
async read(id) {
const docRef = await collection.doc(id).get();
return docRef.exists ? docRef.data() : undefined;
},
async update(id, data) {
// Update must fail if the specified item does not already exist
const docRef = await collection.doc(id).get();
if (!docRef.exists) return false;
await collection.doc(id).set(data);
return true;
},
async delete(id) {
return await collection.doc(id).delete();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment