jsondatabasedata-structuresfirebasembaas

How to pass a MySQL database for structured data in JSON Firebase


I would like some help as the data structure in Firebase. I've read the API guide and some blog articles like these:

  1. Queries, Part 1: Common SQL Queries Converted for Firebase
  2. Firebase: Now with more querying!
  3. Denormalizing Your Data is Normal

I'm leaving SQL (MySQL and SQLite specifically) and trying to migrate to Firebas. I would like to know how to structure these tables and relationships for a structured JSON in Firebase. Relationship 1-1; 1 - N; N - N; I'm sending a picture of an example database containing some relationships, could you give me this support would look like this in the database you? Remembering that I need to consult with the data about the same as would query in SQL.

This is my Database: Please, ignore "upgrade_table"


Solution

  • I'll take a shot at this...

    First and foremost, Firebase is not a relational database so it should not be thought of in those terms. However, you can craft 'relationships' between data that (for an end user) feels 'relational'

    Let's use a Departments and Employees-like example. A department can have many employees and those employees belong to that department.

    departments
      dept_00
        dept_name: "Bridge"
      dept_01
        dept_name: "Engineering"
      dept_02
        dept_name: "Medical"
    
    crew
     crew_00
      crew_name: "Kirk"
      in_dept: "dept_00"
     crew_01
      crew_name: "Scotty"
      in_dept: "dept_01"
     crew_02
      crew_name: "Bones"
      in_dept: "dept_02"
     crew_04
      crew_name: "Spock"
      in_dept: "dept_00"
    

    With this structure, a query can be performed for crew that are bridge members: here's an ObjC snippit

    Firebase *ref = [myRootRef childByAppendingPath:@"crew"];
    FQuery *q1 = [ref queryOrderedByChild:@"in_dept"];
    FQuery *q2 = [q1 queryEqualToValue:@"dept_00"];
    
    [q2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
      NSLog(@"%@", snapshot.value);
    }];
    

    This will return the crew_00 (Kirk) and the crew_04 (Spock) nodes.

    Note the disassociation between the data (Bridge, Engineering) and the node name (dept_00, dept 01). This allows the data to be modified without breaking 'links' between them. So I could rename 'Engineering' to 'Scotty's Hideaway' and everything will continue to work. Those 'random' node names would be generated by a push() in firebase or ChildByAutoId (objc)

    Likewise, if we know the crew member name (Bones) we could query for that specific node to return that he belongs to dept_02 (Medical).

    You could expand beyond this in cases where say, Spock and Bones belongs to several different departments, say Bridge and Engineering, by adding another node to track which departments the crew member belongs to:

    belongs_to
      crew_02
       dept_00: true
       dept_02: true
      crew_04
       dept_00: true
       dept_01: true
    

    Here, crew_02 (Bones) is now in the Medical and Bridge departments (much to Spocks dismay) and crew_04 (Spock) is now in the Bridge and Engineering Department. With this added belong_to node, the in_dept child nodes could be removed from the crew node.

    That should cover one to one, one to many and many to many