Search code examples
javascriptfirebaseweb-applicationsfirebase-realtime-databaseupdating

Updating statistics in multiple child nodes


I am Creating a Soccer Fantasy Web App and am currently concerned that Firebase might not be the right back-end service to use. In my Realtime Database Tree, there are 2 nodes:

  1. The Players node- which contains a fire base array of 49 players. Each player is a java script object converted into a JSON object in the JSON array.

  2. The users node- which contains the details of each user after being created using the fireabseAuth method.

In the local server, there is an empty array. What I have done is to get each user to make a 6- player selection which fills up the empty array. Once full, the array will then be saved on the real time database under the users node.

My Issue

My issue is how to get each players' stats updated in the players node such that the updated stats in the players node will be reflected under each user's selections in the users node. Is this possible with firebase at all? I hope it is because I really want to take advantage of the realtime syncing between the database and the client side server.


Solution

  • It's all about architecture.

    Just make it so that when the user selects a player, a reference is stored on their node. Then whenever you're querying a users selection, you can just grab the ids within their selection and grab player from the players node by the ids.

    E.G.

    Users {
        John: {
            Selection: [1,2,3]
        },
        Mark: {
            Selection: [1,2,4]
        } 
    }
    
    Players {
        1: {
            name: 'Messi',
            agility: 90
        },
        2: {
            name: 'Beckham',
            agility: 54
        },
        3: {
            name: 'Rooney',
            agility: 10
        },
        4: {
            name: 'Neymar',
            agility: 84
        }
    }