user2561505
user2561505

Reputation:

Firebase Database structure review

I have design my firebase database like below. Need suggestion on it. And also guide me which is best db for ionic 2... I am completely new to firebase... Thanks in advance..

I have months, Categories, Actual budget amount and spent amount

[
    {
        "months": {
            "month_1":"january",
            "month_2":"Februrary",
            "month_3":"March",
            "month_4":"April"
        },
        "acutal": {
            "acutal_amount_1":"2000",
            "acutal_amount_2":"4000",
            "acutal_amount_3":"4000"
        }
        "spent": {
            "spent_amount_1":"1000",
            "spent_amount_2":"2000",
            "spent_amount_3":"3000"
        }
        "categories": {
            "category_1": {
                "categoryname":"Food",
                "amounts": {
                    "acutal_amount_2":true,
                    "spent_amount_3":true
                },
                "months": {
                    "month_1":true,
                    "month_3":true,
                    "month_4":true
                }
            }
        }
    }
]

Upvotes: 2

Views: 261

Answers (2)

Gabriel Barreto
Gabriel Barreto

Reputation: 6421

So one thing you need to know about Firebase is that you can (and must) always save everything in different nodes

So it's a bad way to have all your data in one user node like this:

userID
|_ months
   |_ feb_2017
      |_ expense_1
         |_ expense data...

Because when retrieving user data, for example, you'll retrieve everything under his node and after a few months using it can be heavy for a user to load everything once.

So here's an idea of how i think it's the best way for you to do your Firebase structure.

The user node: Users |_ user_uid |_ name: Jon |_ document: 123456789 |_ another data...

The user node needs to have only user data and nothing more than that, all you'll need from a user in your app is his UID. His UID, name and everything else can be stored localy so you don't need to fetch it on your database everytime.

The user_months node:

User_months
|_user_uid
   |_ month_uid
      |_ amount: 2000
      |_ spent: 1800
      |_ month: sep_2017
      |_ other important things in general for the month expenses

The user_months will group all the months the user had budget to spend and will hold all other important data of the month, like amount money he had, how much he spent, the month in question and anything else you need.

When retrieving data to populate a list of months you can have custom pipes to separate and return the full month name to use in a title. You'll use for the month_uid and the expenses_uid (wich i'll show next) the uid returned by the push() method in Firebase. And you can also retrieve a single month by querying it like firebase.database().ref('User_months/' + user.id).orderByChild('month').equalTo('sep_2017')...

The expenses_month node:

Expenses_month
|_ user_uid
   |_ month_uid (same from the user month)
      |_ expense_uid (a new push())
         |_ category: 'food'
         |_ value: 34.55
         |_ date: 2017/09/27 - 11:30AM
         |_ description: ''

So when pushing a new month you'll get the key on push() callback and use it to push every expenses on your month.

If your categories node is something like "how much a user or company has spent on food" i would sugest you not having it, but querying for child whose is 'food' and them adding the values in a total.

Since i don't know exactly how your application will work that's the best i can do, But in general i think this will work great with your application.

Hope this helps.

Upvotes: 1

Rob
Rob

Reputation: 2323

The key to dealing with firebase is understanding what info you need to keep in one object in order to find more info about it in another tree.

Though you can do something like this:

person:{
    name: "rob",
    gender: "male",
    children: {
       person: {
         name:"maria",
         gender:"female"
       },
       person: {
         name:"maria",
         gender:"female"
       }
   }
}

it is posible not the best option. You should keep your tree like:

people: {

   person:{
      name: "rob",
      gender: "male",
      children: {
         name:"maria",
         name:"dasy"
      }
   },
   person: {
      name:"maria",
      gender:"female"
   },
   person: {
      name:"daisy",
      gender:"female"
   }
}

and when you want to know the child's gender, you go ahead and filter by it's name.

I think you should read this:

https://firebase.google.com/docs/database/web/structure-data

They did a great job in Firebase's documentation!

Upvotes: 0

Related Questions