clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

Fetching all attributes in query of local secondary index with projection KEYS_ONLY #146

Closed hammadzz closed 6 years ago

hammadzz commented 6 years ago

For a query like below is there a way to specify get all attributes (fetch) rather than have to explicitly specify each one?

Account.query('foo').usingIndex('YearIndex')
.where('year').equals('2018')
.attributes(['id', 'year', 'a', 'b', 'c'])
.execAsync()
cdhowie commented 6 years ago

@hammadzz Simply omit the .attributes() call and the entire object will be fetched. (Well, all attributes present in the index will be fetched.)

hammadzz commented 6 years ago

As the attributes in the index are KEYS_ONLY that would be none of the attributes. I assume there is no feature to fetch all attributes, is that correct?

cdhowie commented 6 years ago

@hammadzz That is correct; the index doesn't have the attributes. You'll have to get them from the table.

Using a promise library like bluebird, this should work to concurrently fetch all of the "real" objects out of the table based on the results of the query:

const Promise = require('bluebird');

Promise.resolve(
  Account.query('...').usingIndex('...')
  .where('...').equals('...')
  .execAsync()
)
.get('Items')
.map(i => Account.getAsync(i.get()))
.then(items => { /* ... */ })