Filip
Filip

Reputation: 1097

structure of data in Firebase

I want to start using Firebase.

I will start with a simple database for comics.

Which structure is the best?

Remark Structure 1: how can I get a list of series?

Remark Structure 2: too close to sql databases?

STRUCTURE 1:

{
  "comics" : {
    "1" : {
      "serie" : "Batman",
      "nr" : "1",
      "title" : "Title of issue 1"
    },
    "2" : {
      "serie" : "Batman",
      "nr" : "2",
      "title" : "Title of issue 2"
    },
    "3" : {
      "serie" : "Spiderman",
      "nr" : "1",
      "title" : "Title of issue 1"
    }
  }
}

STRUCTURE 2:

{
  "issues" : {
    "1" : {
      "serie" : "1",
      "nr" : "1",
      "title" : "Title of issue 1"
    },
    "2" : {
      "serie" : "1",
      "nr" : "2",
      "title" : "Title of issue 2"
    },
    "3" : {
      "serie" : "2",
      "nr" : "1",
      "title" : "Title of issue 1"
    }
  },
  "series" : {
    "1" : {
      "title" : "Batman"
    },
    "2" : {
      "serie" : "Spiderman"
    }
  }
}

Upvotes: 0

Views: 83

Answers (1)

ksav
ksav

Reputation: 20821

  • I would first take a look at this page in the docs.

  • Then as per Jay's advice, allow firebase to generate unique keys for your records.

  • Keep a record of each series, and as a child, you can keep a list of its associated episodes.

  • Keep a record of each episode, and here you can keep more details about it (year, cast, rating, etc). You can even track the id of the series it belongs to if you need to.

Your data structure might look something like this:

{
  "series": {
    "58371074b694fc9c35ec8891": {
      "name": "Batman",
      "episodes": {
        "583710741ffbf07cfccf7ba2": true,
        "58371074539dde71b245d5a1": true,
        "583710744b14fd3a654a6d75": true
      }
    },
    "58371074898bed03d73cd12c": {
      "name": "Spiderman",
      "episodes": {
        "58371074b63304e2617b558e": true
      }
    }
  },
  "episode": {
    "583710741ffbf07cfccf7ba2": {
      "name": "Batman Begins"
    },
    "58371074539dde71b245d5a1": {
      "name": "Batman Returns"
    },
    "583710744b14fd3a654a6d75": {
      "name": "Batman Forever"
    },
    "58371074b63304e2617b558e": {
      "name": "The Amazing Spider-Man"
    }
  }
}

Note: obviously the keys in the above example have not been generated by firebase, but you get the idea of how your data structure might look.

Upvotes: 2

Related Questions