Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ExpressionAttributeValues replacement's regex in moveParameterNames not working when index >= 10 #1653

Open
6 tasks done
ktx-mega opened this issue Jan 10, 2024 · 0 comments

Comments

@ktx-mega
Copy link
Contributor

ktx-mega commented Jan 10, 2024

Summary:

In the case a moveParameterNames is called, and there is more than 9 filters in the query, the regex on line object.ExpressionAttributeValues[key.replace(new RegExp(":v\\d"), `:${prefix}v`)] = object.ExpressionAttributeValues[key]; become invalid, because there is more than one digit in the parameter index. The whole number must be replaced.

Code sample:

Schema

Anything having a sort key

Current output and behavior (including stack trace):

    "FilterExpression": "NOT contains (#a1, :v1) AND NOT contains (#a2, :v2) AND NOT contains (#a3, :v3) AND NOT contains (#a4, :v4) AND NOT contains (#a5, :v5) AND NOT contains (#a6, :v6) AND NOT contains (#a7, :v7) AND NOT contains (#a8, :v8) AND NOT contains (#a9, :v9) AND NOT contains (#a10, :v10)",
    "ExpressionAttributeNames": {
        "#a1": "step",
        "#a2": "step",
        "#a3": "step",
        "#a4": "step",
        "#a5": "step",
        "#a6": "step",
        "#a7": "step",
        "#a8": "step",
        "#a9": "step",
        "#a10": "step",
        "#qha": "user",
        "#qra": "createdAt"
    },
    "ExpressionAttributeValues": {
        ":v1": {
            "S": "someValue1"
        },
        ":v2": {
            "S": "someValue2"
        },
        ":v3": {
            "S": "someValue3"
        },
        ":v4": {
            "S": "someValue4"
        },
        ":v5": {
            "S": "someValue5"
        },
        ":v6": {
            "S": "someValue6"
        },
        ":v7": {
            "S": "someValue7"
        },
        ":v8": {
            "S": "someValue8"
        },
        ":v9": {
            "S": "someValue9"
        },
        ":v10": {
            "S": "someValue10"
        },
        ":qhv": {
            "S": "360"
        },
        ":qrv1": {
            "N": "1672575150000"
        }
    },
    "TableName": "myTable",
    "IndexName": "myTable-createdAt-index",
    "KeyConditionExpression": "#qha = :qhv AND #qra >= :qrv",
    "ScanIndexForward": false
}

Note the createdAt ExpressionAttributeValues is ':qrv1'. It used to be :v11 but the regex only replaced the first '1'. In KeyConditionExpression, it is properly written as ':qrv'

Here's the stack :

ValidationException: Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :qrv
at throwDefaultError (/thepathtomyproject/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)
at de_QueryCommandError (/thepathtomyproject/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:2140:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /thepathtomyproject/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
at async /thepathtomyproject/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:14:20
at async /thepathtomyproject/node_modules/@aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46
at async /thepathtomyproject/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:7:26
at async main (/thepathtomyproject/node_modules/dynamoose/dist/aws/ddb/internal.js:6:20)
at async /thepathtomyproject/node_modules/dynamoose/dist/ItemRetriever.js:105:32
at async getProcessesV2 (file:///thepathtomyproject/src/controller/user.js:242:25)

Expected output and behavior:

In ExpressionAttributeValues, the regex should remove every digit of the attribute index. It should be ':qrv'.

    "FilterExpression": "NOT contains (#a1, :v1) AND NOT contains (#a2, :v2) AND NOT contains (#a3, :v3) AND NOT contains (#a4, :v4) AND NOT contains (#a5, :v5) AND NOT contains (#a6, :v6) AND NOT contains (#a7, :v7) AND NOT contains (#a8, :v8) AND NOT contains (#a9, :v9) AND NOT contains (#a10, :v10)",
    "ExpressionAttributeNames": {
        "#a1": "step",
        "#a2": "step",
        "#a3": "step",
        "#a4": "step",
        "#a5": "step",
        "#a6": "step",
        "#a7": "step",
        "#a8": "step",
        "#a9": "step",
        "#a10": "step",
        "#qha": "user",
        "#qra": "createdAt"
    },
    "ExpressionAttributeValues": {
        ":v1": {
            "S": "someValue1"
        },
        ":v2": {
            "S": "someValue2"
        },
        ":v3": {
            "S": "someValue3"
        },
        ":v4": {
            "S": "someValue4"
        },
        ":v5": {
            "S": "someValue5"
        },
        ":v6": {
            "S": "someValue6"
        },
        ":v7": {
            "S": "someValue7"
        },
        ":v8": {
            "S": "someValue8"
        },
        ":v9": {
            "S": "someValue9"
        },
        ":v10": {
            "S": "someValue10"
        },
        ":qhv": {
            "S": "360"
        },
        ":qrv": {
            "N": "1672575150000"
        }
    },
    "TableName": "myTable",
    "IndexName": "myTable-createdAt-index",
    "KeyConditionExpression": "#qha = :qhv AND #qra >= :qrv",
    "ScanIndexForward": false
}

Environment:

Operating System: Ubuntu
Operating System Version: 22.04
Node.js version (node -v): 18.12.1
NPM version: (npm -v): 9.5.0
Dynamoose version: 3.2.1

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet and Stack Overflow to ensure this issue hasn't been raised or answered before
  • I have tested the code provided and am confident it doesn't work as intended
  • I have filled out all fields above
  • I am running the latest version of Dynamoose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant