mesqueeb / vuex-easy-firestore

Easy coupling of firestore and a vuex module. 2-way sync with 0 boilerplate!
https://mesqueeb.github.io/vuex-easy-firestore
MIT License
234 stars 28 forks source link

How to openDBChannel in doc mode - where to put the id of the doc? #351

Closed andersbc closed 4 years ago

andersbc commented 4 years ago

Sorry to bother you with this simple question, I have looked but just can't find it in the docs.. :-(

I want to open a realtime listener for a specific doc ..but i can't figure out where to put the id of the doc:

// file: MyComponent.vue

beforeMount() {
// my naive attempt doesnt work: 
  this.$store.dispatch('client/openDBChannel', { where: [['id', '==', someUserId]] })
}

This is my setup:

// vuex module / vuex-easy-firestore
const userModule = {
  firestorePath: 'users',
  firestoreRefType: 'doc',
  moduleName: 'user',
  statePropName: 'data',
  namespaced: true,

  state: {},
  getters: {
    get: (state) => state.data,
  },
}

export default userModule

Any help appreciated, thanx :-)

mesqueeb commented 4 years ago

@andersbc with doc mode you need the id in your firestorePath. Eg. firestorePath: 'users/{userId}',

btw, {userId} is a special path variable that automatically uses Firebase Auth userId of the authenticated user.

If you want to open the module for other users as well you need to do:

firestorePath: 'users/{id}'

and

this.$store.dispatch('user/openDBChannel', {pathVariables: {id: 'some-user-id'}})

More info on this feature here: https://mesqueeb.github.io/vuex-easy-firestore/extra-features.html#variables-for-firestorepath-or-clauses

--
Vuex Easy Firestore was made with ♥ by Luca Ban.
If you use this library in your projects, you can support the maintenance of this library by a small contribution via Github 💜.
You can also reach out on twitter if you want a one-on-one coding review/lesson. 🦜

andersbc commented 4 years ago

Thanx, @mesqueeb :-)

I went with using a collection, as you suggest in the link you provided.. to save a round trip to firestore. I already had a users collection set up.

You probably know this, but in case other reads this, you can query a firestore collection for a single doc, by it's id ...with this trick:

this.$store.dispatch('users/openDBChannel', {
  where: [['__name__', '==', props.clientId]],
})

apparently '__name__' works as a placeholder for the doc id in a firebase query (https://stackoverflow.com/questions/47876754/query-firestore-database-for-document-id)

So, in my component I first check if my particular user is included in my already fetched users collection (getById getter), and if not I issue the openDBChannelcommand above to populate the collection with the single user I need for that particular component.