Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const AWS = require('aws-sdk');
- const dynamoDbDocumentClient = new AWS.DynamoDB.DocumentClient({
- apiVersion: '2012-10-08',
- region: 'eu-west-1',
- });
- const openDbConnection = () => dynamoDbDocumentClient;
- const getTableName = (table) => {
- const environment = process.argv[2];
- return `${table}${environment}`;
- };
- const scan = async ({
- dbConnection, filter, values, table,
- }) => {
- const { Items } = await dbConnection.scan({
- FilterExpression: filter,
- ExpressionAttributeValues: values,
- TableName: table,
- }).promise();
- return Items;
- };
- const updateItem = async ({
- dbConnection,
- table,
- id,
- expression,
- values,
- names,
- condition,
- returnUpdated = false,
- }) => {
- try {
- const result = await dbConnection
- .update({
- TableName: table,
- Key: { id },
- UpdateExpression: expression,
- ExpressionAttributeValues: values,
- ExpressionAttributeNames: names,
- ConditionExpression: condition,
- ReturnValues: returnUpdated ? 'ALL_NEW' : 'NONE',
- })
- .promise();
- return (returnUpdated && result.Attributes) || true;
- } catch (error) {
- if (error.code === 'ConditionalCheckFailedException') {
- log(`Item ${id} in ${table} did not pass the update condition: ${condition}`);
- return false;
- }
- throw error;
- }
- };
- const handler = async () => {
- const dbConnection = openDbConnection();
- const conversations = await scan({
- dbConnection,
- table: getTableName('Conversations'),
- });
- const updatedConversations = await Promise.all(
- conversations.map(async (conversation) => updateItem({
- dbConnection,
- table: getTableName('Conversations'),
- id: conversation.id,
- expression: 'SET groupDetails = :groupDetails',
- values: { ':groupDetails': null },
- condition: 'attribute_not_exists(groupDetails)',
- returnUpdated: true,
- })),
- );
- console.log(updatedConversations);
- };
- handler();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement