Skip to content

Commit

Permalink
Merge pull request #1654 from ktx-mega/main
Browse files Browse the repository at this point in the history
fix : replace any numeric character when moving parameter in ExpressionAttributeValues
  • Loading branch information
fishcharlie committed Apr 6, 2024
2 parents 1e5b220 + d64b8a3 commit fd010eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/dynamoose/lib/ItemRetriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ ItemRetriever.prototype.getRequest = async function (this: ItemRetriever): Promi

const valueKey = key.replace("#a", ":v");

Object.keys(object.ExpressionAttributeValues).filter((key) => key.startsWith(valueKey)).forEach((key) => {
object.ExpressionAttributeValues[key.replace(new RegExp(":v\\d"), `:${prefix}v`)] = object.ExpressionAttributeValues[key];
Object.keys(object.ExpressionAttributeValues).filter((key) => key === valueKey || key.startsWith(`${valueKey}_`)).forEach((key) => {
object.ExpressionAttributeValues[key.replace(new RegExp(":v\\d+"), `:${prefix}v`)] = object.ExpressionAttributeValues[key];
delete object.ExpressionAttributeValues[key];
});
const newExpression = filterExpression.replace(key, `#${prefix}a`).replace(new RegExp(valueKey, "g"), `:${prefix}v`);
Expand Down
53 changes: 53 additions & 0 deletions packages/dynamoose/test/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,59 @@ describe("Query", () => {
});
});

it("Should send correct request with 10+ arguments", async () => {
queryPromiseResolver = () => ({"Items": []});
Model = dynamoose.model("Cat", {"id": String, "name": {"type": String, "index": {"type": "global", "rangeKey": "color"}}, "color": String});
new dynamoose.Table("Cat", [Model]);
const functionToTest = Model.query().where("name").eq("Charlie")
.where("color").eq("I")
.where("color").eq("am")
.where("color").eq("a")
.where("color").eq("test")
.where("color").eq("with")
.where("color").eq("more")
.where("color").eq("than")
.where("color").eq("ten")
.where("color").eq("arguments")
.where("color").eq("in")
.where("color").eq("query");
await callType.func(functionToTest.exec).bind(functionToTest)();
expect(queryParams).toEqual({
"TableName": "Cat",
"IndexName": "nameGlobalIndex",
"ExpressionAttributeNames": {
"#qha": "name",
"#qra": "color",
"#a2": "color",
"#a3": "color",
"#a4": "color",
"#a5": "color",
"#a6": "color",
"#a7": "color",
"#a8": "color",
"#a9": "color",
"#a10": "color",
"#a11": "color"
},
"ExpressionAttributeValues": {
":qhv": {"S": "Charlie"},
":qrv": {"S": "I"},
":v2": {"S": "am"},
":v3": {"S": "a"},
":v4": {"S": "test"},
":v5": {"S": "with"},
":v6": {"S": "more"},
":v7": {"S": "than"},
":v8": {"S": "ten"},
":v9": {"S": "arguments"},
":v10": {"S": "in"},
":v11": {"S": "query"}
},
"FilterExpression": "#a2 = :v2 AND #a3 = :v3 AND #a4 = :v4 AND #a5 = :v5 AND #a6 = :v6 AND #a7 = :v7 AND #a8 = :v8 AND #a9 = :v9 AND #a10 = :v10 AND #a11 = :v11",
"KeyConditionExpression": "#qha = :qhv AND #qra = :qrv"
});
});

it("Should send correct request on query.exec using array of indexes", async () => {
queryPromiseResolver = () => ({"Items": []});
Model = dynamoose.model("Cat", {"id": String, "name": {"type": String, "index": [{"type": "global", "rangeKey": "age", "name": "NameAgeIndex"}, {"type": "global", "rangeKey": "breed", "name": "NameBreedIndex"}]}, "age": Number, "breed": String});
Expand Down

0 comments on commit fd010eb

Please sign in to comment.