3

I was reading about how to model many-to-many relationships in DynamoDB from this article: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html

enter image description here

Let's say that the requirement is to display a list of all Bills for a given Invoice. But you need to display all attributes for each bill (red circles in the image).

I could query all the Bills for Invoice-92551 as follow:

    var params = {
       TableName: "some-table",
       KeyConditionExpression: "#pk = :pk",
       ExpressionAttributeNames: {
          "#pk":"pk",
       },
       ExpressionAttributeValues: {
          ":pk": "Invoice-92551"
       },
       ProjectionExpression: "pk, sk, isPaid, Amount, created...etc"
    }; 

docClient.query(params, function(error, billsForGivenInvoice) {...});

Ok, I have now the bills but I need more attributes for each of them. I could do the following:

            var params = {
                RequestItems: {
                    "some-table": {
                        Keys: ["Bill-4224663", "Bill-4224687"],
                        ProjectionExpression: "...other attributes needed..."
                    }
                }
            };

            docClient.batchGet(params, function(error, bills) {});

Is it possible to query both results in one go?. Instead of calling first to query() and them a batchGet().

Mark
  • 90,562
  • 7
  • 108
  • 148
osotorrio
  • 960
  • 1
  • 11
  • 30
  • Are you trying to get the data from different tables? If yes, it is not possible to achieve this in dynamodb as there is no concept of join. – notionquest Jun 05 '18 at 19:56
  • 1
    No, there is only one table as you can see in the image. One table which represents a many-to-many relationship by applying the Adjacency List Design Pattern. – osotorrio Jun 05 '18 at 21:17
  • I believe the Adjacency List pattern would have you create a global secondary index that has your same partition keys, but uses the sort key to define the relationships _from Bill to Invoice_ (in other words, the opposite of the relationships you've expressed so far). You would then use that index to find all Bills that are related to your Invoice, and you could obtain all the attributes of those Bills. – Charlie Flowers Mar 12 '20 at 19:56

0 Answers0