多对多使用Firebase [英] Many-to-many using Firebase

查看:109
本文介绍了多对多使用Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有两种对象用户和帐户。用户可以拥有多个帐户并与其他用户共享。所以AccountA可能对User1和User2可用。而AccountB只适用于User1。所以User1有两个帐户,User2有一个帐户。



什么是firebase方式来构造这个?

 

我最初以为用户可以存储他们所属的一系列账户。用户:{
1:{
name:'Ted',
accounts:[1,2]
}
2:{
name:'Frank',
accounts:[1]
}
}

accounts:{
1:{
name:Checking
},
2:{
name:Savings
}
}

或者账户有一个用户数组。

  users:{
1:{
name:'Ted'
}
2:{
name:'Frank '
}
}

帐户:{
1:{
name:Checking,
users:[1,2]
},
2:{
名称:储蓄
用户:[1]
}
}

我想知道哪种方式可以让我轻松找到账户对于一个用户,所以当Ted登录时,我可以列出他所属的账户,而不必下拉整个数据库。为了安全,我不希望他的机器上的所有数据。

问题#1
安全规则是否会考虑到这一点?我的意思是我打算设置我的安全规则,使用户只能拥有他们所属的账户。我希望,如果我查询/帐户,我会回来只有那些用户可以访问的帐户...不是?



问题#2
是否有查询项目列表的机制?像/ accounts / [1,2],以便我找回帐户/ accounts / 1和/ accounts / 2?

您可以分享的信息。

解决方案

在深入细节之前,您需要牢记以下几点: / p>


  1. 您通常会根据您的阅读模式构建数据。因此,如果您需要查找用户所在的所有帐户,则需要将该列表存储在某个地方。如果您想查找与某个帐户关联的所有用户,则还需要将该列表存储在某个位置。因此,关于问题#2,Firebase目前没有任何通用查询功能(如/ accounts [1,2]),尽管这可能会在将来发生。

  2. 安全性规则也不能用于查询数据。您可以读取某个位置的所有数据,也可以不读取。因此,关于您的问题#1,安全规则不会自动为您查询/帐户。

  3. 安全规则不允许您搜索某个元素的数组内容,但可以检查某个对象中是否存在某个键。因此,如果用户1和4有权访问某些内容,则可能需要存储{1:true,4:true}而不是[1,4]。

考虑到这一点,我建议存储数据,就像你的第一个例子,但不使用数组:

  users:{
1:{
name:'Ted',
accounts:{
1:true,
2:true


2:{
name:'Frank',
accounts:{
1:true
}



帐户:{
1:{
name:Checking
},
2:{
name:Savings




这样可以让您可以轻松获取特定用户的所有帐户。如果您还需要转向其他方向(所有用户的帐户),则必须复制该信息并将其存储在帐户中。例如:
$ b

  accounts:{
1:{
name:检查,
用户:{
1:true,
2:true
}
},
2:{
name:储蓄,
用户:{
1:true
}
}
}

希望这有助于!


Lets say I have two kinds of objects users and accounts. Users can have many Accounts and share them with other Users. So AccountA might be available to User1 and User2. While AccountB is only available to User1. So User1 has two accounts, and User2 has one Account.

What is the "firebase" way to structure this?

I was initially thinking that the users could each store an array of accounts they belong to.

users: {
  1: {
    name: 'Ted',
    accounts: [1, 2]
  }
  2: {
    name: 'Frank',
    accounts: [1]
  }
}

accounts: {
  1: {
    name: "Checking"
  },
  2: {
    name: "Savings"
  }
}

Or the Account would have an array of users.

users: {
  1: {
    name: 'Ted'
  }
  2: {
    name: 'Frank'
  }
}

accounts: {
  1: {
    name: "Checking",
    users: [1, 2]
  },
  2: {
    name: "Savings"
    users: [1]
  }
}

I'm wondering which way lends itself for me to easily find the accounts for a user so that when Ted logs in I can list the accounts he belong to without having to pull down the entire database. For security I don't want all that data on his machine anyway.

Question #1 Would the security rules take care of this? By that I mean I'm planning to set my security rule such that users can only the accounts they belong to. I'm hoping that if I query for "/accounts" I'll get back only those accounts that the user can access... no?

Question #2 Is there a mechanism for querying for a list of items. Like "/accounts/[1,2]" so that I get back the accounts "/accounts/1" and "/accounts/2"?

Thanks for any information you can share.

解决方案

Before jumping into specifics, there are a few things you'll want to keep in mind:

  1. You'll typically want to structure your data based on your reading patterns. So if you need to look up all accounts a user is in, you'll need to store that list somewhere. And if you want to look up all users associated with an account, you'll need to store that list somewhere as well. So regarding question #2, Firebase doesn't currently have any generic querying ability (like "/accounts[1,2]"), though this will probably come in the future.
  2. Security rules can't be used to query data either. You're either allowed to read all of the data at a location, or none of it. So regarding your question #1, the security rules won't automatically query /accounts for you.
  3. Security rules don't let you search the contents of an array for an element, but they can check for the existence of a key in an object. Therefore if users 1 and 4 have access to something, you'll probably want to store { 1: true, 4: true } rather than [1, 4].

With that in mind, I'd recommend storing the data like your first example, but without using arrays:

users: {
  1: {
    name: 'Ted',
    accounts: {
      1: true,
      2: true
    }
  }
  2: {
    name: 'Frank',
    accounts: {
      1: true
    }
  }
}

accounts: {
  1: {
    name: "Checking"
  },
  2: {
    name: "Savings"
  }
}

This will let you easily get all of the accounts for a particular user. If you need to also be able to go the other direction (all of the users for an account), you'll have to duplicate that information and store it in the accounts as well. E.g.:

accounts: {
  1: {
    name: "Checking",
    users: {
      1: true,
      2: true
    }
  },
  2: {
    name: "Savings",
    users: {
      1: true
    }
  }
}

Hope this helps!

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

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