Last active
January 7, 2017 22:03
-
-
Save davideast/0b5705af956d14a03fa8f35869e1efac to your computer and use it in GitHub Desktop.
Guillermo Views Transaction
This file contains hidden or 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
/** | |
* Increases view count by one. | |
* @param: db {FirebaseDatabase} - Firebase Database instance | |
* @param: id {String} - id of post | |
* @return: Promise<{committed: boolean, snapshot: nullable firebase.database.DataSnapshot}> | |
* @docs: https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction | |
*/ | |
const incrementViews = (db, id) => { | |
const ref = db.ref('views').child(id) | |
return ref.transaction(currentViews => { | |
// if it has never been set it returns null | |
if(currentViews === null) { currentViews = 0; } | |
return currentViews + 1; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment