简体   繁体   English

Dynamodb Node js (Dynamoose) 中的一对多关系

[英]One to many relation in Dynamodb Node js (Dynamoose)

I am using Dynamodb with nodejs for my reservation system.我将 Dynamodb 与 nodejs 一起用于我的预订系统。 And Dynamoose as ORM.Dynamoose为 ORM。 I have two tables ie Table and Reservation .我有两张桌子,即TableReservation To create relation between them, I have added tableId attribute in Reservation which is of type Model type (of type Table type), as mentioned in the dyanmoose docs .为了在它们之间创建关系,我在Reservation中添加了tableId属性,该属性的类型为 Model 类型(表类型),如 dyanmoose文档中所述。 Using the document.populate I am able to get the Table data through the tableId attribute from Reservation table.使用document.populate我可以通过来自Reservation表的tableId属性获取数据。 But how can I retrieve all Reservation for a Table ?但是我怎样才能检索一个的所有预订 (Reservation and Table has one to many relation)? (Reservation 和 Table 有一对多的关系)?

These are my Model: Table Model:这些是我的 Model:表 Model:

    const tableSchema = new Schema ({
        tableId: {
            type: String,
            required: true,
            unique: true,
            hashKey: true
        },
        name: {
             type: String,
             default: null
        },
    });

*Reservation Model:*

    const reservationSchema = new Schema ({
        id: {
            type: Number,
            required: true,
            unique: true,
            hashKey: true
        },
        tableId: table,   \\as per doc attribute of Table (Model) type 
        date: {
            type: String
        }
    });

This is how I retrieve table data from reservation model这就是我从预留 model 中检索表数据的方式

reservationModel.scan().exec()
        .then(posts => {
            return posts.populate({
                path: 'tableId',
                model: 'Space'
              });
        })
        .then(populatedPosts => {
        
            console.log('pp',populatedPosts);
            return {
                allData: {
                  message: "Executedddd succesfully",
                  data: populatedPosts
                }
              }
        })

Anyone please help to retrieve all Reservation data from Table??请任何人帮助从表中检索所有预订数据?

As of v2.8.2, Dynamoose does not support this.从 v2.8.2 开始,Dynamoose 不支持此功能。 Dynamoose is focused on one directional simple relationships. Dynamoose 专注于单向简单关系。 This is partly due to the fact that we discourage use of model.populate .这部分是因为我们不鼓励使用model.populate It is important to note that model.populate does another completely separate request to DynamoDB.重要的是要注意model.populate对 DynamoDB 执行另一个完全独立的请求。 This increases the latency and decreases the performance of your application.这会增加延迟并降低应用程序的性能。

DynamoDB truly requires a shift in how you think about modeling your data compared to SQL.与 SQL 相比,DynamoDB 确实需要转变您对数据建模的看法。 I recommend watching AWS re:Invent 2019: Data modeling with Amazon DynamoDB (CMY304) for a great explanation of how you can model your data in DynamoDB in a highly efficient manner.我建议观看AWS re:Invent 2019:使用 Amazon DynamoDB (CMY304) 进行数据建模,以很好地解释如何以高效的方式在 DynamoDB 中对数据进行 model。

At some point Dynamoose might add support for this, but it's really hard to say if we will.在某些时候 Dynamoose 可能会添加对此的支持,但很难说我们是否会这样做。

If you truly want to do this, I'd recommend adding a global index to your tableId property in your reservation schema.如果你真的想这样做,我建议在你的预订模式中为你的tableId属性添加一个全局索引。 Then you can run something like the following:然后你可以运行类似下面的东西:

async function code(id) {
    const reservation = await reservationModel.get(id);
    const tables = await tableModel.query("tableId").eq(id).exec(); // This will be an array of `table` entries where `"tableId"=id`. Remember, it is required you add an index for this to work.
}

Remember, this will cause multiple calls to DynamoDB and isn't as efficient.请记住,这将导致对 DynamoDB 的多次调用并且效率不高。 I'd highly recommend watching the video I linked above to get more information about how to model your data in an more efficient manner.我强烈建议观看上面链接的视频,以获取有关如何以更有效的方式 model 数据的更多信息。

Finally, I'd like to point out that your unique: true code does nothing.最后,我想指出您的unique: true代码什么都不做。 As seen in the Dynamoose Attribute Settings Documentation , unique is not a valid setting.Dynamoose 属性设置文档中所见, unique性不是有效设置。 In your case since you don't have a rangeKey , it's not possible for two items to have the same hashKey , so technically it's already a unique property based on that.在您的情况下,由于您没有rangeKey ,因此两个项目不可能具有相同的hashKey ,因此从技术上讲,它已经是基于此的唯一属性。 However it is important to note that you can overwrite existing items when creating an item.但是重要的是要注意,您可以在创建项目时覆盖现有项目。 You can set overwrite to false for document.save or Model.create to prevent that behavior and throw an error instead of overwriting your document.您可以将document.saveModel.createoverwrite设置为false以防止该行为并引发错误而不是覆盖您的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM