Querying user specific data (incl. population) wit

2019-07-29 12:19发布

问题:

i'm working with react-native and have the problem to load only user specific data with firebaseConnect. Imagine there are thousends of todos, users and doers and don't want to reference all elements of them.

First of all, a shortcut of my data structure in the real time database:

- todos
  |- cleanRoom
  |    |- createdBy: alica
  |    |- title: "Clean the room up"
  |    |- doerList
  |        |- roomCleanerMichael
  |        |- roomCleanerAlica
  |- playBall
       |- createdBy: michael
       |- title: "Play the ball"
       |- doerList
           |- ballPlayerAlica

- users
  |- michael
  |    |- name: Michael
  |    |- age: 22
  |- alica
       |- name: Alica
       |- age: 27

- doers
  |- roomCleanerMichael
  |    |- user: michael
  |    |- status: done
  |- roomCleanerAlica
  |    |- user: alica
  |    |- status: done
  |- ballPlayerAlica
       |- user: alica
       |- status: done

I have a view/screen in RN, where all informations should be displayed. For now, i have...

const populatesTodoCreator = [{child: 'createdBy', root: 'users'}]

const mapStateToProps = (state, props) => {
  return {
    todos: populate(state.firebase, 'todos', populatesTodoCreator),
    uid: state.firebase.auth.uid
  }
}

export default compose(
  firebaseConnect((props, store) => {
      return [
        {path: 'todos', populatesTodoCreator},
      ]
    }
  ),
  connect(mapStateToProps, mapDispatchToProps)
)(TodoOverviewScreen)

When i log in the application with firebase.auth.uid = michael, i only want to reference the list of todos, where michael participates in as doer and populate all doers in doersList (including the user in a specific doer), and that all in only one single compose method.

So the resulting redux state should look like this, because michael is only a doer for cleanRoom:

- todos
  |- cleanRoom
      |- createdBy
      |    |- name: Alica
      |    |- age: 27
      |- title: "Clean the room up"
      |- doerList
           |- roomCleanerMichael
           |     |- user
           |     |    |- name: Michael
           |     |    |- age: 22
           |     |- status: done
           |- roomCleanerAlica
                 |- user
                 |    |- name: Alica
                 |    |- age: 27
                 |- status: done

Is this task possible? If yes, how do i get this done? Any structural suggestions?

回答1:

Populating a list like doerList is done by another populate parameter.

Changing the populate array to...

const populatesTodoCreator = [
  { child: 'createdBy', root: 'users' },
  { child: 'doerList', root: 'doers' } // populates the whole list of elements
]

worked for me.

Querying only the todos, where the logged in user is participating in and populating the user within the doer object (to get the expected result) is another question.