John Doe
John Doe

Reputation: 21

How can I make relational database in firebase?

I'm working on very big scale projects. Finally, I've chosen Android and Firebase due to user friendliness and simplicity. I'm going to have multiple children like following:

products
 1
 name: 
"p1"
 2
 name: 
"p2"


 route
 1: 
"xyz"
 2: 
"abx"

Here is one user case: If user updates route 1 and puts 'john' instead of 'xyz', then name 1 should be updated to 'x1' instead of 'p1'.

One of the way is to do it programatically via android to use methods like 'ondatachange()' But I want to do it on the server side. I don't want to write each time when route 'x' gets updated product 'a' should update in android app.

How can we build relational database on firebase?

Remember there won't be just two tables but there will be 50+ key-value pairs.

Upvotes: 1

Views: 1169

Answers (2)

Jen Person
Jen Person

Reputation: 7566

I agree that you won't want to do all that work client-side! Cloud Functions for Firebase are a good option. You can trigger a function on a write to a path in the database, and then make the change to the other related paths. Check out these resources to find out more:

Getting Started with Cloud Functions for Firebase

GitHub samples

Cloud Functions for Firebase documentation

Writing a Database Trigger

Writing a Cloud Storage Trigger: Part 1

Writing a Cloud Storage Trigger: Part 2

Upvotes: 2

Viktor Peovski
Viktor Peovski

Reputation: 26

You can use firebase admin sdk or firebase functions to listen on changes. The code will look something like this :

exports.routeUpdated = functions.database.ref("route/{id}").onWrite(event => {
    // read the event value/s you need
    const id = // id of the product you want to update
    let updates = {};
    //set updates like updates['/name'] = ...
    admin.database().ref('products/' + id).update(updates).then(() => {
        //...
    }); });

Upvotes: 0

Related Questions