Repository: dynamoosejs/dynamoose
Pull request #789 (2020-04-08 03:41:33)

Query no range key bug


Patch:
diff --git a/lib/DocumentRetriever.js b/lib/DocumentRetriever.js
index c0d5038b..d4e9c1a9 100644
--- a/lib/DocumentRetriever.js
+++ b/lib/DocumentRetriever.js
@@ -70,7 +70,7 @@ function main(documentRetrieverTypeString) {
 						res[item.KeyType.toLowerCase()] = item.AttributeName;
 						return res;
 					}, {});
-					return comparisonChart[hash].type === "EQ" && (!range || comparisonChart[range].type === "EQ");
+					return (comparisonChart[hash] || {}).type === "EQ" && (!range || !comparisonChart[range] || comparisonChart[range].type === "EQ");
 				});
 				if (!index) {
 					throw new Error.InvalidParameter("Index can't be found for query.");
@@ -78,7 +78,11 @@ function main(documentRetrieverTypeString) {
 				object.IndexName = index.IndexName;
 			}
 			function moveParameterNames(val, prefix) {
-				const [key, value] = Object.entries(object.ExpressionAttributeNames).find((entry) => entry[1] === val);
+				const entry = Object.entries(object.ExpressionAttributeNames).find((entry) => entry[1] === val);
+				if (!entry) {
+					return;
+				}
+				const [key, value] = entry;
 				object.ExpressionAttributeNames[`#${prefix}a`] = value;
 				delete object.ExpressionAttributeNames[key];
 


Test patch:
diff --git a/test/Query.js b/test/Query.js
index 7522fc6b..0550fdce 100644
--- a/test/Query.js
+++ b/test/Query.js
@@ -302,6 +302,23 @@ describe("Query", () => {
 					});
 				});
 
+				it("Should send correct request on query.exec for index with hash and range key but only querying hash key", async () => {
+					Model = new dynamoose.Model("Cat", {"id": Number, "age": {"type": Number, "index": {"global": true, "rangeKey": "name"}}, "name": String});
+					queryPromiseResolver = () => ({"Items": []});
+					await callType.func(Model.query("age").eq(1).exec).bind(Model.query("age").eq(1))();
+					expect(queryParams).to.eql({
+						"TableName": "Cat",
+						"IndexName": "ageGlobalIndex",
+						"ExpressionAttributeNames": {
+							"#qha": "age"
+						},
+						"ExpressionAttributeValues": {
+							":qhv": {"N": "1"}
+						},
+						"KeyConditionExpression": "#qha = :qhv"
+					});
+				});
+
 				it("Should return correct result with get function for attribute", async () => {
 					Model = new dynamoose.Model("Cat", new dynamoose.Schema({"id": Number, "name": {"type": String, "index": {"global": true}, "get": (val) => `${val}-get`}}));
 					queryPromiseResolver = () => ({"Items": [{"id": {"N": "1"}, "name": {"S": "Charlie"}}]});
@@ -321,6 +338,12 @@ describe("Query", () => {
 					return expect(callType.func(Model.query("name").eq("Charlie").exec).bind(Model.query("name").eq("Charlie"))()).to.be.rejectedWith("Index can't be found for query.");
 				});
 
+				it("Should throw error if not querying index hash key", async () => {
+					Model = new dynamoose.Model("Cat", {"id": Number, "age": {"type": Number, "index": {"global": true, "rangeKey": "name"}}, "name": String});
+					queryPromiseResolver = () => ({"Items": []});
+					return expect(callType.func(Model.query("name").eq("Charlie").exec).bind(Model.query("name").eq("Charlie"))()).to.be.rejectedWith("Index can't be found for query.");
+				});
+
 				it("Should throw error from AWS", () => {
 					queryPromiseResolver = () => {
 						throw {"error": "Error"};