Skip to content

Commit

Permalink
fix: using a conditional check on a nowOnUpdate Date attribute
Browse files Browse the repository at this point in the history
We need to remove the legacy logic for trying to ensure we always update
a Date with "nowOnUpdate" enabled. We now have a method on the table
called "updateOnSaveAttributes" that ensures properties that need to
self-check themselves do so, so this check is not necessary. By removing
this, it allows you to use a conditional check comparison (such as in a
transaction) against the date's previous value.
  • Loading branch information
benhutchins committed Nov 16, 2023
1 parent 04ea1ab commit 074fe81
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/decorator/attribute-types/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ describe('AttributeType/Date', () => {
await new Promise((resolve) => setTimeout(resolve, 2000))

// save again
const updatedAtBeforeSave = record.updatedAt
await record.save({ force: true }) // using force save so it saves, ignoring the fact there are no changes

expect(record.updatedAt).to.be.a('date')
expect(record.updatedAt).to.be.at.least(later)
expect(record.updatedAt).to.be.at.within(later, new Date())
expect(record.updatedAt).to.not.eq(updatedAtBeforeSave)

expect(record.getAttributeDynamoValue('createdAt')).to.deep.eq({
S: record.createdAt.toISOString(),
Expand Down
6 changes: 1 addition & 5 deletions src/decorator/attribute-types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export class DateAttributeType extends AttributeType<Value, Metadata> implements
}

toDynamo(dt: Value): AttributeValue {
if (this.metadata?.nowOnUpdate === true) {
dt = new Date()
}

if (this.metadata?.unixTimestamp === true || this.metadata?.millisecondTimestamp === true || this.metadata?.timeToLive === true) {
return {
N: this.parseDate(dt).toString(),
Expand Down Expand Up @@ -114,7 +110,7 @@ export class DateAttributeType extends AttributeType<Value, Metadata> implements
// parse YYYY-MM-DD and ensure we create the Date object in UTC
const b = dt.split('-').map((d) => parseInt(d, 10))
dt = new Date(Date.UTC(b[0], --b[1], b[2]))
// if timestamp, assume the value is a timestamp
// if configured as a timestamp, assume the value is a numeric string as a timestamp
} else if (this.metadata?.unixTimestamp === true || this.metadata?.timeToLive === true) {
dt = new Date(stringToNumber(dt) * 1000)
} else if (this.metadata?.millisecondTimestamp === true) {
Expand Down

0 comments on commit 074fe81

Please sign in to comment.