Pete
Pete

Reputation: 19

How to access the same Firebase data by two different indexes?

I'm attempting to build an interactive matrix of a team sports schedule where the rows are each team, and the columns are each week. This is a survivor pool where users are only allowed to choose one winner a week and each team only once per year. Therefor, when a user clicks on a given game, the associated row and column should be set to inactive. I'm new to noSQL so I'm not sure how to interact with the games by both team and week.

Sine I'm using Firebase as my backend, I started with a .json file structure with an $firebaseArray of teams each with a nested schedule like so:

{
"teams": {
"California": {
  "slug": "california",
  "chosen": false,
  "schedule": [
    {
      "week": 1,
      "opponent": "Grambling St",
      "selected": false,
      "available": true
    },
    {
      "week": 2,
      "opponent": "San Diego St",
      "selected": false,
      "available": true
    },
    {
      "week": 3,
      "opponent": "at Texas",
      "selected": false,
      "available": true
    },
    {
      "week": 4,
      "opponent": "at Washington",
      "selected": false,
      "available": true
    },
    {
      "week": 5,
      "opponent": "Washington St",
      "selected": false,
      "available": true
    },
    {
      "week": 6,
      "opponent": "at Utah",
      "selected": false,
      "available": true
    },
    {
      "week": 7,
      "opponent": "Bye",
      "selected": false,
      "available": true
    },
    {
      "week": 8,
      "opponent": "at UCLA",
      "selected": false,
      "available": true
    },
    {
      "week": 9,
      "opponent": "USC",
      "selected": false,
      "available": true
    },
    {
      "week": 10,
      "opponent": "at Oregon",
      "selected": false,
      "available": true
    },
    {
      "week": 11,
      "opponent": "Oregon St",
      "selected": false,
      "available": true
    },
    {
      "week": 12,
      "opponent": "at Stanford",
      "selected": false,
      "available": true
    },
    {
      "week": 13,
      "opponent": "Arizona St",
      "selected": false,
      "available": true
    }
  ]
},

"Oregon": {
  "slug": "oregon",
  "chosen": false,
  "schedule": [
    {
      "week": 1,
      "opponent": "EWU",
      "selected": false,
      "available": true
    },
    {
      "week": 2,
      "opponent": "at Michigan St",
      "selected": false,
      "available": true
    },
    {
      "week": 3,
      "opponent": "Georgia St",
      "selected": false,
      "available": true
    },
    {
      "week": 4,
      "opponent": "Utah",
      "selected": false,
      "available": true
    },
    {
      "week": 5,
      "opponent": "at Colorado",
      "selected": false,
      "available": true
    },
    {
      "week": 6,
      "opponent": "Washington St",
      "selected": false,
      "available": true
    },
    {
      "week": 7,
      "opponent": "at Washington",
      "selected": false,
      "available": true
    },
    {
      "week": 8,
      "opponent": "Bye",
      "selected": false,
      "available": true
    },
    {
      "week": 9,
      "opponent": "at Arizona St",
      "selected": false,
      "available": true
    },
    {
      "week": 10,
      "opponent": "California",
      "selected": false,
      "available": true
    },
    {
      "week": 11,
      "opponent": "at Stanford",
      "selected": false,
      "available": true
    },
    {
      "week": 12,
      "opponent": "USC",
      "selected": false,
      "available": true
    },
    {
      "week": 13,
      "opponent": "Oregon St",
      "selected": false,
      "available": true
    }
  ]
},

I was easily able to build the table via an ng-repeat="team in teams" at the row level and another game in team.schedule at the td level. Setting the associated team.chosen to true marks the row inactive but I'm not sure how to do the same by week (column).

========

I decided to give it another try with a different data structure, this time breaking the games out into its own table separate from teams like so:

{
"teams": {
"California": {
  "chosen": false,
  "slug": "california"
},

"Oregon": {
  "chosen": false,
  "slug": "oregon"
},

"Oregon St": {
  "chosen": false,
  "slug": "oregon-st"
},

"Stanford": {
  "chosen": false,
  "slug": "stanford"
},

"Washington": {
  "chosen": false,
  "slug": "washington"
},

"Washington St": {
  "chosen": false,
  "slug": "washington-st"
}
},

"games": {
"one": {
  "Stanford": "at Northwestern",
  "Washington": "at Boise St",
  "California": "Grambling St",
  "Oregon": "EWU",
  "Oregon St": "Weber St",
  "Washington St": "Portland St"
},
"two": {
  "Stanford": "UCF",
  "Washington": "Sacramento St",
  "California": "San Diego St",
  "Oregon": "at Michigan St",
  "Oregon St": "at Michigan",
  "Washington": "at Rutgers"
},
"three": {
  "Stanford": "at USC",
  "Washington": "Utah St",
  "California": "at Texas",
  "Oregon": "Georgia St",
  "Oregon St": "San Jose St",
  "Washington": "Wyoming"
}
}
}

This hasn't worked out any better as I'm not able to reach the value of the game object inside an ng-repeat of weeks.

I suspect there's something I'm missing structuring this data within Firebase. Any help is much appreciated.

Upvotes: 0

Views: 83

Answers (1)

Jay
Jay

Reputation: 35659

The random part of this is the cell in the matrix the user clicks on (what they picked) so how about keeping track of each pick. The children would the user_id of who picked it, the week # (column) and team_id (row).

The pick_id_x would be a randomly generated Firebase node name (childByAutoId)

picks_node:
    pick_id_0:
      user: user_id
      week: week #
      team: team_id
    pick_id_1:
      user: user_id
      week: week #
      team: team_id

With this structure you could query for week, team or user and anything that appears in the picks node would have been, well, picked, so it couldn't be picked again (you would resolve that in the UI). So you populate the matrix rows and columns from source data and then load in this list of picks and 'block out' each week and team.

You could use this to answer questions 'I wonder which user picked which team on week 3' or 'which team is liked by the most users'.

(I used team_id, but it could be game: game_id_x as well if you want to keep more game data)

games_node:
  game_id_0:
    t1: home_team
    t2: visiting_team
  game_id_1:
    t1: home_team
    t2: visiting_team

Notice that I disassociated the data from it's key (node name, reference, whatever you want to call it).

In general for Firebase: the flatter the better and don't hard code absolute data.

Upvotes: 1

Related Questions