shiftcode / dynamo-easy

DynamoDB client for NodeJS and browser with a fluent api to build requests. We take care of the type mapping between JS and DynamoDB, customizable trough typescript decorators.
https://shiftcode.github.io/dynamo-easy/
MIT License
206 stars 27 forks source link

Documentation: .index must be called before .wherePartitionKey #301

Closed jthomerson closed 2 years ago

jthomerson commented 4 years ago

It would be good to show examples of querying on a GSI in the documentation. I was scratching my head for a while because I had code like this:

async function find(pk: string, skPrefix?: string, index?: MyIndexEnum): Promise<Foo[]> {
   let query = store.query().wherePartitionKey(pk);

   if (index) {
      query = query.index(index);
   }

   if (skPrefix && !isEmpty(skPrefix)) {
      query = query.whereSortKey().beginsWith(skPrefix.toLowerCase());
   }

   console.debug(`Query ${index || 'table'} for "${pk}" begins_with(${skPrefix})`);

   return query.exec();
}

I didn't realize that you had to call .index(...) before calling wherePartitionKey. That seems a bit counterintuitive on this builder pattern, but if it was documented along with the query documentation, that'd make it easy to see.

michaelwittwer commented 2 years ago

@jthomerson also closing in favour of #374, thanks for bringing that to our attention