Appropiate structure for a Firebase database to manage basketball matches

Viewed 81

I'm creating a database in Firebase to manage a local basketball league, for the first part of development I want to work on match management, mainly scoring and foul registration.

The thing with scoring is that a player can score 1, 2 or 3 points and there are different kinds of fouls, like regular fouls and technical fouls, I want to be able to differentiate between those.

Also, a small detail is that a person can play for different teams in different divisions

Here is my idea for the data structure in Firebase:

divisions:{
  division1:{
    name:"first division"
    teams:{
      team1:true
      team2:true
    }
  }
}

teams:{
  team1:{
    name:"Team 1"
    division: division1
    players:{
      player1:true
      player2:true
    }
    matches:{
      match1: true
    }
  }
}

players:{
  player1:{
    name:"Player 1"
    phoneNumber:"555-XXXX"
    address:"123 address"
    teams:{ //A player can play for different teams in different divisions
      team1:true
      team2:true
    }
  }
}


matches:{
  match1:{
    date:10-20-2019
    court:"West Park"
    referee:"John Doe"
    players:{
      player1:{
        /*Should I store points scored and fouls comitted in here and
          the players collection?*/
      }
      player2:{...}
    }
  }

I'm unsure as to where to put the data regarding points and fouls, in the future I plan to use the database to create statistics and such, but in the meantime I just want to have a registry for matches including the players, their points score and fouls.

1 Answers

There is no singular correct way to model data in a NoSQL database. It all depends on the use-cases of the app you want to build. In fact, when using a NoSQL database, it is quite common to adapt your data model as you add new use-cases to your app.

On your current model, I would add an additional top-level data structure to store information about each individual match. Something like this:

matches: {
  matchid1: {
    teams: {
      team1: true,
      team2: true
    },
    events: {
      "-Lasdkjhd31": {
        time: "2m44",
        type: "foul",
        player: "player1id",
        team: "team1"
      }
    }
  }
}

But as said, that is dependent on the use-cases of the app. So my structure above allows the storing of event information for each match, which clearly would be useful if you want to show a timeline of what happened in each match.

Beyond general guidance, it's hard to be concrete. I do recommend that you read/watch these though:

Related