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 8, 2022
1 parent 01f59b6 commit 5ddb84b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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 5ddb84b

Please sign in to comment.