Created
February 10, 2023 10:45
-
-
Save microadam/44dd05d18f6998792c7ace740fe14489 to your computer and use it in GitHub Desktop.
dynamodb
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
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