Skip to content

Commit

Permalink
Improving Base model timestamp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwalter committed Jun 10, 2021
1 parent 2111038 commit 6d087e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-crews-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ianwalter/nrg": patch
---

Improving Base model timestamp logic
9 changes: 4 additions & 5 deletions packages/nrg/lib/models/Base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Model } = require('objection')
const { Model, raw } = require('objection')
const { nanoid } = require('nanoid')

module.exports = class Base extends Model {
Expand All @@ -8,16 +8,15 @@ module.exports = class Base extends Model {
}

$beforeInsert () {
const timestamp = new Date().toISOString()
this.createdAt = timestamp
this.updatedAt = timestamp
this.createdAt = raw('NOW()')
this.updatedAt = raw('NOW()')

// If no primary key value was specified, generate one using nanoid.
const idColumn = this.constructor.idColumn
if (!this[idColumn]) this[idColumn] = nanoid()
}

$beforeUpdate () {
this.updatedAt = new Date().toISOString()
this.updatedAt = raw('NOW()')
}
}

0 comments on commit 6d087e8

Please sign in to comment.