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

Defining and querying a local index. #439

Closed
7 of 11 tasks
dolsem opened this issue Oct 13, 2018 · 8 comments · Fixed by #442
Closed
7 of 11 tasks

Defining and querying a local index. #439

dolsem opened this issue Oct 13, 2018 · 8 comments · Fixed by #442

Comments

@dolsem
Copy link
Contributor

dolsem commented Oct 13, 2018

Summary:

I have a schema for posts with timestamps that uses createdAt as the range key and updatedAt as a local index. The idea is to be able to query posts by a particular author by both createdAt and updatedAt.

Code sample:

Schema

const PostSchema = new dynamoose.Schema({
  author: {
    type: String,
    hashKey: true,
  },
  createdAt: {
    type: Number,
    rangeKey: true,
    default: Date.now,
  },
  updatedAt: {
    type: Number,
    default: Date.now,
    index: true,
  },
  text: String,
  deletedAt: {
    type: Number,
    index: {
      global: true,
    },
  },
}, {
  timestamps: true,
});

General

Post.query('author').eq(author).where('createdAt').lt(startAt).exec((err, posts) => {
// All posts created before the `startAt` timestamp.
}
Post.query('author').eq(author).where('updatedAt').gt(since).exec((err, posts) => {
// All posts updated (or created) after the `since` timestamp.
}

Current output and behavior:

First query works as expected. Second query fails with:

Query condition missed key schema element: createdAt

Expected output and behavior:

Both queries work fine.

Environment:

Operating System: Linux
Operating System Version: 4.15.0-36-generic
Node.js version (node -v): v9.11.2
NPM version: (npm -v): 6.4.1
Dynamoose version: 1.0.0

Other information (if applicable):

I've noticed that local index queries in Query tests in Dynamoose look differently. Instead of querying on the hash key of the table first and then specifying local index as the range key, it queries on the local index directly. This would make sense for a global index, but for local? If I understand DynamoDB correctly, local index allows you to choose an alternative range key for the table, while keeping the hash key. So by querying local index I should be able to choose the partition based on the author's name, then display all posts in that partition, sorted by the updatedAt values. Am I missing / not understanding something?

Type (select 1):

  • Bug report
  • Feature suggestion
  • Question
  • Other suggestion
  • Something not listed here

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
@fishcharlie
Copy link
Member

@dolsem Sadly I have only really worked with global indexes. I'd recommend checking out the AWS DynamoDB documentation.

I also saw this answer that might help you.

@yokkoyokko
Copy link

I have the same error and there seems to be a bug in Query.js file. Please check my comment of this page.
3a85c95#diff-c7f251522c2a751e0d457dee5ffb83f0

@fishcharlie
Copy link
Member

@yokkoyokko You might be right. It was part of PR #344 and issue #343. Any ideas what this should be instead?

@dolsem
Copy link
Contributor Author

dolsem commented Oct 14, 2018

@yokkoyokko is right, @fishcharlie. I've just tested his suggestion in that comment and it works. It also doesn't break any of the existing tests. But that brings another problem: queries don't return any data. If I change my second query to Post.query('author').eq(author).where('createdAt').gt(0), it returns all posts by that author, because every timestamp is obviously greater than 0. But if I change 'createdAt' back to 'updatedAt', no posts get returned. Moreover, scannedCount is always 0.

@dolsem
Copy link
Contributor Author

dolsem commented Oct 14, 2018

I've tested this with an older version of Dynamoose, before PR #344, and it has the same problem: no data gets returned.

@fishcharlie
Copy link
Member

@dolsem Super strange. Would you mind submitting a PR that creates a test for that failing case? And if you have a fix for #343 that doesn't break things feel free to include that as well.

@dolsem
Copy link
Contributor Author

dolsem commented Oct 14, 2018

@fishcharlie I'm investigating this issue further. It gets even more strange. The issue is with this line. val is 0, but it returns what seems to be the result of Date.now(). So I guess it ignores the value and uses the default value of the attribute instead.

@fishcharlie
Copy link
Member

@dolsem Sadly I don't have time to help investigate right now. If you do happen to figure out what is going on feel free to submit a PR and I can take care of that ASAP. @brandongoode, any thoughts here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants