henhal / dynamodb-expressions

Helpers for creating DynamoDB expressions
MIT License
0 stars 0 forks source link

buildKeyConditionParams messed up if values contains # #4

Open MrNghia123 opened 7 months ago

MrNghia123 commented 7 months ago

Thank you for the package. I found that buildKeyConditionParams does not produce the right output when input values contain the character '#'

Minimal producible

const params = { TableName: "main", ScanIndexForward: false, Limit: 25, } const conditions = { pk: "s#danteshirt", sk: Condition.beginsWith('v#'), }

const queryInput = buildKeyConditionParams({ params, conditions })

//queryInput = { TableName: "main", ScanIndexForward: false, Limit: 25, ExpressionAttributeNames: { "#pk": "pk", "#danteshirt": "danteshirt", "#sk": "sk", }, KeyConditionExpression: "#pk = s#danteshirt AND begins_with(#sk, v#)", }

henhal commented 7 months ago

Thanks for your issue!

I reproduced the problem; however, this case is covered in the README under pitfalls - when a value contains a #, the entire value must be prefixed with a : to avoid ambiguity. Without it, the library cannot know if the value is a reference to an attribute name - including nested names - or a string literal containing a #.

I verified that the output is as expected if prepending the ::

const conditions = {
  pk: ":s#danteshirt",
  sk: Condition.beginsWith(':v#'),
}

Result:

 {
    KeyConditionExpression: "#pk = :cond_ AND begins_with(#sk, :cond_begins_with_arg0)",
    ExpressionAttributeNames: { '#pk': 'pk', '#sk': 'sk' },
    ExpressionAttributeValues: { ':cond_': 's#danteshirt', ':cond_begins_with_arg0': 'v#' }
 }