circles-learning-labs / ecto_adapters_dynamodb

DynamoDB adapter for Elixir's Ecto Database layer.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support primary-key queries with a filter condition

darrenklein opened this issue · comments

When querying by a primary key with a filter condition, it seems that the filter condition is not being applied - for example:

from(f in Foo, where: f.id == "1234" and is_nil(f.deleted_at))
|> Repo.one()

will ignore the filter condition and will return the record, even in a scenario where deleted_at is not nil - I would expect that this would return nil, as is the case with the Postgres adapter.

DynamoDB does support this kind of query:

var params = {
    TableName: 'foo',
    KeyConditionExpression: 'id = :value',
    FilterExpression: 'attribute_not_exists(deleted_at) or deleted_at = :null',
    ExpressionAttributeValues: {
      ':value': '1234,
      ':null': null
    },
};
docClient.query(params, function(err, data) {
    if (err) ppJson(err);
    else ppJson(data);
});

That behaves as expected, no record is returned.

@alhambra1 I found that the reason for this was that we were using ExAws.Dynamo.get_item, rather than ExAws.Dynamo.query for queries on a primary key - so the filter was not being applied. I was able to resolve this issue by making use of ExAws.Dynamo.query instead - can I ask you for a code review on the branch "filter", c3e79469e35f032edd119fffc3da5961ae27dabe?

I added test conditions for this and all tests are passing, but would appreciate your eyes on this before I merge to master.

@alhambra1 Ok, I think I've got the right (or at least, better) approach to resolving this issue - see branch "query_filter", 1cdecb554c028f1cbf38add3c0ef6f23d97a3e39.

Basically, if the search argument in a primary key query contains fields that are not reflected in the indexes, we'll use ExAws.Dynamo.query - otherwise, we'll use ExAws.Dynamo.get_item.