存储关系“类型”或“类别” Firebase中的数据,无需更新多个地点 [英] Storing Relational "Type" or "Category" Data in Firebase Without the Need to Update Multiple Locations

查看:133
本文介绍了存储关系“类型”或“类别” Firebase中的数据,无需更新多个地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图很好地理解如何在Firebase中构建关系数据。我明白,数据模型是非常不同的,但我有关系数据库的悠久历史。今天,我试图解决一个简单的问题。

在MySQL中,我有一个Node表和一个NodeType表:
$ b Node Table
$ b


  • 节点ID

  • 节点名称

  • 类型ID



节点类型表

    类型名称
    $ b
  • 类型名称

  • 类型名称

  • b

    我的问题是我想要为每个节点类型存储一定数量的元数据,但是我只想将它们存储在一个地方。我希望能够更改节点类型A 的元数据,并使其自动应用于属于该类型的所有节点。



    在Firebase中构建这个结构的最佳方式是什么?

    解决方案

    本博客文章对如何重构Firebase中的关系数据提供了一些建议: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html ,我建议阅读它以更好地理解问题。



    对于您的特定用例,您应该能够将节点和节点类型存储在节点元数据中单独的顶级密钥和参考节点类型下。例如:

    /节点
    /
    名称:
    类型:
    /类型
    /
    名称:
    元数据:

    您可以使用 push 方法为节点自动生成ID 。对于节点类型,您可以使用相同的推送方法,或为这些类型提供手动标识。


    $ b

      var ref = new Firebase(https://< my-firebase> .firebaseio.com /); (type-a)。set({name:Type A,元数据:关于Type A}); 
    ref.child(types)。 $ {$ b ref.child(nodes)。push({name:Some node,type:type-a});

    您可以使用 .once('值')


    $ b

      ref.child(types / type (value,function(snapshot)){
    console.log(类型A的元数据是:+ snapshot.val()。metadata);
    });


    I am trying to get a good grasp of how to structure relational data in Firebase. I understand that the data model is very different, but I have a long history with relational databases. Today, I am trying to solve a simple problem.

    In MySQL, I have a Node table and a NodeType table:

    Node Table

    • Node Id
    • Node Name
    • Type Id

    Node Type Table

    • Type Id
    • Type Name
    • Type Metadata

    My problem is that I want to store a certain set of metadata for each Node Type, but I only want to store that in one place. I want to be able to change the metadata for Node Type A, and have it automatically apply to all Nodes that belong to that type.

    What is the best way to structure this in Firebase?

    解决方案

    This blog post has some ideas on how to restructure relational data in Firebase: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html, I recommend reading it to better understand the problem.

    For your specific use-case, you should simply be able to store the nodes and node types under separate top-level keys and reference node types in the node metadata. For example:

    /nodes / name: type: /types / name: metadata:

    You can use the push method to automatically generate IDs for nodes. For node types, you can use the same push method, or provide manual IDs for the types.

    var ref = new Firebase("https://<my-firebase>.firebaseio.com/");
    ref.child("types").child("type-a").set({name: "Type A", metadata: "About Type A"});
    ref.child("nodes").push({name: "Some node", type: "type-a"});
    

    You can retrieve type information for a specific type by using .once('value'):

    ref.child("types/type-a").once("value", function(snapshot) {
      console.log("Metadata for Type A is: " + snapshot.val().metadata);
    });
    

    这篇关于存储关系“类型”或“类别” Firebase中的数据,无需更新多个地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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