0
votes

From the official AWS documentation it seems that to query for items in dynamodb one needs to provide an equality key expression for the primary key. However, the AppSync doc did not explicitly say this. I am wondering is it possible in the resolver, to query the dynamodb without providing an equality key expression for the primary key of either the index or the table? For example, can I do the following:

primaryKey != anInput

or simply omit the equality expression altogether and use expressions on other attributes instead?

2

2 Answers

0
votes

The AppSync doc says: "The query expression. This field must be specified." In the case of DDB query the DDB doc says: The Query operation finds items based on primary key values. You can query any table or secondary index that has a composite primary key (a partition key and a sort key).

0
votes

It is possible to retrieve records from DynamoDB without providing a key condition expression for the primary key (or an index), but you must use the Scan operation, which accesses every item in the table (or index).

An example from AWS's AppSync Scan documentation:

{
  "version" : "2017-02-28",
  "operation" : "Scan",
  "filter" : {
    "expression" : "begins_with(title, :title)",
    "expressionValues" : {
      ":title" : { "S" : "${context.arguments.title}" }
    },
  }
}

DynamoDB Scan API:

The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index. To have DynamoDB return fewer items, you can provide a FilterExpression operation.

Regarding your specific question about finding items that do not match a particular primary key:

primaryKey != anInput

If your primaryKey is a composite of partition key + sort key, you should consider using the NOT and logical AND operators with parentheses:

{
  "version": "2017-02-28",
  "operation": "Scan",
  "filter": {
    "expression": "NOT (#PK = :PK AND #SK = :SK)",
    "expressionNames": {"#PK": "PK", "#SK": "SK"},
    "expressionValues": {
      ":PK":{"S":"some-partition-key"},
      ":SK":{"S":"some-sort-key"}
    },
  },
  "consistentRead": true,
  "limit": 10
}