I want to execute a SELECT query on DynamoDb with PartiQL in NodeJs, and getting errors.
a simple statement works with no problem.
const r = await client.send(
new ExecuteStatementCommand({
Statement: `SELECT * FROM Users WHERE userId in ['abcd1234']`
})
)
but when try to execute statements with parameters, i got an error.
code
const r2 = await client.send(
new ExecuteStatementCommand({
Statement: `SELECT * FROM Users WHERE userId in ?`,
Parameters: [
['abcd1234']
]
})
)
error
{
"errorType": "ValidationException",
"errorMessage": "1 validation error detected: Value '[]' at 'parameters' failed to satisfy constraint: Member must have length greater than or equal to 1",
"trace": [
"ValidationException: 1 validation error detected: Value '[]' at 'parameters' failed to satisfy constraint: Member must have length greater than or equal to 1",
" at deserializeAws_json1_0ExecuteStatementCommandError (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:2202:41)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)",
" at async /var/task/node_modules/@aws-sdk/middleware-serde/dist/cjs/deserializerMiddleware.js:6:20",
" at async /var/task/node_modules/@aws-sdk/middleware-signing/dist/cjs/middleware.js:12:24",
" at async StandardRetryStrategy.retry (/var/task/node_modules/@aws-sdk/middleware-retry/dist/cjs/defaultStrategy.js:56:46)",
" at async /var/task/node_modules/@aws-sdk/middleware-logger/dist/cjs/loggerMiddleware.js:6:22",
" at async /var/task/node_modules/@aws-sdk/lib-dynamodb/dist/cjs/commands/ExecuteStatementCommand.js:29:26",
" at async Runtime.handler (/var/task/save.js:39:16)"
]
}
Does anyone has any solutions?
here is the whole code.
const client = DynamoDBDocumentClient.from(
new DynamoDBClient({})
);
// this works
const r = await client.send(
new ExecuteStatementCommand({
Statement: `SELECT * FROM Users WHERE userId in ['abcd1234']`
})
)
// this gets an error
const r2 = await client.send(
new ExecuteStatementCommand({
Statement: `SELECT * FROM Users WHERE userId in ?`,
Parameters: [
['abcd1234']
]
})
)
As far as I can tell, it's not quite possible to do what you're trying to do (pass an array of strings as a parameter in a prepared statement as the right-hand argument to an IN operator ).
It seems that the best you can do is:
Note that, the
Parameters
property takes an array of key-value pairs. Each parameter attribute value is described as a name-value pair. The name is the data type (e.g."S"
for string), and the value is the data itself (e.g. the user ID.)In your case, if you had a list of users, you could do something like:
Be aware that there's probably some upper limit to how many parameters you can pass, but I couldn't find this number documented anywhere.