优化Firebase数据库设计 [英] Optimize Firebase database design

查看:179
本文介绍了优化Firebase数据库设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计我的应用程序的数据库时遇到了麻烦。在应用程序中,用户可以创建作业,然后使用 p>

正如你所看到的,有用户,然后是工人。将新作业推送到serviceUsers下的用户唯一标识(UID)后,我使用geoFire查找附近的workerUsers。然后我把工作推到工人用户的UID中。

现在我的问题是:

我基本上是创建这些作业的副本。一次给创建它的人(在serviceUsers下),一次给附近的每个workerUsers。
这是低效的吗?我应该宁愿将某种指针而不是整个工作对象传递给附近的用户?



这里是更重要的问题:如果设计是好的,当作业的创建者删除它时,我将如何继续?然后,我需要在workerUsers中找到每个作业,并使用作业UID删除作业。 Firebase是否支持查询?



非常感谢您的提前支付!

解决方案


我基本上是创建这些作业的副本。一次给
创建的人(在serviceUsers下),一次给附近的每个workerUsers。
这是低效的吗?我应该宁愿传递某种指针,而不是将整个作业对象的
传递给附近的用户?

每个工作都应该有一个UUID可以作为一个指针(我宁愿把它称为一个关键)。那么每个用户都应该包括一个工作UUID,而不是一个完整的副本,所以你可以参考它。我不会完全复制您的使用案例,但您应该明白。

  {
users:{
exampleUserId:{
jobs:['exampleUUID']
}
},
作业:{
exampleUUID:{
name:'awesome





lock $ $









$如果设计没问题的话,当作品的
创建者删除它时,我将如何继续?然后,我需要在
workerUsers中找到每个作业,并使用作业UID删除作业。 Firebase
是否支持查询?

它支持它,但是您应该从上面执行我的建议来做到这一点以一种理智的方式。在此之后,您可以创建一个云端功能,其工作应该是这样的:当作业与给定的UUID被删除,然后通过每个用户,并删除它的引用,如果它存在

  exports.checkReferences =如果(!event.data.val())在这里检查信息

('/ jobs / {uuid}')。onWrite(event => ){
//作业已被删除!获取其uuid并遍历用户并从其中移除uuid
}
});


I am having trouble designing the database of my app. In the app users are allowed to create jobs and then using GeoFire I find people nearby.

This is my design for the jobs so far:

As you can see there are the users and then the workers. After pushing the new job to the users Unique ID (UID) under serviceUsers, I then use geoFire to find the workerUsers that are nearby. I then push the jobs into the UID's of the workerUsers.

Now here are my questions:

I am basically creating copies of these jobs. Once for the person who created it (under serviceUsers) and once for every nearby workerUsers. Is this inefficient? Should I rather pass some kind of pointer instead of the whole job object to the nearby users?

And here for the more important question: If the design is fine as it is, how would I go on about when the creator of the job deletes it? I would then need to find each job in workerUsers and delete the job with the jobs UID. Does Firebase support queries for this?

Thank you very much in advance!

解决方案

I am basically creating copies of these jobs. Once for the person who created it (under serviceUsers) and once for every nearby workerUsers. Is this inefficient? Should I rather pass some kind of pointer instead of the whole job object to the nearby users?

Every job should have a UUID which can act as a "pointer" (I'd rather call it a key). Then every user should include a job UUID, not a whole copy, so you can refer to it. I won't completely replicate your use case, but you should get an idea.

{
  users: {
   exampleUserId: {
      jobs: ['exampleUUID']
    }
  },
  jobs: {
   exampleUUID: {
     name: 'awesome job'
   }
  }
}

If the design is fine as it is, how would I go on about when the creator of the job deletes it? I would then need to find each job in workerUsers and delete the job with the jobs UID. Does Firebase support queries for this?

It does support it, but you should implement my suggestion from above to do it in a sane way. After this, you can create a cloud function whose job should sound like this: "When a job with given UUID is removed, then go through every user and remove a reference to it if it exists"

exports.checkReferences = functions.database.ref('/jobs/{uuid}').onWrite(event => {
  // check information here

  if (!event.data.val()) {
    // job was removed! get its uuid and iterate through users and remove the uuid from them
  }
});

这篇关于优化Firebase数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆