-1

I have a key in dynamo that has two Global Secondary Indexes with different range keys. Like this:

 const productSchema = new Schema(
  {
   productCategory: {
      type: String,
      index: [{
        global: true,
        rangeKey: 'serialNumberEnd',
        name: 'productCategory',
        throughput: { read: 1, write: 1 },
        project: ['quantity'],
      },
      {
        global: true,
        rangeKey: 'orderType',
        name: 'openOrders',
        throughput: { read: 1, write: 1 },
        project: true,
      }],
  },
  {
    throughput: { read: 1, write: 1 },
    useNativeBooleans: true,
    saveUnknown: true,
  },
);`

Trying to use the 'name' does not seem to be the answer.

Resource.query('openOrder').eq(id)

How am I supposed to distinguish between the two GSI's on the same Key in a resource when constructing a query?

EDIT - Added additional context to the schema, moved answer to the answer section

Bryce
  • 41
  • 2
  • 7

2 Answers2

2

In this case you don't want to be using the name property of the index. You want to be using the actual property to do this.

For example:

Resource.query('openOrder').eq(id)
.where('serialNumberEnd').lt(5)

I don't know your entire schema so it's not an exact match of what you want to do. But something like that should work.

You can also look at this test in the source code for an example of using multiple indexes on one property and querying.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
0
const result = await Product.query(
{
  hash: { productCategory: { eq: 'tags' } },
  range: { orderType: { eq: 'sale' } },
},
{ indexName: 'openOrders' },).exec();

I was a bit slow at getting to this syntax. Hope this helps someone else.

EDIT: A little cleaner syntax/more context to the schema

const result = await Product.query('productCategory', { indexName: 'openOrders' }).eq('tags')
.where('orderType').eq('purchase')
.exec();

I had no luck getting Dynamoose to recognize the correct index based on the Range found in a 'where' statement. The { indexName: 'value' } is needed.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
Bryce
  • 41
  • 2
  • 7