Pushing to Data Store Polymorphic Models


I have 3 models: Node, Document and Folder. Node model is polymorphic, it can be either Document or Folder. Now, server side provides an endpoint /api/nodes/<node_id>/ which returns an array of nodes i.e. an array of objects, each being of type either “folders” or “documents”.

I push incoming data into the store as follows:

    url = this.buildURL('nodes', node_id);

    nodes = await fetch(url, {
      method: 'GET',
      headers: this.headers
    }).then(response => response.json());

    node_ids = nodes.data.map(node => {
      let normalized_node = this.store.normalize('node', node);

      return node.id;

The problem with code above is that later, the only way to search for data is to peek them by individual data types:


 this.store.peekAll('folder') // correctly returns nodes which are of type: 'folders'


this.store.peekAll('document') // correctly returns nodes of type 'documents'

The problem is that later code is supposed to treat them as one array of nodes. Is there a way to combine results of two peekAll call ? How can I push to store - and then retrieve polymorphic models ?

I don’t think the store will let you peekAll on the base model type to get all derivative models which is pretty annoying, I feel like that should work. That said combining them is fairly trivial… you could do something like:

let allNodes = [...this.store.peekAll('document'), ...this.store.peekAll('folder')];

This is actually is what I ended up doing (joining results of those peekAll methods). Thank you for your reply!

1 Like