Netflix / falcor

A JavaScript library for efficient data fetching

Home Page:http://netflix.github.io/falcor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best practices to fetch all referenced data

jakub-bogacz opened this issue · comments

Hello,
I'm trying to find best solution for fetching whole referenced data.

We've got following "database" structure:

{
  genresById: {
    1: {
      name: "Crime",
      titles: [
        $ref('titlesById[1]'),
        $ref('titlesById[3]')
      ]
    },
    2: {
      name: "Comedy",
      titles: [
        $ref('titlesById[2]'),
        $ref('titlesById[4]')
      ]
    }
  },
  titlesById: {
    1: {
      name: "Godfather"
    },
    2: {
      name: "La vita è bella"
    },
    3: {
      name: "Pulp Fiction"
    },
    4: {
      name: "Intouchables"
    }
  }
}

Question: What is the best way to fetch all titles for genre with id 1?

The only way I've found is to do overfetch, like this:

model
  .get(["genresById", 1, 'name'],
       ["genresById", 1, 'titles', {to: 100}, ["name"]],
       ["titlesById", {to: 100}, "name"]
      )

For me it seems that it shouldn't work like that, as we are querying for more data than exists in database. Am I right?

It could be great if documentaion will have some examples how to do that properly.

Full working example could be found here: https://github.com/Animagani/falcor-referenced-data

The way I always approach this problem is:

  1. Get into a real-world mindset and assume the database list ("titles", in your case) would eventually grow to have more things than would fit in browser memory, or at least more than would be appropriate to load into a single view.
  2. Build my list view to support some kind of pagination from the start, and decide up-front how many things to display per page. This decision is informed purely by UX factors like available screen space and load time, rather than being a guess of how many items might exist in the DB.
  3. In the list view, if the result is undefined (as is the case in an over-fetch) then simply don't render it or render some kind of blank "tile" or placeholder, depending on the mechanics of the view.

It sounds like you're misunderstanding something. Why are you fetching titlesById directly? The items in genresById[1].titles reference titlesById, and Falcor will follow this reference directly. So if you just want the titles in genre 1, ["genresById", 1, "titles", {from: 0, to: 100}, "name"] is sufficient.