Skip to content

Commit

Permalink
fix: create instance dirty check rule fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Mar 9, 2022
1 parent 01f59b6 commit 2f2a528
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,18 @@ class Bone {
* Sync changes made in {@link Bone.raw} back to {@link Bone.rawSaved}.
* @private
*/
syncRaw(changes) {
syncRaw() {
const { attributes } = this.constructor;
this.isNewRecord = false;
for (const name of Object.keys(changes || attributes)) {
for (const name of Object.keys(attributes)) {
const attribute = attributes[name];
// Take advantage of uncast/cast to create new copy of value
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 (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);
}
Expand Down Expand Up @@ -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;
});
Expand Down
17 changes: 12 additions & 5 deletions test/integration/suite/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down
36 changes: 11 additions & 25 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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();
Expand Down

0 comments on commit 2f2a528

Please sign in to comment.