Structuring Relationships in Firebase

孤人 提交于 2019-12-05 07:12:35

The problem with joined data in Firebase is that you optimize for certain read or update use cases at the expense of others. In your sample above, creating or deleting a relationship between services and providers requires two separate updates to each "table". There's really nothing wrong with that, but it's not the only way to go.

For a modestly sized data set, you could have a "join table" that maps services to providers, similar to what might be done in the relational DB world. The data might look something like this:

{
  "services": {
    "hip_replacement": {}
  },
  "providers": {
    "the_blue_hospital": {...},
    "the_red_hospital": {...},
    "the_green_hospital": {...}
  },
  "serviceProviders": {
    "-JqD5JX0RUDTXsu7Ok3R": {
      "provider": "the_blue_hospital",
      "service": "hip_replacement"
  }
    "-JqDoKfyJqPkQlCXDvFM": {
      "provider": "the_green_hospital",
      "service": "hip_replacement"
  }
    "-JbE7Ji_JRz2bHgBdMWQ": {
      "provider": "the_blue_hospital",
      "service": "hip_replacement"
  }
}

There are pros and cons of this approach:

Pro

  • Easy to add mappings in one place
  • Easy to delete mappings in one place
  • Flexible options to reformat the data for display, beyond the context of a single provider or service, such as an index.

Con

  • You have load the whole data set. Firebase doesn't let you filter within a key, clients have to load the whole list, then filter in memory. I suspect this will work fine for hundreds of records, anyways, maybe for low thousands.
  • You have to do some client work to filter the list for display and merge it with the actual service and provider data. Again, if the data set isn't too big, underscore/lodash groupBy() can make short work of this.

You should consider:

  • How much updating and deleting will you do?
  • Is the join information really that simple? Would you need more records (display names, prices, etc.) that make maintenance on the join table more complicated than I suggested?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!