diff --git a/src/bone.js b/src/bone.js index 80084148..9f15d13c 100644 --- a/src/bone.js +++ b/src/bone.js @@ -546,9 +546,9 @@ class Bone { const value = attribute.uncast(this.#raw[name]); if (this.#rawSaved[name] !== undefined) { this.#rawPrevious[name] = this.#rawSaved[name]; - } else if (!changes && this.#rawPrevious[name] === undefined) { + } else if (!changes && this.#rawPrevious[name] === undefined && this.#raw[name] != null) { // first persisting - this.#rawPrevious[name] = attribute.cast(value); + this.#rawPrevious[name] = null; } this.#rawSaved[name] = attribute.cast(value); } @@ -735,7 +735,7 @@ class Bone { const spell = new Spell(Model, opts).$insert(data); return spell.later(result => { this[primaryKey] = result.insertId; - this.#rawSaved[primaryKey] = null; + // this.#rawSaved[primaryKey] = null; this.syncRaw(); return this; }); diff --git a/test/integration/suite/basics.test.js b/test/integration/suite/basics.test.js index 12f37fc8..39eea28b 100644 --- a/test/integration/suite/basics.test.js +++ b/test/integration/suite/basics.test.js @@ -160,8 +160,8 @@ describe('=> Basic', () => { expect(post.previousChanged('extra')).to.be(false); post.extra = { greeting: 'hi' }; await post.save(); - // should return false after first persisting - expect(post.previousChanged('extra')).to.be(false); + // should return true after first persisting + expect(post.previousChanged('extra')).to.be(true); post.extra = { greeting: 'ohayo' }; await post.save(); // should return true after updating @@ -177,7 +177,7 @@ describe('=> Basic', () => { post.extra = { greeting: 'hi' }; await post.save(); // isPrivate has default value in DSL - assert.deepEqual(post.previousChanged(), [ 'id' ]); + assert.deepEqual(post.previousChanged().sort(), [ 'id', 'extra', 'isPrivate', 'title', 'wordCount', 'createdAt', 'updatedAt' ].sort()); post.extra = { greeting: 'ohayo' }; await sleep(10); await post.save(); @@ -195,7 +195,7 @@ describe('=> Basic', () => { const post = new Post({ title: 'Untitled' }); assert.deepEqual(post.previousChanges('title'), {}); await post.save(); - assert.deepEqual(post.previousChanges('title'), {}); + assert.deepEqual(post.previousChanges('title'), { title: [ null, 'Untitled' ] }); assert.deepEqual(post.previousChanges('authorId'), { }); post.title = 'MHW'; post.authorId = 100; @@ -210,7 +210,14 @@ describe('=> Basic', () => { const post = new Post({ title: 'Untitled' }); assert.deepEqual(post.previousChanges(), {}); await post.save(); - assert.deepEqual(post.previousChanges(), { id: [ null, post.id ] }); + assert.deepEqual(post.previousChanges(), { + id: [ null, post.id ], + createdAt: [ null, post.createdAt ], + isPrivate: [ null, Post.driver.type === 'mysql'? 0 : false ] , + title: [ null, 'Untitled' ], + updatedAt: [ null, post.updatedAt ], + wordCount: [ null, 0 ], + }); post.title = 'MHW'; post.authorId = 100; const prevUpdatedAt = post.updatedAt; diff --git a/test/unit/adapters/sequelize.test.js b/test/unit/adapters/sequelize.test.js index ab35a696..be779476 100644 --- a/test/unit/adapters/sequelize.test.js +++ b/test/unit/adapters/sequelize.test.js @@ -1016,7 +1016,7 @@ describe('=> Sequelize adapter', () => { it('model.previous(key)', async () => { const post = await Post.create({ title: 'By three they come' }); post.title = 'Hello there'; - assert.equal(post.previous('title'), 'By three they come'); + assert.equal(post.previous('title'), null); await post.update(); assert.equal(post.previous('title'), 'By three they come'); }); @@ -1025,35 +1025,21 @@ describe('=> Sequelize adapter', () => { const post = await Post.create({ title: 'By three they come' }); post.title = 'Hello there'; assert.deepEqual(post.previous(), { - title: 'By three they come', + title: null, id: null, - isPrivate: false, - updatedAt: post.updatedAt, - createdAt: post.createdAt, - wordCount: 0, - authorId: null, - content: null, - deletedAt: null, - extra: null, - settings: null, - summary: null, - thumb: null, + isPrivate: null, + updatedAt: null, + createdAt: null, + wordCount: null, }); post.content = 'a'; assert.deepEqual(post.previous(), { - title: 'By three they come', + title: null, id: null, - isPrivate: false, - updatedAt: post.updatedAt, - createdAt: post.createdAt, - wordCount: 0, - authorId: null, - content: null, - deletedAt: null, - extra: null, - settings: null, - summary: null, - thumb: null, + isPrivate: null, + updatedAt: null, + createdAt: null, + wordCount: null, }); const prevUpdatedAt = post.updatedAt; await post.update();