wobsoriano
wobsoriano

Reputation: 13434

Firebase Database Normalization

So I made a simple app, a Tricycle Patrol app which you can report reckless tricycle drivers (no 1 problem here in our city) by logging in and filling up forms. The report form contains:

- created_at
- description
- lat
- lng
- plateNumber

And so far, this is the structure of my json:

enter image description here

Is there a cleaner way to do this? So far it works though for example I want to get all reports by plateNumber I think it would be tricky.

Any suggestions would be much appreciated.

PS: I know it's not a relational database though I'm open for suggestions

Upvotes: 3

Views: 1047

Answers (2)

Now as you say you need normalize database ... When I have this problem, I stop writing code and start thinking about my application / web ...

  • I want to show a list of all reports, another list sorted by plate and ordered by others?
  • Or in all lists of reports are sorted by plate?

If your answer is the first, then you need to generate nodes as indexes, like these:

reports{
    report_id{
        created_at:" "
        description:" "
        lat:" "
        lang:" "
        location:" "
        plate_number:" "
    }
}
reports_idx_plate{
    plate_number1{
        report_id1:"true"
        report_id2:"true"
        report_id3:"true"
        report_id4:"true"
    }
}

If your answer is the second one, then your best option is this one:

reports{
    plate_number{
         report_id{
            created_at:" "
            description:" "
            lat:" "
            lang:" "
            location:" "
            plate_number:" "
        }
    }
}

In resume if you need to get list from child node, you need to normalize the nodes and generate the nodes as index. Let me know if I have helped you and good programming!

Upvotes: 1

ughai
ughai

Reputation: 9880

I would suggest keeping reports separate and don't group them under users. just keep a reports id inside users record with created date as a value if you need to sort reports based on created date. Something like this

reports
    9F0A756DF0B849CCAB7FAA0AC089475E
       - created_at:14804893896
       ...
    1C060A2CA16D42CF9DF53FD80A9C6ECA
    ADC4582B3C5746A487178887A495E005

users
    F4C8F5642EC44727B929E3E408321122
        - email: ....
        - reports
            9F0A756DF0B849CCAB7FAA0AC089475E:14804893896
            1C060A2CA16D42CF9DF53FD80A9C6ECA:14865486878
    C9E0FAF081AC4447B0C194811314DF0F

Upvotes: 1

Related Questions