DEV Community

Edisson Ballesteros Aguilar
Edisson Ballesteros Aguilar

Posted on • Edited on

Get Paginated Data DynamoDB

function getLogsNovedades() {

  const getAllData = async (params) => {
    const _getAllData = async (params, startKey) => {
      if (startKey) {
        params.ExclusiveStartKey = startKey
      }
      return dynamodb.query(params).promise();
    }
    let lastEvaluatedKey = null
    let rows = []
    do {
      const result = await _getAllData(params, lastEvaluatedKey)
      rows = rows.concat(result.Items)
      lastEvaluatedKey = result.LastEvaluatedKey
    } while (lastEvaluatedKey)
    return rows
  }

  var params = {
    TableName: tableName,
    ProjectionExpression: "Id, #Type, #Data, IDPV, #Name, #Timestamp",
    IndexName: 'Type-index',
    KeyConditionExpression: '#Type = :tp',
    ExpressionAttributeNames: {
      '#Type': 'Type',
      '#Data': 'Data',
      '#Name': 'Name',
      '#Timestamp': 'Timestamp'
    },
    ExpressionAttributeValues: {
      ':tp': 'NOVEDAD',
    },
    Limit: 100
  };

  return new Promise(async (resolve, reject) => {
    try {
      let Items = await getAllData(params);
      resolve(Items);
    } catch (err) {
      reject(err);
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)