From 0f444abc4a9f3e367017ab38febabacee5900c35 Mon Sep 17 00:00:00 2001 From: Benjamin Hutchins Date: Sat, 14 Oct 2023 02:30:48 -0400 Subject: [PATCH] fix: store original value when removing an attribute --- src/table.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/table.ts b/src/table.ts index b83b7bd5..e353f6f1 100644 --- a/src/table.ts +++ b/src/table.ts @@ -384,10 +384,8 @@ export class Table { * To set the value from a JavaScript object, use {@link Table.setAttribute} */ public setAttributeDynamoValue(attributeName: string, attributeValue: AttributeValue): this { - // save the original value before we update the attributes value - if (!_.isUndefined(this.__attributes[attributeName]) && _.isUndefined(this.__original[attributeName])) { - this.__original[attributeName] = this.getAttributeDynamoValue(attributeName) - } + // store the original value + this.saveOriginalValue(attributeName) // store the new value this.__attributes[attributeName] = attributeValue @@ -439,6 +437,7 @@ export class Table { for (const attributeName of attributeNames) { // delete the attribute as long as it existed and wasn't already null if (!_.isNil(this.__attributes[attributeName]) || !this.__entireDocumentIsKnown) { + this.saveOriginalValue(attributeName) this.__attributes[attributeName] = { NULL: true } this.__removedAttributes.add(attributeName) this.__updatedAttributes.delete(attributeName) @@ -711,7 +710,7 @@ export class Table { const attributeValue = attribute.toDynamo(value) // avoid recording the value if it is unchanged, so we do not send it as an updated value during a save - if (params.force !== true && !_.isUndefined(this.__attributes[attribute.name]) && _.isEqual(this.__attributes[attribute.name], attributeValue)) { + if (params.force !== true && !_.isNil(this.__attributes[attribute.name]) && _.isEqual(this.__attributes[attribute.name], attributeValue)) { return this } @@ -731,6 +730,13 @@ export class Table { return value } + protected saveOriginalValue(attributeName: string): void { + // save the original value before we remove the attribute's value + if (!_.isNil(this.__attributes[attributeName]) && _.isNil(this.__original[attributeName])) { + this.__original[attributeName] = this.getAttributeDynamoValue(attributeName) + } + } + /** * Returns a list of attributes that should not be allowed when Table.fromJSON is used. */