如果我使用这种方法,我的FireBase数据库布局会导致问题吗?

时间:2016-10-24 11:11:32

标签: firebase-realtime-database firebase-security

我有一个看起来像这样的FireBase结构。我想代表世界上所有国家。

在此示例代码中,只有9个国家/地区,只有美国和委内瑞拉有数据来证明我的问题。

我有非规范化并尽可能地平坦化数据。

她会发现用户可以搜索街道地址,例如

US/California/Orange County/Orange/3138 E Maple Ave    

在下面的数据库中,它看起来像这样:

US/ADMINISTRATIVE_AREA_LEVEL_1/    
US/ADMINISTRATIVE_AREA_LEVEL_2    
US/LOCALITY    
US/STREET_ADDRESS    

  ....
  ....
  "AE": {
    "name": "United Arab Emirates"
  },
  "GB": {
    "name": "United Kingdom"
  },
  "US": {
    "name": "United States"
    "ADMINISTRATIVE_AREA_LEVEL_1": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "level1": "California"
      },

    },
    "ADMINISTRATIVE_AREA_LEVEL_2": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "level2": "Orange County"
      },
    },
    "LOCALITY": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "level2": "Orange"
      },
    },
    "STREET_ADDRESS": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "3138 E Maple Ave": {
        }
      }
    },
    "USER_LIST": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "name": "Jhon Doe",
      }
    },
    "CHAT_LIST": {
      "hjg86tghg8hubyhiuhb88ihi": {
        "title": "Wam-Bam-CHAT",
      }
    },
    "chat_members": {
      "hjg86tghg8hubyhiuhb88ihi": {
      }
    },
    "chat_messages": {
      "hjg86tghg8hubyhiuhb88ihi": {
    },
  },
  "UM": {
    "name": "United States Minor Outlying Islands"
  },
  "UY": {
    "name": "Uruguay"
  },
  "UZ": {
    "name": "Uzbekistan"
  },
  "VU": {
    "name": "Vanuate"
  },
  "VE": {
    "name": "Venezuela"
       "ADMINISTRATIVE_AREA_LEVEL_1": {
      "swdkewsjdr34378943489324": {
        "level1": "California"
      },

    },
    "ADMINISTRATIVE_AREA_LEVEL_2": {
      "swdkewsjdr34378943489324": {
        "level2": "Orange County"
      },
    },
    "LOCALITY": {
      "swdkewsjdr34378943489324": {
        "level2": "Orange"
      },
    },
    "STREET_ADDRESS": {
      "swdkewsjdr34378943489324": {
        "3138 E Maple Ave": {
        }
      }
    },
    "USER_LIST": {
      "swdkewsjdr34378943489324": {
        "name": "Jhon Doe",
      }
    },
    "CHAT_LIST": {
      "swdkewsjdr34378943489324": {
        "title": "Wam-Bam-CHAT",
      }
    },
    "chat_members": {
      "swdkewsjdr34378943489324": {
      }
    },
    "chat_messages": {
      "swdkewsjdr34378943489324": {
    },
  },
  "VN": {
    "name": "Viet Nam"
    ....
    ....

当我创建实时数据库规则时喜欢这样:我必须创建240个根点,因为有240个国家正确。 因为所有".read": "$uid === auth.uid"和其他人看起来都是一样的,所以有ADMINISTRATIVE_AREA_LEVEL_1的战利品和一堆重复的json。 如果我把ADMINISTRATIVE_AREA_LEVEL_1作为根,我可能会有一小部分条目,对于STREET_ADDRESS,仅有1.54亿美国人就不会说“世界”。所以我将它们分组,将Country作为关键的根节点。

小样本:

{
  "rules": {
    "SE": {
      "ADMINISTRATIVE_AREA_LEVEL_1": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      }
    },
    "VE": {
      "ADMINISTRATIVE_AREA_LEVEL_1": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      }
    }
  }
}

我的问题是如何才能进一步提高效率,以及我可以期待的瓶颈。有没有办法建立这种结构并集中设置规则而不在所有位置明确地写它

更新 可能我错了,但无论如何,在@FrankvanPuffelen回答后我试试这个:必须测试运行这个但是在我上面的代码中这对所有240个国家都有效..

{
  "rules": {
    "$country": {
      "ADMINISTRATIVE_AREA_LEVEL_1": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      },
      "ADMINISTRATIVE_AREA_LEVEL_2": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      },
      "LOCALITY": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      },
....and more
    }
  }
}

更新 是的,它适用于一个小小的推文

{
  "rules": {
    "$hubaBuba": {
      "ADMINISTRATIVE_AREA_LEVEL_1": {
         ".read": "auth != null",
         ".write": "auth != null",
      },
      "ADMINISTRATIVE_AREA_LEVEL_2": {
           ".read": "auth != null",
           ".write": "auth != null",
      },
      "LOCALITY": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      },
....and more
    }
  }

1 个答案:

答案 0 :(得分:0)

如果每个国家/地区的规则相同,您可以为该国家/地区使用通配符:

{
  "rules": {
    "$country": {
      "ADMINISTRATIVE_AREA_LEVEL_1": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      }
    }
  }
}