Last active
September 28, 2020 00:37
-
-
Save mtimbs/bd057782e78e852f41e7c42c4d23178b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface DynamoUpdateParams { | |
UpdateExpression: string; | |
ExpressionAttributeNames: Record<string, string>; | |
ExpressionAttributeValues: Record<string, unknown>; | |
} | |
export const DynamicUpdateExpressionFromObject = (map: Record<string, unknown>): DynamoUpdateParams => { | |
const ExpressionAttributeNames: Record<string, string> = Object.keys(map) | |
.reduce((acc, curr) => ({ ...acc, ...{ [`#${curr}`]: curr } }), {}); | |
const UpdateExpression: string = Object.values(ExpressionAttributeNames) | |
.reduce((acc, curr) => `${acc} #${curr} = :${curr},`, 'SET').slice(0, -1); | |
const ExpressionAttributeValues = Object.entries(map) | |
.reduce((acc, [key, value]) => ({ ...acc, ...{ [`:${key}`]: value } }), {}); | |
return { | |
UpdateExpression, | |
ExpressionAttributeNames, | |
ExpressionAttributeValues, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage