1

I'm trying to share data between two accounts (literally children's data with multiple parents). Here's my data structure:

{
  "children" : {
    "1633465800166" : {
      "name" : "Malcolm",
      "parents" : {
        "123456789123456789" : true,
        "987654321987654321" : true
      }
    }
  }
}

I've started setting up my DB rules to allow the first parent to create the node, then add an additional parent, then make that parent read/write supported:

"children":{
  "$childid":{
    // Data doesn't already exist
    // AND newData has a child called parents
    // AND parents has a uid in it that matches the current parent
    ".write": "(
      !data.exists() 
      && newData.hasChild('parents') 
      && newData.child('parents').hasChild(auth.uid)) 
    // OR the data exists, 
    // AND one of the parents is the current parent
    || (
      data.exists()
      && data.child('parents').hasChild(auth.uid)
    )",
    // one of the parents is the current parent
    ".read":"data.child('parents').hasChild(auth.uid) 
    // ???
      && query.orderByChild == 'parents'
      && query.equalTo == auth.uid
    "
  }
}

Every tutorial I've seen about this has a single node, not a group of nodes. How do I share ownership this way?

MorganEngel
  • 73
  • 1
  • 6
  • I finally found this post: [link](https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value) that recommends I store my data twice, once to reference the parents of children, and once to reference the children of parents. Use whichever fits the need best. – MorganEngel Oct 07 '21 at 18:28
  • 1
    Hey Morgan. Good to see you found a relevant answer already. Your current structure makes it east to find the parents for a given child, but not the children for a given parent. Adding an inverted data structure, using that for "list" type calls, and then securing access per individual node is usually the solution. Loading individual is not as slow as you may think, as [Firebase pipelines the requests over a single socket connection](http://stackoverflow.com/q/35931526). I'd also recommend checking: https://stackoverflow.com/q/27207059 and https://stackoverflow.com/q/41527058. – Frank van Puffelen Oct 07 '21 at 18:44

0 Answers0