How can I structure data in firebase?

enter image description hereI have data about family, family members, and children.

  1. User can add children.
  2. User can create a family.
  3. User can add as many other users to the family.
  4. All family members can access the children's data of all other family members.

what I decided is something like,

User:
    user_1:
       name: name,
       email: email,
       familyId: family_1
    user_2:
       name : name,
       email: email,
       familyId: family_1

Family:
    family_1:
      familyName: name
      creator: user_1

Children:
    child_1:
      name: name,
      parentId: user_1
    child_2:
      name: name,
      parentId: user_1

using this user can access his own children and the family can access all family members. But how can family members access family children's data?

anyone who can guide.

Upvotes: 1

Views: 63

Answers (1)

DIGI Byte
DIGI Byte

Reputation: 4174

You can do this with rules, but this does require you to know the child's UID since you can't query children if you can't read all nodes in the structure.

{
  "rules": {
    "children": {
      "$uid": {
        ".read": "auth != null && data.child('parentId').val() === auth.uid"
      }
    }
  }
}

To read the child node based on the Family ID, you will need to maintain a list of users who are in the family

Family:
    family_1:
      familyName: name
      creator: user_1
      members: user_1 : true
               user_2 : true

and the corresponding rules:

{
  "rules": {
    "Children": {
      "$child_id": {
        ".read": "auth != null && 
               root.child('Family').child(
                 root.child('User').child(
                   data.child('parentId').val()
                 ).val()
               ).child('members').child(auth.uid).exists()
      }
    }
  }
}

you can also swap out the last .exists() for a .val() allowing you to test against true so you can essentially blacklist members if you desire.

Upvotes: 1

Related Questions