Skip to content

Commit

Permalink
refactor: rename "getDeletedAttributes" to "getRemovedAttributes"
Browse files Browse the repository at this point in the history
This keeps the terminology in line with DynamoDB, which uses "remove" to
indicate an attribute should be removed from the document and "delete"
to either delete an item from the database or remove an item from a Set
attribute.
  • Loading branch information
benhutchins committed Oct 31, 2023
1 parent 968fecc commit 04ea1ab
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ export interface BeforeSaveEvent<T extends Table> extends SaveEvent<T> {
export interface AfterSaveEvent<T extends Table> extends BeforeSaveEvent<T> {
output: PutItemCommandOutput | UpdateItemCommandOutput
originalValues: AttributeMap
deletedAttributes: string[]
removedAttributes: string[]
updatedAttributes: string[]

/**
* @deprecated
*/
deletedAttributes: string[]
}

export interface DeleteEvent<T extends Table> extends BaseEvent<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/query/update-item-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function getUpdateItemInput<T extends Table>(record: T, params?: UpdateIt
}
})

_.each(_.uniq(record.getDeletedAttributes()), (attrName, i) => {
_.each(_.uniq(record.getRemovedAttributes()), (attrName, i) => {
const slug = `#DA${valueCounter}`
attributeNameMap[slug] = attrName
removes.push(slug)
Expand Down
2 changes: 1 addition & 1 deletion src/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Table', () => {
card.testString = ''
expect(card.testString).to.eq(null, 'cleared strings become null, because DynamoDB does not allow empty string values')
expect(card.getUpdatedAttributes()).to.deep.eq([])
expect(card.getDeletedAttributes()).to.deep.eq(['testString'])
expect(card.getRemovedAttributes()).to.deep.eq(['testString'])
await card.save()

const reloadedCard = await TestableTable.primaryKey.get(10, '100')
Expand Down
22 changes: 16 additions & 6 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class Table {
* Get the list of attributes pending update.
*
* The result includes attributes that have also been deleted. To get just
* the list of attributes pending deletion, use {@link Table.getDeletedAttributes}.
* the list of attributes pending removal, use {@link Table.getRemovedAttributes}.
*
* If you want to easily know if this record has updates pending, use {@link Table.hasChanges}.
*/
Expand All @@ -269,16 +269,25 @@ export class Table {
}

/**
* Get the list of attributes pending deletion.
* Get the list of attributes pending removal.
*
* To get all the attributes that have been updated, use {@link Table.getUpdatedAttributes}.
*
* If you want to easily know if this record has updates pending, use {@link Table.hasChanges}.
*/
public getDeletedAttributes(): string[] {
public getRemovedAttributes(): string[] {
return [...this.__removedAttributes]
}

/**
* Use getRemovedAttributes.
*
* @deprecated
*/
public getDeletedAttributes(): string[] {
return this.getRemovedAttributes()
}

/**
* While similar to setAttributes, this method runs the attribute's defined fromJSON
* methods to help standardize the attribute values as much as possible.
Expand Down Expand Up @@ -393,7 +402,7 @@ export class Table {
// track that this value was updated
this.__updatedAttributes.add(attributeName)

// ensure the attribute is not marked for being deleted
// ensure the attribute is not marked for removal
this.__removedAttributes.delete(attributeName)

return this
Expand Down Expand Up @@ -560,7 +569,7 @@ export class Table {
// grab the current changes to pass to the afterSave event
const originalValues = this.getOriginalValues()
const updatedAttributes = this.getUpdatedAttributes()
const deletedAttributes = this.getDeletedAttributes()
const removedAttributes = this.getRemovedAttributes()

// reset internal tracking of changes attributes
this.__putRequired = false
Expand All @@ -575,7 +584,8 @@ export class Table {
output,
originalValues,
updatedAttributes,
deletedAttributes,
removedAttributes,
deletedAttributes: removedAttributes,
})

if (beforeSaveEvent.returnOutput === true) {
Expand Down

0 comments on commit 04ea1ab

Please sign in to comment.