samedi 11 juillet 2020

Firebase Query Specific List From Node

As a relatively new user to Firebase Realtime, I created a Vuejs app using Quasar and Firebase. The app successfully creates, reads, updates and deletes database records. Now that I'm building out more features, I've run into a problem that seems pretty basic and any advice would be appreciated.

DESIGN
A simplfied view of the database looks like this where users and groups need to be scalable.

-users
  -user1_ID
    -groups:
      - group1_ID
      - group4_ID

-groups
  -group1_ID
    -members:
      -user1_ID
      -user22_ID
  -group2_ID
    -members:
      -user33_ID
      -user22_ID
      -user57_ID
  -group3_ID
    -members:
      -user8_ID
      -user76_ID
  -group4_ID
    -members:
      -user1_ID

The app reads user data from the store using this method:

  fbReadUserData({ commit, dispatch }) {
    let userId = firebaseAuth.currentUser.uid
    let userData = firebaseDb.ref('users/' + userId)

    // initial check for data
    userData.once('value', snapshot => {
      commit('setUserDataDownloaded', true)
      dispatch('groups/fbReadGroups', null, {root: true})
    }, error => {
      showErrorMessage(error.message)
      this.$router.replace('/auth')
    })

    // child added
    userData.on('child_added', snapshot => {
      let user = snapshot.val()
      let payload = {
        id: snapshot.key,
        user: user
      }
      commit('addUser', payload)
    })
  }

Once fbReadUserData() reads the user data, the app has the IDs of the groups that the user belongs and then fetches those group objects with fbReadGroups().

PROBLEM
After pouring through the Firebase docs and many examples, I have not been able to find a solution that reads all of the groups for a user using a single query. I will likely run into similar problems for updating group objects.

QUESTIONS
What is the recommended pattern (code example) for reading all of the groups where the user is a member using a single query? Is there a better, more scalable architecture I should use that keeps the app efficient and reactive?

Thank you for your time!

Aucun commentaire:

Enregistrer un commentaire