Skip to content

Instantly share code, notes, and snippets.

@drewrothstein
Last active July 11, 2020 05:31
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 drewrothstein/608d9216ce6959f81480f3f8d7328396 to your computer and use it in GitHub Desktop.
Save drewrothstein/608d9216ce6959f81480f3f8d7328396 to your computer and use it in GitHub Desktop.
Example Firestore interaction w/JS
query.get()
.then((doc) => {
if (!doc.exists) {
console.log('Item: No such document for:', loggingEvent);
collection.add(item)
.then((documentReference) => {
console.log(`Added item with ref/id: ${documentReference.id}`);
track('Wrote Message to DB Completed', {
team: item.team,
channel: item.channel,
});
})
.then((documentReference) => {
// Dual-write event without message for summary
const docPath = (item.type === 'message') ? 'Messages' : 'Reactions';
const summaryCollection = db.collection('Summaries').doc(docPath).collection(item.team)
.doc(item.channel).collection(item.channel);
const itemSafeCopy = Object.assign({}, item);
if (itemSafeCopy.type === 'message') {
delete itemSafeCopy.message;
}
summaryCollection.add(itemSafeCopy)
.then((documentReference) => {
console.log(`Dual-wrote item for summary with ref/id: ${documentReference.id}`);
track('Wrote Message to DB Copy to Summary Completed', {
team: item.team,
channel: item.channel,
});
});
});
} else {
console.log('Item: Document already exists (skipping) for:', loggingEvent);
}
})
.catch((err) => {
console.log('Item: Error getting document for:', loggingEvent, 'Error:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment