cdklabs / cdklabs/awscdk-appsync-utils
Adde a helper function to @aws-appsync/utils that creates the update expression for you
- Dominant language
- TypeScript
- Stars
- 26
- Forks
- 6
- Avg merge
- 9m
- Merged PRs (30d)
- 4
Description
Hi,
Can someone add a function to the @appsync/utils dynamodb utils that does something like this in runtime:
```
export function createUpdateValues(data: any, replace?: string[]) {
let updateExpression = 'set';
let removeExpression = '';
const removeExpressions: string[] = [];
const expressionAttributeValues: Dictionary = {};
const expressionAttributeNames: Dictionary = {};
// loop through data and create alias and update expression
for (const [index, [key, value]] of Object.entries(Object.entries(data))) {
// Remove undefined values from Database record.
if (value === undefined) {
const expName = `#${key}`;
removeExpressions.push(expName)
expressionAttributeNames[expName] = key;
continue;
}
const alias = getLetter(parseInt(index, 10));
expressionAttributeNames[`#${alias}`] = key;
const replaceList = Array.isArray(replace) && replace.includes(key);
if (value instanceof Array && !replaceList) {
expressionAttributeValues[`:${alias}`] = value;
expressionAttributeValues[`:empty_list`] = [];
updateExpression = `${updateExpression} #${alias} = list_append(if_not_exists(#${alias}, :empty_list), :${alias}), `;
} else {
expressionAttributeValues[`:${alias}`] = value;
updateExpression = `${updateExpression} #${alias} = :${alias}, `;
}
}
// remove trailing comma
const removeTrailingCommaRegex = new RegExp(/,\s*$/);
updateExpression = updateExpression.replace(removeTrailingCommaRegex, '');
if (removeExpressions.length) {
removeExpression = `REMOVE ${removeExpressions.join(',').replace(removeTrailingCommaRegex, '')}`
}
const expression = `${updateExpression} ${removeExpression}`;
return {
update: {
expression,
expressionNames: expressionAttributeNames,
expressionValues: marshall(expressionAttributeValues, { removeUndefinedValues: true }),
}
}
}
```
that way we don't have create our own update expressions it the resolver, it is ugly and unnecessary.
Cheers
Contributor guide
Research direction
No file or test is named; start by locating the @appsync/utils DynamoDB utilities and their runtime tests. Implement and document the requested createUpdateValues helper, then verify that it builds update and remove expressions, aliases names and values, handles arrays and undefined values, and returns the expected marshalled structure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- api, databases
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100