Home > Mobile >  Why doesn't working LastEvaluatedKey on the DynamoDB
Why doesn't working LastEvaluatedKey on the DynamoDB

Time:12-12

I'm using DynamoDB to store some additional info but it's not working on the order I stored data like seed based on my JSON file But it seems like an irregular position about the ID I set the partition key to ID But on the DynamoDB has arranged irregular ID enter image description here So I cannot do any control like pagination according to the LastEvaluatedKey How can I fix the issue?

CodePudding user response:

You want to get a list of items sorted by the ID property. You set ID property to be the partition key. You do not have a sort key. You state that the items returned are not sorted by ID.

This implies you are performing a scan operation. Scan operations do not guarantee the result will be in a sorted order. To get items in a sorted order you must define a sort key for the table and perform a query operation.

If you create a new table and define the partition key as 'item_type' and the sort key as 'ID', you will be able to run a query and return all items of a specified 'item_type' sorted by 'ID'.

CodePudding user response:

Thanks for your mail. I tried that. but when I put the data in my DynamoDB as seed. But the data is stored my DB as random

const axios = require('axios');
const { addOrUpdateCharacter } = require('./dynamo');
const seedData = async () => {
const url = 'http://hp-api.herokuapp.com/api/characters';
try {
    const characters = [];
   
    for(let i=0; i<characters.length; i  ) {
      await addOrUpdateCharacter({...characters[i], ID: i ''})
    }
    await Promise.all(characterPromises);
    } catch (err) {
    console.error(err);
    console.log('AHHHHHHHHHHH');
    }
 };

seedData();

Could you please check my code? why the data is random stored I used the await function

  • Related