Skip to content

Commit c02316c

Browse files
committed
v1.4.2 enable conditional updates
1 parent 99c7ed4 commit c02316c

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

dynadash.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
QueryOutput,
1616
GetItemCommandOutput,
1717
WriteRequest,
18-
QueryCommandOutput,
1918
} from '@aws-sdk/client-dynamodb';
2019
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
2120

@@ -389,13 +388,17 @@ export async function queryTable<R>(
389388
export async function updateTableRow<R>(
390389
TableName: UpdateItemCommandInput['TableName'],
391390
keys: { [x: string]: any },
392-
UpdateExpression: string,
393-
expressionAttributeValues: { [x: string]: any },
394-
ExpressionAttributeNames?: { [x: string]: string },
391+
params: {
392+
UpdateExpression: string;
393+
expressionAttributeValues: { [x: string]: any };
394+
ExpressionAttributeNames?: { [x: string]: string };
395+
ConditionExpression?: string;
396+
},
395397
ReturnValues = 'ALL_NEW',
396398
) {
397399
if (!TableName) return logTableNameUndefined();
398400
try {
401+
const { UpdateExpression, expressionAttributeValues, ExpressionAttributeNames, ConditionExpression } = params;
399402
const ddb = new DynamoDBClient({});
400403
const query: UpdateItemCommandInput = {
401404
TableName,
@@ -405,6 +408,7 @@ export async function updateTableRow<R>(
405408
removeUndefinedValues: true,
406409
}),
407410
ExpressionAttributeNames,
411+
ConditionExpression,
408412
ReturnValues,
409413
};
410414
const result = await ddb.send(new UpdateItemCommand(query));
@@ -420,32 +424,33 @@ export async function updateTableRow<R>(
420424
* @param TableName
421425
* @param keys
422426
* @param row
427+
* @param ConditionExpression
423428
*/
424429
export async function shallowUpdateTableRow(
425430
TableName: UpdateItemCommandInput['TableName'],
426431
keys: { [x: string]: any },
427432
row: { [x: string]: any },
433+
ConditionExpression?: string,
428434
) {
429435
const updateExpressions = [];
430436
const expressionAttributeValues: {
431437
[x: string]: any;
432438
} = {};
433-
const expressionAttributeNames: { [x: string]: string } = {};
439+
const ExpressionAttributeNames: { [x: string]: string } = {};
434440

435441
for (const key in row) {
436442
if (row.hasOwnProperty(key)) {
437443
const val = row[key];
438444
updateExpressions.push(`#${key} = :${key}`);
439445
expressionAttributeValues[`:${key}`] = val;
440-
expressionAttributeNames[`#${key}`] = key;
446+
ExpressionAttributeNames[`#${key}`] = key;
441447
}
442448
}
443449

444-
return updateTableRow(
445-
TableName,
446-
keys,
447-
`SET ${updateExpressions.join(', ')}`,
450+
return updateTableRow(TableName, keys, {
451+
UpdateExpression: `SET ${updateExpressions.join(', ')}`,
448452
expressionAttributeValues,
449-
expressionAttributeNames,
450-
);
453+
ExpressionAttributeNames,
454+
ConditionExpression,
455+
});
451456
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dynadash",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "DynamoDb helpers",
55
"main": "dist/dynadash.js",
66
"typings": "dist/types/dynadash.d.ts",

0 commit comments

Comments
 (0)