1

The code bellow works fine but I wonder if this is the right way to query by sortkey using .NET Persistence Model. Examples from official documentation are using other approach. Just wondering if this is fine too?

public async Task<List<Transaction>> GetUserTransactionsAsync(string userEmail, DateTime start, DateTime end)
{
    var operationConfig = new DynamoDBOperationConfig
    {
        IndexName = DynamoConstants.TransactionsUserDateIndexName,
        
    };
    var sortKeyValues = new List<object> { start, end };

    return await _dBContext
        .QueryAsync<Transaction>(userEmail, QueryOperator.Between, sortKeyValues,  operationConfig)
        .GetRemainingAsync();
}

Global secondary Index:

transactionsTable.AddGlobalSecondaryIndex(new GlobalSecondaryIndexProps
{
    IndexName = DynamoConstants.TransactionsUserDateIndexName,
    PartitionKey = new Attribute { Name = nameof(Transaction.UserEmail), Type = AttributeType.STRING },
    SortKey = new Attribute { Name = nameof(Transaction.Date), Type = AttributeType.STRING},
    ReadCapacity = 10,
    WriteCapacity = 10,
    ProjectionType = ProjectionType.ALL
});

Model:

[DynamoDBTable(DynamoConstants.TransactionsTableName)]
public class Transaction
{
    [DynamoDBHashKey]
    public string TransactionId { get; set; }

    [DynamoDBGlobalSecondaryIndexHashKey]
    public string UserEmail { get; set; }

    [DynamoDBGlobalSecondaryIndexRangeKey]
    public DateTime Date { get; set; }
    
    // Some attributes
}
RogerM
  • 21
  • 5

0 Answers0