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

Cannot use "startKey" when using an index #184

Open
mhconradt opened this issue Jul 2, 2019 · 1 comment
Open

Cannot use "startKey" when using an index #184

mhconradt opened this issue Jul 2, 2019 · 1 comment

Comments

@mhconradt
Copy link

mhconradt commented Jul 2, 2019

Example query below:
const { Items, LastEvaluatedKey } = await this.model. query(objectId). usingIndex('objectId-updatedAt-index'). startKey(objectId, startKey || "_"). limit(limit || 20). attributes(this.defaultAttributes). execAsync();
The generated request is here:
{ IndexName: 'objectId-updatedAt-index', ExclusiveStartKey: { objectId: 'alpha1_customer_1', noteId: '_' }, Limit: 20, ProjectionExpression: '#objectId,#noteId,#updatedAt,#message', ExpressionAttributeNames: { '#objectId': 'objectId', '#noteId': 'noteId', '#updatedAt': 'updatedAt', '#message': 'message' } } }
It generates the following error:
ValidationException: The provided starting key is invalid
The problem is that the generated start key has the attributes objectId and noteId, when the index requires objectId and updatedAt.
I am going to submit a PR for this shortly.

@deedw
Copy link

deedw commented Sep 4, 2019

I also ran into this issue.

startKey() provides the value for ExclusiveStartKey in the DynamoDB api call. The only valid value for ExclusiveStartKey is the value returned as LastEvaluatedKey from the previous query or scan operation, so correct functionality can be restored by changing the following functions:

Query.prototype.startKey = function (key) {
  this.request.ExclusiveStartKey = key;
  return this;
};
Scan.prototype.startKey = function (key) {
  this.request.ExclusiveStartKey = key;

  return this;
};

Note that this would be a breaking change from existing startKey(hashKey, rangeKey) parameters.

I implemented a temporary fix in my application as follows:

const dynogels = require('dynogels');

require('dynogels/lib/query').prototype.startKey = function (key) {
    this.request.ExclusiveStartKey = key;

  return this;
};

require('dynogels/lib/scan').prototype.startKey = function (key) {
    this.request.ExclusiveStartKey = key;

  return this;
};

jd-bell pushed a commit to jd-bell/dynogels-promise that referenced this issue Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants