I can not use query in dynamoose becasue error: 'Index can't be found for query.'

267 views Asked by At

I'm using nest JS 9 which are using dynamoDB and dynamoose.
I found a library which can support me connect to db nestjs-dynamoose
i tried to write a get many API but a got Error at .exec()

const pagination = formatPagination(basePagination);
    const query = this.baseRepo
      .query()
      .limit(+pagination.limit);
      .sort(pagination.sort);
    if (pagination.page > 1) {
      query.startAt(basePagination.keyItem);
    }
    const result = await query
      .exec()
      .then((result) => result)
      .catch((err) => err);
    return result;

here is my schema

schema: new Schema(
    {
      id: {
        type: Number,
        rangeKey: true,
      },

      songCode: {
        type: String,
        index: {
          name: 'songCodeIndex',
        },
      },

      music: {
        type: String,
        hashKey: true,
        index: {
          name: 'musicIndex',
          rangeKey: 'id',
        },
      },
  // ..... another field
    },
    {
      timestamps: defaultSettings,
    },
  )

I replaced Query with Scan. It worked, but the scan command did not support sort options.
I tried to follow the GPT solution, but it seemed to not work.
I checked in the noSQL workbench and saw 2 GSIs in the table.

Please help me. I'm stucking more than 4 hours at this bug.

1

There are 1 answers

2
Leeroy Hannigan On BEST ANSWER

When you use Query, you must always pass in the partition key value. And if you want to Query from one of your indices you must explicitly state so:

const query = this.baseRepo
      .query("music").eq("some-music")
      .using("musicIndex")

I suggest you thoroughly read the Dynamoose Docs on Query