Skip to content

Commit 3b944ed

Browse files
NickColleygr2m
authored andcommitted
feat(timestamps): Add timestamps addedAt, updatedAt and createdAt
1 parent b82c1c4 commit 3b944ed

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

helpers/update-many.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var changeObject = require('../utils/change-object')
66
var toDoc = require('../utils/to-doc')
77
var addTimestamps = require('../utils/add-timestamps')
88
var toId = require('../utils/to-id')
9+
910
var findMany = require('./find-many')
1011

1112
module.exports = function updateMany (array, change) {

tests/specs/update.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ test('store.update(array, updateFunction)', function (t) {
276276
})
277277

278278
test('store.update(object) updates updatedAt timestamp', function (t) {
279-
t.plan(4)
279+
t.plan(5)
280280

281281
var clock = lolex.install(0, ['Date'])
282282
var db = dbFactory()
@@ -300,6 +300,7 @@ test('store.update(object) updates updatedAt timestamp', function (t) {
300300

301301
store.on('update', function (object) {
302302
t.is(object.id, 'shouldHaveTimestamps', 'resolves doc')
303+
t.is(typeof object.deletedAt, 'undefined', 'deletedAt shouldnt be set')
303304
t.ok(isValidDate(object.updatedAt), 'updatedAt should be a valid date')
304305
t.is(now(), object.updatedAt, 'updatedAt should be the same time as right now')
305306
t.not(object.createdAt, object.updatedAt, 'createdAt and updatedAt should not be the same')
@@ -309,7 +310,7 @@ test('store.update(object) updates updatedAt timestamp', function (t) {
309310
})
310311

311312
test('store.update([objects]) updates updatedAt timestamps', function (t) {
312-
t.plan(8)
313+
t.plan(10)
313314

314315
var clock = lolex.install(0, ['Date'])
315316
var db = dbFactory()
@@ -334,6 +335,7 @@ test('store.update([objects]) updates updatedAt timestamps', function (t) {
334335

335336
store.on('update', function (object) {
336337
t.ok(object.id, 'resolves doc')
338+
t.is(typeof object.deletedAt, 'undefined', 'deletedAt shouldnt be set')
337339
t.ok(isValidDate(object.updatedAt), 'updatedAt should be a valid date')
338340
t.is(now(), object.updatedAt, 'updatedAt should be the same time as right now')
339341
t.not(object.createdAt, object.updatedAt, 'createdAt and updatedAt should not be the same')

utils/add-timestamps.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
var now = require('./now')
4+
5+
module.exports = function addTimestamps (object) {
6+
object.updatedAt = now()
7+
object.createdAt = object.createdAt || object.updatedAt
8+
9+
if (object._deleted) {
10+
object.deletedAt = object.deletedAt || object.updatedAt
11+
}
12+
13+
return object
14+
}

utils/now.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function now () {
2+
return new Date().toISOString()
3+
}

0 commit comments

Comments
 (0)