Skip to content

Instantly share code, notes, and snippets.

@spac3unit
Forked from victorsilent/firebase.js
Created October 8, 2018 01:37
Show Gist options
  • Select an option

  • Save spac3unit/685a3f53bc2312ff1abea57cdb4acd9b to your computer and use it in GitHub Desktop.

Select an option

Save spac3unit/685a3f53bc2312ff1abea57cdb4acd9b to your computer and use it in GitHub Desktop.
import { firestore } from './firebase'
export function updateDocument(path, data) {
return firestore.doc(path).update(data);
}
export function deleteDocument(path) {
return firestore.doc(path).delete();
}
export function addDocument(path = '/', collection, data) {
return firestore.doc(path).collection(collection).add(data);
}
export function setDocument(path, data) {
return firestore.doc(path).set(data);
}
export function snapshotDocuments(query, callback) {
return query.onSnapshot(documents => {
const documentsWithReference = documents.docs.map(document => ({
...document.data(),
selfReference: document.ref.path,
}))
callback(documentsWithReference);
})
}
export function queryDocuments(query, callback) {
return query.get().then(documents => {
const documentsWithReference = documents.docs.map(document => ({
...document.data(),
selfReference: document.ref.path,
}))
callback(documentsWithReference);
})
}
export function getDocument(path) {
return firestore.doc(path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment