Search code examples
reactjsfirebasegoogle-cloud-firestorereact-reduxreact-redux-firebase

useFirestoreConnect with populates - usage/implementation


DB Setup:

- users
  - A: { // private user info }
- usersPublic
  - A: { // public user info }
- rooms
  - 1: { readAccess: ['A'] }

I have a component that displays all rooms and am fetching that in the following way:

useFirestoreConnect(() => [{collection: 'rooms'}] )

This is working fine, but I now want to also load in the info from usersPublic for each user in the rooms readAccess array.

I'm attempting to use populates in the following way:

useFirestoreConnect(() => [{
  collection: 'rooms',
  populates: [{
    root: 'usersPublic',
    child: 'A'
  }]
}])

I'm pretty sure my implementation of populates is wrong and I'm failing to understand exactly how to make this work.

I could return a bunch of other query configs for all users with read access once I have the room object but that seems inefficient and it seems that populates is meant to solve exactly this problem.

I'm also open to suggestions on modeling the DB structure - the above made sense to me and offers a nice separation between private/public user info but there might be a better way.


Solution

  • The way to do it is:

    populates: [{ root: usersPublic, child: 'read_access ' }]

    This results in a redux state that looks like:

    ...etc
    data: {
      rooms: { ... rooms ... }
      usersPublic: { A: { ... usersPublic[A] ... }, etc }
    }