DEV Community

codeisgood
codeisgood

Posted on

1

DynamoDB Scan with sort

To query and retrieve 100 records from DynamoDB and order them by a sort key column in Node.js, you can use the AWS SDK for JavaScript. Make sure you have the AWS SDK installed and configured. Here's an example code snippet:

const AWS = require('aws-sdk');

AWS.config.update({
  region: 'your-dynamodb-region', // Replace with your DynamoDB region
  accessKeyId: 'your-access-key-id', // Replace with your AWS access key ID
  secretAccessKey: 'your-secret-access-key', // Replace with your AWS secret access key
});

const dynamoDB = new AWS.DynamoDB.DocumentClient();
const tableName = 'your-dynamodb-table-name'; // Replace with your DynamoDB table name
const sortKeyColumn = 'your-sort-key-column'; // Replace with your sort key column name

const params = {
  TableName: tableName,
  Limit: 100,
  ScanIndexForward: true, // Set to false for descending order
  KeyConditionExpression: '#sk > :sk', // Replace > with < for descending order
  ExpressionAttributeNames: {
    '#sk': sortKeyColumn,
  },
  ExpressionAttributeValues: {
    ':sk': '0', // Replace with your start key value
  },
};

dynamoDB.query(params, (err, data) => {
  if (err) {
    console.error('Error querying DynamoDB:', err);
  } else {
    console.log('Queried records:', data.Items);
  }
});

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay