DynamoDB Read Operations using AWS SDK Javascript

Read an Item

const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });

const docClient = new AWS.DynamoDB.DocumentClient();

docClient.get({
    TableName: 'dev_volumes',
    Key: {
        volume_id: 'aaa',
        timestamp: 1
    }
}, (err, data) => {
    if (err) {
        console.log(err);
    } else {
        console.log(data);
    }
});

Perform a query to find some items

docClient.query({
    TableName: 'dev_volumes',
    KeyConditionExpression: "volume_id = :vid",
    ExpressionAttributeValues: {
        ":vid": 'aaa'
    }
}, (err, data) => {
    if (err) {
        console.log(err);
    } else {
        console.log(data);
    }
});

Performing a Table Scan

docClient.scan({
    TableName: 'dev_volumes',
    FilterExpression: "category = :category",
    ExpressionAttributeValues: {
        ":category": "todo"
    }
}, (err, data) => {
    if (err) {
        console.log(err);
    } else {
        console.log(data);
    }
});

Performing a Batch Get operation

Get two items given their Partition key and sort key:

docClient.batchGet({
    RequestItems: {
        'dev_volumes': {
            Keys: [
                {
                    volume_id: 'aaa',
                    timestamp: 1
                },
                {
                    volume_id: 'aaa',
                    timestamp: 2
                }
            ]
        }
    }
}, (err, data) => {
    if (err) {
        console.log(err);
    } else {
        console.log(JSON.stringify(data, null, 2));
    }
});