Search code examples
javascriptnode.jsamazon-dynamodb

Updating AWS DynamoDB from version 2 to version 3 using KeyConditionExpression?


I’m updating my AWS DynamoDB code from version 2 to version 3 and I’m having trouble understanding how to change my use of KeyConditionExpression.

Here is my original code that uses version 2:


const getUsoAXIS = async (cuse) => {
    const query = {
        TableName: TABLE_NAME,
        KeyConditionExpression: "#cuse = :cuse AND #entity = :entity",
        ScanIndexForward: false,
        ExpressionAttributeNames: {
            "#cuse": "cuse",
            "#entity": "entity",
        },
        ExpressionAttributeValues: {
            ":cuse": cuse,
            ":entity": "USOAXIS",
        },
    };
    logger.info("Getting dynamo data in (getUsoAXIS) function", { info: { TableName: TABLE_NAME } });
    const dynamodbRecord = await getDynamodbItem(query);
    if ("Items" in dynamodbRecord && dynamodbRecord.Items.length > 0) {
        const data = dynamodbRecord.Items[0];
        logger.info("Dynamo data obtained");
        return data.data;
    } else {
        return undefined;
    }
};

And here is my updated code that uses version 3:

const getUsoAXIS = async (cuse) => {
    const params = {
        tableName: TABLE_NAME,
        key: {
            "cuse": cuse,
            "entity": "USOAXIS",
        },
    };
    logger.info("Getting dynamo data in (getUsoAXIS) function", { info: { TableName: TABLE_NAME } });
    const dynamodbRecord = await getDynamodbItem(params);
    if (dynamodbRecord) {
        logger.info("Dynamo data obtained");
        return dynamodbRecord.data;
    } else {
        return undefined;
    }
};

//fuction 
const getDynamodbItem = async (params) => {
       try {
        logger.info("Get item dynamo");
        console.log(params);
        const { tableName, key } = params;
        const command = new GetCommand({
            TableName: tableName,
            Key: key,
        });
        const response = await docClient.send(command);
        logger.info("Get item dynamo response", { info: { response } });
        return response?.Item ? response.Item : null;
    } catch (error) {
        logger.error("Error getting item dynamo", error);
    }
};

I’m having trouble understanding how KeyConditionExpression translates into DynamoDB version 3. Please, Could someone explain how this works?


Solution

  • Your issue is not between version 2 or 3, as the KeyConditionExpression for a Query remains the same. You have changed your API to use GetItem instead of Query which you used in v2.

    Option 1

    Continue to use Query:

        const query = {
            TableName: TABLE_NAME,
            KeyConditionExpression: "#cuse = :cuse AND #entity = :entity",
            ScanIndexForward: false,
            ExpressionAttributeNames: {
                "#cuse": "cuse",
                "#entity": "entity",
            },
            ExpressionAttributeValues: {
                ":cuse": cuse,
                ":entity": "USOAXIS",
            },
        };
    
        const dynamodbRecord = await docClient.send(new QueryCommand(command));
    

    Option 2

    If you only have a single item, which I presume is true as you query from the base table, you can use GetItem instead, which you highlight in your code. Yo do not state a KeyConditionExpression in a GetItem, you just specify the primary key, which is the partition and sort key of the item.