dynamodb sort key nodejs code example

Example 1: begins_with node js AWS dynamodb sort key

let AWS = require('aws-sdk');
let util = require('util');

AWS.config.update({
    region: 'us-east-1'
});

let connection = new AWS.DynamoDB.DocumentClient();

let params = {
    TableName: 'user_details',
    KeyConditionExpression: 'user_id = :user_id and user_relation begins_with :user_relation',
    ExpressionAttributeValues: {
        ':user_id': "1234",
        ':user_relation': "followed-by"
    }
};

console.log('getQuery Params => ', params);
let dynamoDb = util.promisify(connection.query).bind(connection);
let results = await dynamoDb(params);
console.log('results => ', results);

Example 2: begins_with node js AWS dynamodb sort key

const params = {
  TableName: 'user_details',
  KeyConditionExpression: '#user_id = :user_id and begins_with(#user_relation, :user_relation)',
  ExpressionAttributeNames:{
    "#user_id": "user_id",
    "#user_relation": 'user_relation'
  },
  ExpressionAttributeValues: {
    ":user_id": "1234",
    ":user_relation": "followed-by"
  }
}