1

Is it possible to have references to an object elsewhere in the Firebase database? Here's an example:

{
  "articles": {
    "-article1": {
      "title": "Article 1",
      "category": "Other",
      "owner": root.users['-user1']
    },
    "-article2": {
      "title": "Article 2",
      "category": "Other",
      "owner": root.users['-user2'].value
    }
  },
  "users": {
    "-user1": {
      "name": "User 1",
      "articles": {
        "-article1": root.articles['-article1'].value
      }
    },
    "-user2": {
      "name": "User 2",
      "articles": {
        "-article2": root.articles['-article2'].value
      }
    }
  }
}

This way is should make querying a lot easier as I wouldn't need to first make a query to the article itself, and then get the user info from "users" path. However, doing it like above would generate an endless loop of article->owner->article->owner and so on... Unless, Firebase would see this and instead set a repeated value into null.

Anyways, is this even possible?

Thanks in advance!

Nordling Art
  • 857
  • 2
  • 9
  • 19

1 Answers1

4

In Firebase it's generally best practice to use childByAutoId (swift) or push().key (web) to create keys for your nodes. That disassociates the keys from the data it contains and enables you to use 'references'

Here's an example where the articles refer to the user that created it and the user references which articles they created.

Note that it's typical to have a /users node with child user node key being the uid of the user.

  articles
    -YUj9sj9sjaalkd
      title: "Article 1",
      category: "Other",
      owner: "uid_1"
    -YE0989jkamskks
      title: "Article 2",
      category: "Other",
      owner: "uid_2"

  users
    uid_1
      name: "User 1",
      articles
        -YUj9sj9sjaalkd: true
    uid_2
      name: "User 2",
      articles
        -YE0989jkamskks: true
Jay
  • 34,438
  • 18
  • 52
  • 81
  • 1
    This is how I have it right now. This way I first have to get the article, then I would need to get the user. So that requires two queries to be made. The example I brought would only require one query since the user info is already inside the article object as a reference. – Nordling Art Jan 07 '17 at 15:03
  • @NordlingArt Looking at the original question, the Firebase structure presented does *not* contain the user info. It contains a key:value pair of *owner:root.users['-user1']*, which is a 'reference' to the user. i.e. it doesn't contain the user name for example. I was trying to give you a way to reference the data without 'locking' in the key names by separating the key names from their data. – Jay Jan 07 '17 at 15:16
  • @NordlingArt Oh, and yes, part of Firebase is denormalizing data. Flattening data makes it more accessible and flexible at the cost of perhaps an additional query. A lot of it depends on your use case as well - the original question doesn't specify what the ultimate goal is other than *Is it possible to have references to an object elsewhere in Firebase?*. So say that this is an application that has only users and posts. In that case all of the actual posts could be stored in the user node that created it which would eliminate queries entirely. – Jay Jan 07 '17 at 15:28
  • @NordlingArt in many cases looking up the joined data is not as slow as you may think, because the requests are pipelined over the same connection. See my explanation here: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786. – Frank van Puffelen Jan 07 '17 at 16:20
  • @jay - You're right, I was only looking for an alternative structure type. Flattened database works for me too. I will mark your answer as correct :) – Nordling Art Jan 08 '17 at 11:03
  • @frank-van-pullen - Thanks, I read about this before but this reminded me once again that it doesn't matter to make multiple queries. – Nordling Art Jan 08 '17 at 11:05