Last active
July 11, 2020 05:31
-
-
Save drewrothstein/608d9216ce6959f81480f3f8d7328396 to your computer and use it in GitHub Desktop.
Example Firestore interaction w/JS
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
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