skip to Main Content

I have the following data structure in Dynamo and am able to execute queries according to my required access patterns, but I am attempting to use the higher level API to let the AWS framework take care of conversion/deserialization from the base dictionary types.

Dynamo Db Sample Data

When I query, I do get back all of the "rows" and fields that I expect, but I am unable to determine how to tell the framework that "this order has a collection of products"

DynamoDBContext context = new(client);
var order = context.QueryAsync<Order>("10594", new DynamoDBOperationConfig
{
    OverrideTableName = tableName,
});

This returns data in a way that makes sense conceptually, but isn’t what I’m trying to do.

enter image description here

public class Order
{
    [DynamoDBHashKey(AttributeName = "pk")]
    public string OrderId { get; set; }
    
    [DynamoDBRangeKey(AttributeName = "sk")]
    public string SortKey { get; set; }
    
    [DynamoDBProperty(AttributeName = "customerID")]
    public string CustomerId { get; set; }
    
    [DynamoDBProperty(AttributeName = "employeeID")]
    public string EmployeeId { get; set; }
    
    [DynamoDBProperty(AttributeName = "freight")]
    public decimal Freight { get; set; }
    
    [DynamoDBProperty("Products")]
    public List<Product> Products { get; set; }
    
}

Using the AmazonDynamoDbClient, I can get all the data back in a single call, but them I’m stuck mapping dictionaries to types. Is there a way to do this with the DynamoDbContext API, or when using approach should I be issuing multiple queries?

2

Answers


  1. DynamoDB doesn’t support aggregation function such as group by and project like MongoDB. In order to achieve within DynamoDB, I would suggest storing one record for each order, so list of products can be stored in list directly in that record.

    Login or Signup to reply.
  2. DynamoDB doesnt support that natively. I think that you can achive that via below sample.

    public class OrderItem
    {
        [DynamoDBHashKey(AttributeName = "pk")]
        public string OrderId { get; set; }
        
        [DynamoDBRangeKey(AttributeName = "sk")]
        public string SortKey { get; set; }
        // Other common properties
    }
    
    public class Order : OrderItem
    {
        // Properties specific to the main order
        public List<Product> Products { get; set; } = new List<Product>();
    }
    
    public class Product : OrderItem
    {
        // Properties specific to product items        
    }
    
    // query all data 
    DynamoDBContext context = new(client);
    var order = context.QueryAsync<OrderItem>("10594", new DynamoDBOperationConfig
    {
        OverrideTableName = tableName,
    });
    Order mainOrder = null;
    List<Product> products = new List<Product>();
    // loop through the list => determine your main order and the product based-on your sk
    foreach (var item in items)
    {
       if (item.SortKey == "ORDER")
       {
           mainOrder = item as Order;  // Cast if necessary and instantiate as Order
       }
       else if (item.SortKey.StartsWith("product#"))
       {
           products.Add(item as Product);  // Cast and add to products list
       }
    }
    // add product to your order object.
    if (mainOrder != null)
    {
       mainOrder.Products = products;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search