Skip to content

Instantly share code, notes, and snippets.

@microadam
Created February 10, 2023 10:45
Show Gist options
  • Save microadam/44dd05d18f6998792c7ace740fe14489 to your computer and use it in GitHub Desktop.
Save microadam/44dd05d18f6998792c7ace740fe14489 to your computer and use it in GitHub Desktop.
dynamodb
const generateUpdateQueryExpression = (fields: Partial<TableStructure>) => {
const exp = {
UpdateExpression: "SET",
ExpressionAttributeNames: {} as ObjectWithProperties,
ExpressionAttributeValues: {} as ObjectWithProperties,
};
Object.entries(fields).forEach(([key, item]) => {
const keyParts = key.split(".");
const valueKey = keyParts[keyParts.length - 1];
const attributeNames = `#${keyParts.join(".#")}`;
exp.UpdateExpression += ` ${attributeNames} = :${valueKey},`;
for (const name of keyParts) {
exp.ExpressionAttributeNames[`#${name}`] = name;
}
exp.ExpressionAttributeValues[`:${valueKey}`] = marshall(item);
});
exp.UpdateExpression = exp.UpdateExpression.slice(0, -1);
return exp;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment