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

v0.2 Get query on secondary index failing due to requiring PK #39

Closed
justynspooner opened this issue May 6, 2020 · 1 comment
Closed
Labels
question Further information is requested

Comments

@justynspooner
Copy link

I think this might just be a lack of my knowledge of DynamoDB again, but I thought that the following test shouldn't throw the error:

'pk' is required

I thought, that because the GSI partitionKey has been specified, that I can run a get query against it without having to include the main table's partitionKey. The error suggests that this is not the case. I'm a bit confused and I'm sure there is a simple answer to this which I'm struggling to find. Any pointer would be greatly appreciated.

const TestTable3 = new Table({
    name: 'test-table',
    partitionKey: 'pk',
    sortKey: 'sk',
    indexes: {
        GSI1: { partitionKey: 'GSI1sk', sortKey: 'data' },
    },
    DocumentClient,
})

const TestEntity3 = new Entity({
    name: 'TestEntity',
    attributes: {
        pk: { type: 'string', partitionKey: true },
        sk: { type: 'string', partitionKey: 'GSI1', sortKey: true },
        data: { type: 'string', sortKey: 'GSI1' },
    },
    table: TestTable3,
})

describe('get', () => {
    it('gets the Key from GSI1 inputs (sync)', async () => {
        const { TableName, Key } = TestEntity3.getParams({
            sk: 'test-sk', // This is the GSI1 partitionKey I also tried the name `GSI1sk`
            data: 'test-data',
        })
        expect(TableName).toBe('test-table')
        expect(Key).toEqual({ sk: 'test-sk', data: 'test-data' })
    })
})
@justynspooner justynspooner changed the title Get query on secondary index failing due to requiring PK v0.2 Get query on secondary index failing due to requiring PK May 6, 2020
@jeremydaly
Copy link
Owner

Hi @justynspooner,

You cannot get an item from a secondary index (this is a DynamoDB thing), you would need to query the GSI instead.

Use this instead:

const payload = TestTable3.queryParams(
  'test-sk', // your partitionKey on GSI1
  {
    eq: 'test-data', // the value of the GSI1 sortKey you want to equal
    index: 'GSI1' // the name of the index you are querying
  }
)

@jeremydaly jeremydaly added the question Further information is requested label May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants