redux-firestore
redux-firestore copied to clipboard
feat(actions): uniqueSet() for transaction capability
What is the feature? Exposing a uniqueSet() in props.firestore that is similar to the one within props.firebase would be fantastic for use cases where multiple clients are writing to the same document.
Originally requested in 419 of react-redux-firebase by @giles-t
What version would this apply to?
Do you have thoughts/examples on how the feature would be used (i.e. API)?
uniqueSet would match what react-redux-firebase does for RTDB, so that makes the most sense
const uniqueSet = (docRef, value, onComplete) => firebase
.runTransaction(
transaction => transaction
.get(docRef)
.then(docSnap => {
if (docSnap.exists) {
const newError = new Error('Document already exists.')
if (onComplete) onComplete(newError)
return Promise.reject(newError)
}
return transaction.set(value)
})
}
)
.then(() => {
if (onComplete) onComplete()
})
}
What do you think? Haven't tested yet. Just want to know if this is a desire behavior or not.
Yup, that is the desired behavior @illuminist, thanks for posting. Feel free to make a PR with the changes, and we can get them in 😄
Does it need to use redux-firestore function? Somehow I feel like I need to make it accept QueryOption like the other functions.
@illuminist yeah, it would live in redux-firestore since that is where the other Firestore specific actions live. Accepting QueryOption like you mentioned would be good to match the other call signatures