title: @Ngrx/store: how to query models with relationships
body: <p>I an Angular 2 app using Redux (with @ngrx/store), I have modeled the store this way:</p>
  
  <pre><code>{
  
    modelA: {
      ids: [1, 2],
      entities: { 1: { name: "name modelA 1" },
                  2: { name: "name modelA 2" }
                }
    },
  
    modelB: {
      ids: [5, 8],
      entities: { 5: { name: "name modelB 5" },
                  8: { name: "name modelA 8" },
                  9: { name: "name modelA 9" }
                }
    } 
  
  }
  </code></pre>
  
  <p>Basically, I have 2 types of objects: modelA and modelB. This is ok for now.
  But I can't find which is the best way to write a relationship between then, representing something like modelA has many modelB (one-to-many). Can I do something like this?</p>
  
  <pre><code>modelAmodelB: {
    entities: {
      1: [5],
      2: [8, 9]
    }
  }
  </code></pre>
  
  <p>This is in the root of the store, it's not a child from 'modelA'.
  This might work, but how then would I 'query' the modelB from a specific modelA, using @ngrx/store methods? Because if I write a selector function that reads the global state and returns the partial state from modelAmodelB, I don't have access to the rest of the state when I compose my functions. Example:</p>
  
  <pre><code>compose(getModelAEntities(number[]), getModelAModelBRelations(modelA_id: number), getModelAModelBState() );
  </code></pre>
  
  <p>I can query this using Observable.combineLast</p>
  
  <pre><code>Observable
  .combineLatest(
    this.store.select('contentContents'),
    this.store.select('contents'),
    (relations: any, contents: any) =&gt; {
      return relations.entities[1].map( id =&gt; {
        return contents.entities[id]
      })
    }
  ).subscribe( data =&gt; {
    console.log(data);
  })
  </code></pre>
  
  <p>But I don't know if this is right: anytime I change modelA entities object (adding a new one, for example), the subscribe() is called, but the output is the same, because neither modelA entity has changed nor its modelB related objects.</p>
  
  <p>PS: I could do the query this way</p>
  
  <pre><code>export const getModelAModelBs = (id) =&gt; {
    return state =&gt; 
      state.select( (s: any) =&gt; [s.modelAModelB.entities[id], s.modelB.entities])
      .distinctUntilChanged( (prev, next) =&gt; {
  
        const new_m = (next[0] || []).map( id =&gt; {
          return next[1][id];
        });
  
        const old_m = (next[0] || []).map( id =&gt; {
          return prev[1][id];
        });
  
        return prev[0] === next[0] &amp;&amp; (JSON.stringify(new_m) === JSON.stringify(old_m))
      })
      .map( ([ids = [], modelBs]) =&gt; ids.map( id =&gt; modelBs[id]) );
  };
  
  //use the selector
  this.store.let(getModelAModelBs(1)).subscribe( data =&gt; {
    console.log(data)
  })
  </code></pre>
  
  <p>But I don't know if this is the best approach.</p>