Home » » Designing Data structure for Firebase

Designing Data structure for Firebase

------Question-------
Warning: My query would be more theoretical (sorry programmers, please bear with me). I am trying to get some idea on how to define the database structure for use in Firebase.
I am exploring the use of Firebase as a backend for a Review app (in Android) I am trying to build.
The app provides product details and review for products of different kinds. So here is an example use case.
  1. The products displayed in the app are of same type (say smartphones). In this use case, defining the database structure is easier. For every phone, I simply need to save the phone specs to Firebase and retrieve them into my app.
Root
 |
 +--Smartphone
     |
     +--Manufacturer Name
     +--Screen Size
     +--Screen Density
     +--Processor
     +--RAM,...
  1. The products displayed in the app are of different type (say smartphones, Car, Book,...). In this use case, defining the database structure becomes complex. I can simply define the data structure like
Root
 |
 +--Product
     |
     +--Manufacturer Name
     +--Screen Size
     +--Screen Density
     +--Processor
     +--RAM
     +--Fuel type (Petrol/Diesel/Electric)
     +--Vehicle Type (Sedan/Hatchback)
     +--Vehicle Price,...
However, the problem with above data structure is, when I am trying to make a product review for a smartphone, the data related to Car will remain blank. Same will be the case for a product review of a Car.
This problem can be solved by using Flattening the data structure. This is where I am confused.
Root
 |
 +--Smartphone
 |   |
 |   +--Manufacturer Name
 |   +--Screen Size
 |   +--Screen Density
 |   +--Processor
 |   +--RAM
 |
 +--Car
     |
     +--Fuel type (Petrol/Diesel/Electric)
     +--Vehicle Type (Sedan/Hatchback)
     +--Vehicle Price,...
However, all product reviews will be displayed in a single activity/fragment. Hence, there will not be different activities/fragments for every product type. Could someone provide me a clear picture of using flattened data structures in my use case?
----Answers----
1.
You can structure your database like this:
products: {
    smartphones: {
        smartphone1: {
            name: "Best Phone",
            ram: "6 GB",
            screen: "5.5 inch"
            reviews: {
                review1: true,
                review2: true
            }
        }
    },
    cars: {
        car1: {
            name: "Lightning"
            reviews: {
                review3: true,
                review4: true,
                review5: true
            }
        }
    }
},
product-review: {
    review1: {
        submittedBy: "Conqueror",
        message: "Best phone at this price",
        timestamp: 1472405901
    },
    review2: {
        submittedBy: "Magic Blaster",
        message: "Pros: RAM, Cons: Everything else."
        timestamp: 1472405901
    },
    review3: {
       submittedBy: "Boss",
       message: "Excellent Car",
       timestamp: 1472405901
    },
    ...
}
Every product(smartphone1, car1 etc..) contains a reviews node, so you can easily load the linked reviews of a particular product.
2.
Here is the flattest database structure that I can think of. For the products node, you can also use the third structure in your question, it will only affect the logic on how to map the item in your app.
products: {
    item1: {
        type: "smartphone",
        manufacturer_name: "some value",
        screen_size: "some value",
        screen_density: "some value",
        processor: "some value",
        ram: "some value"
    },
    item2: {
        type: "car",
        fuel_type: "some value",
        vehicle_type: "some value",
        vehicle_price: "some value"
    }
},
users: {
    user1: {
        name: "some value",
        email: "some value"
    },
    user2: {
        name: "some value",
        email: "some value"
    },
},
products_reviews: {
    item1: {
        user1: ewview1,
        user2: review2
    },
    item2: {
        user2: review3
    }
},
users_reviews: {
    user1: {
        item1: review1
    },
    user2: {
        item1: review2,
        item2: review3
    }
},
reviews: {
    review1: {
        text: "this is my review",
        timestamp: 1472488486000
    },
    review2: {
        text: "this is my review",
        timestamp: 1472488486000
    },
    review3: {
        text: "this is my review",
        timestamp: 1472488486000
    }
}
Now you should be able to retrieve all reviews from each user and also retrieve all reviews for each product.
Comment here if you have questions, hope this helps :)

0 nhận xét:

Post a Comment

Popular Posts

Powered by Blogger.