Skip to content

Commit

Permalink
fix: store original value when removing an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
benhutchins committed Oct 14, 2023
1 parent d238cfa commit 0f444ab
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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.
*/
Expand Down

0 comments on commit 0f444ab

Please sign in to comment.