redux-firestore icon indicating copy to clipboard operation
redux-firestore copied to clipboard

feat(actions): uniqueSet() for transaction capability

Open prescottprue opened this issue 6 years ago • 4 comments
trafficstars

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

prescottprue avatar Apr 21 '19 18:04 prescottprue

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.

illuminist avatar May 16 '19 10:05 illuminist

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 😄

prescottprue avatar May 20 '19 23:05 prescottprue

Does it need to use redux-firestore function? Somehow I feel like I need to make it accept QueryOption like the other functions.

illuminist avatar May 24 '19 06:05 illuminist

@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

prescottprue avatar May 30 '19 22:05 prescottprue