From 5c2b1803d2b72ac1afb3d11e70cf603653213ddc Mon Sep 17 00:00:00 2001 From: thetutlage Date: Fri, 21 Dec 2018 13:30:41 +0530 Subject: [PATCH] feat(model): add support for pre-defining timestamp values --- src/Lucid/Model/index.js | 4 ++-- src/Lucid/QueryBuilder/index.js | 10 +++++++++ test/unit/lucid.spec.js | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Lucid/Model/index.js b/src/Lucid/Model/index.js index c98a5c7b..629d4eab 100644 --- a/src/Lucid/Model/index.js +++ b/src/Lucid/Model/index.js @@ -529,7 +529,7 @@ class Model extends BaseModel { */ _setCreatedAt (values) { const createdAtColumn = this.constructor.createdAtColumn - if (createdAtColumn) { + if (createdAtColumn && !values[createdAtColumn]) { values[createdAtColumn] = this._getSetterValue(createdAtColumn, new Date()) } } @@ -548,7 +548,7 @@ class Model extends BaseModel { */ _setUpdatedAt (values) { const updatedAtColumn = this.constructor.updatedAtColumn - if (updatedAtColumn) { + if (updatedAtColumn && !this.dirty[updatedAtColumn]) { values[updatedAtColumn] = this._getSetterValue(updatedAtColumn, new Date()) } } diff --git a/src/Lucid/QueryBuilder/index.js b/src/Lucid/QueryBuilder/index.js index 007d9207..b12491df 100644 --- a/src/Lucid/QueryBuilder/index.js +++ b/src/Lucid/QueryBuilder/index.js @@ -518,6 +518,16 @@ class QueryBuilder { const valuesCopy = _.clone(valuesOrModelInstance) const fakeModel = new this.Model() + + /** + * Here we fill attributes on the model, so that the logic to + * find if `updated_at` should be set or not can be + * evaluated by the model instance. + * + * For this model instance relies on `dirty` values. + */ + fakeModel.fill(valuesCopy) + fakeModel._setUpdatedAt(valuesCopy) fakeModel._formatDateFields(valuesCopy) diff --git a/test/unit/lucid.spec.js b/test/unit/lucid.spec.js index 31a8e506..54ec2b9f 100644 --- a/test/unit/lucid.spec.js +++ b/test/unit/lucid.spec.js @@ -2028,4 +2028,44 @@ test.group('Model', (group) => { assert.equal(userQuery.sql, helpers.formatQuery('select * from "users" where (exists (select * from "profiles" where "users"."id" = "profiles"."user_id")) limit ?')) }) + + test('do not set created_at when explicitly set in values', async (assert) => { + class User extends Model { + } + + User._bootIfNotBooted() + const createdAt = moment().subtract('1', 'day').format('YYYY-MM-DD HH:mm:ss') + + const user = await User.create({ username: 'virk', created_at: createdAt }) + await user.reload() + + assert.equal(user.created_at, createdAt) + }) + + test('do not set updated_at when explicitly set in values', async (assert) => { + class User extends Model { + } + + User._bootIfNotBooted() + const updatedAt = moment().subtract('1', 'day').format('YYYY-MM-DD HH:mm:ss') + + const user = await User.create({ username: 'virk', updated_at: updatedAt }) + await user.reload() + + assert.equal(user.updated_at, updatedAt) + }) + + test('do not set updated_at when calling update on query builder', async (assert) => { + class User extends Model { + } + + User._bootIfNotBooted() + const updatedAt = moment().subtract('1', 'day').format('YYYY-MM-DD HH:mm:ss') + + const user = await User.create({ username: 'virk' }) + await User.query().where('username', 'virk').update({ updated_at: updatedAt }) + await user.reload() + + assert.equal(user.updated_at, updatedAt) + }) })