我如何在Firebase中实现这种多对多的关系? [英] How can I achieve this Many-to-many relationship in Firebase?

查看:102
本文介绍了我如何在Firebase中实现这种多对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Firebase不支持节点之间的连接(例如表之间的SQL Server),但这正是我需要完成的。这里是我的情况:

我在Firebase中有一个交易节点,就像这样(包括每个交易的类别名称):
$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' 2.35类别:外出就餐
}
-JruadR11b4a_aTVmZFi:
{
收款人:沃尔玛,金额:78.12,类别:
}
-Jruazf35b9a_AgvNWCq:
{
payee:CapitalOne,金额:150.00,类别:债务
}
-JryJF2c33ijbjbBc24p:
{
payee:FootLocker,金额:107.54,类别:个人爆炸
}
-Jrz0T-aL61Vuw4SOqRb:
{
payee:Starbucks,amount:2.88,category:Eating Out
}
}

我有一个这样的Categories节点(我在这里包括每个类别下的事务):

pre > catego

-Jruazf35b2a_gAVmZRy:
{
categoryname:饮食外出,
categorytype:费用
}
交易:{
-Jruazf35b9a_AgvNWCq:{
payee:McDonalds,金额:2.35
}






$ / code $ / pre

到目前为止,我的数据是平的。我可以显示类别名称的交易列表(下面的屏幕截图),我可以在每个类别的费用下显示每个类别下的交易清单(截图在这里没有显示)。


<我有的问题是,如果我重命名一个类别的变化只反映了未来的交易。过去的交易显示旧的类别名称。



这是显而易见的,因为我保存数据的方式。所以我的第一个逻辑反应是将类别唯一ID保存在事务节点中,而不是类别名称。然而,这提出了挑战,在我的SQL Server小脑袋,我需要一个JOIN,所以我可以得到的交易列表,还包括每个交易类别的名称。

如何构建我的数据,以便我可以:
$ b $ ol

  • 显示包含类别名称的事务列表就像今天这样)

  • 允许用户重命名一个类别,并显示所有交易(过去和将来)反映的变化

  • 显示一个列表每个类别下的交易(我认为目前的方法仍然有效) 加入来自两个列表的数据本质上是一个缓慢的操作,特别是在NoSQL数据库上。



    我建议保留categoryName并添加一个categoryId。这样,你可以显示你的当前屏幕只读一个,但也链接到类别。有关如何处理更新每个事务中的categoryName,请参阅 How在Firebase中编写非规范化数据 https:// medium。 com / @ collardeau / es6-promises-with-firebase-76606f36c80c

    或者:类别列表可能相对较小。所以你也可以预先加载它,并在迭代事务时执行客户端查找。

    I know Firebase does not support JOINs between nodes (like SQL Server does between tables), but that is exactly what I need to accomplish. Here's my situation:

    I have a transactions node in Firebase like this (where I am including the category name for each transaction):

    "transactions": 
    {
        "-Jruazf35b9a_gAVmZBe": 
        {
        payee: "McDonalds", amount: "2.35", category: "Eating Out"
        }
        "-JruadR11b4a_aTVmZFi": 
        {
        payee: "Walmart", amount: "78.12", category: "Household"
        }
        "-Jruazf35b9a_AgvNWCq": 
        {
        payee: "CapitalOne", amount: "150.00", category: "Debt"
        }
        "-JryJF2c33ijbjbBc24p": 
        {
        payee: "FootLocker", amount: "107.54", category: "Personal Blow"
        }
        "-Jrz0T-aL61Vuw4SOqRb": 
        {
        payee: "Starbucks", amount: "2.88", category: "Eating Out"
        }
    }
    

    And I have a Categories node like this (where I am including the transactions under each category):

    "categories": 
    {
        "-Jruazf35b2a_gAVmZRy": 
        {
            categoryname: "Eating Out", 
            categorytype: "Expense"
        }
            "transactions": {
                "-Jruazf35b9a_AgvNWCq": {
                    payee: "McDonalds", amount: "2.35"
                   }
                   .
                   .
                   .
            }
        }
    }
    

    So far so good. My data is flat. I'm able to show the list of transactions with the category name (screenshot below) and I can show the list of transactions under each category in the expenses per category section (screenshot not shown here).

    The problem I have is that if I rename a category the change is only reflected for future transactions. Past transactions show the old category name.

    This is obvious due to the way I'm saving the data. So my first logical reaction was to save the category unique ID in the transactions node instead of the category name. However, that presents the challenge where, in my SQL Server little brain, I would need a JOIN so I can get the list of transactions and also include the name of the category for each transaction.

    How can I structure my data so that I can:

    1. show a list of transactions including the name of the category (as it does today)
    2. allow a user to rename a category and show the change reflected for ALL transactions (past and future)
    3. show a list of transactions under each category (I think the current approach would still be valid)

    解决方案

    Joining data from two lists is inherently a slow operation, especially on NoSQL databases.

    I would recommend keeping the categoryName and adding a categoryId. That way you can show your current screen with a single read, but also link to the category. For how to deal with updating the categoryName in each transaction, see How to write denormalized data in Firebase and https://medium.com/@collardeau/es6-promises-with-firebase-76606f36c80c.

    Alternatively: the list of categories is likely to be relatively small. So you could also pre-load it and perform a client-side lookup while you're iterating the transactions.

    这篇关于我如何在Firebase中实现这种多对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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