Firebase Java Admin SDK beginner from SQL

I'm working on a client's Point-of-Sales (POS) software. I was going to use django with MySQL but the client can't pay for a host, so I decided to write it in Java with Firebase. I'm having some trouble thinking in terms of Firebase coming from MySQL.

According to the Firebase documentation, to do relations like I would in SQL, it would have to look like:

inventory : {
    CD001 : {
        genre : {
            "CLASSICAL" : TRUE
    }
}
genre : {
    CLASSICAL : {
        Name : "CLASSICAL"
        inventory : {
            CD001 : TRUE
        }
    }
}

Whereas in SQL I would just put the genre primary key as a foreign key in inventory. Is there a better way to do this in Firebase? It seems for every product that has the genre CLASSICAL I would have to make two updateChildAsync(). Also, any changes (like removing a genre from inventory) I would also have to loop through two DatabaseReference.

If I were to use push to get the generated primary key it would be even worse because I would have to loop through each child just to get the genre name.

I know this may not be the optimal way to make a POS, but given the constraints of the project and that I like learning new stuff, I'm going to stick with it.

Upvotes: 0

Views: 42

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598775

In NoSQL you will often have the same values in multiple places, to allow the specific use-cases of your app. This is normal, but may take some getting used to if you're coming from a background in relational databases.

To get to grips with Firebase's NoSQL model, I recommend reading NoSQL data modeling, watching Firebase for SQL developers (for Firebase Realtime Database) and Getting to know Clouf Firestore.

For a good example of how to model many-to-many relationships, see Many to Many relationship in Firebase. Your example looks more like a categorization problem, in which case I also recommend reading Firebase query if child of child contains a value.

Upvotes: 1

Related Questions