Getting Season Stats with Falcor

58 views Asked by At

What's the best way to approach getting sport season stats with Falcor and Falcor router?

My data source for a season outputs data that looks like this:

{ id: 'recNMJfs4sqiJshna',
   fields: {
     Wins: 23,
     Losses: 51,
     Team: [ 'reckEYqAz3r8pUtUg' ],
     ... 
}

My id's are guids and I've got a teamsById route working that returns the season guid. However to try and prevent a lot of duplicate routes and code I'm trying to create a seasonsIndex route that would look something like this:

seasonIndex[0..10][year] or [seasonIndex[0..10]["2016"]

Where I can select the data that is in the season data source for a specific year. Hoping I can create an output like this:

seasonIndex: {
   teamGuid: {
      2016: {
         Wins: 23,
         Losses: 51,
         ...
      },
      2015: {
         ...
      }
   },
   ...
 },
 teamById: {
    teamGuid: {
        Name: Team Name
    }
 }

I'm have trouble figuring out the routes I would need to build this model response. Because I'm unsure how to get the data out of the different season's data source and associate it to the unique team guids, and still be able reference specific values in the season data like Wins, Losses, or Winning Percentage.

1

There are 1 answers

0
Hugo Wood On

You need to build a route by field you want to expose. For example, for the wins field:

{
    route: "seasonIndex[{keys:teams}][{keys:years}].wins",
    get(pathSet) {
        const results = []
        for (team of pathSet.teams) {
            for (year of pathSet.years) {
                const wins = ... // get the wins for that team and year from the data source
                results.push({
                    path: ["seasonIndex", team, year, "wins"],
                    value: wins
                })
            }
        }
    }
}

The route for wins and the route for losses are probably going to end up similar, so you might be able to collapse them like so:

{
    route: "seasonIndex[{keys:teams}][{keys:years}][{keys:property}]",
    get(pathSet) {
        const results = []
        for (team of pathSet.teams) {
            for (year of pathSet.years) {
                for (property of pathSet.properties) {
                    const value = ... // get the value of the property for that team and year from the data source
                    results.push({
                        path: ["seasonIndex", team, year, property],
                        value
                    })
                }
            }
        }
    }
}