Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create instance dirty check rule fix #290

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 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 @@ -681,7 +681,7 @@ class Bone {
for (const key in changes) {
this.attribute(key, changes[key]);
}
this.syncRaw(changes);
this.syncRaw();
return result.affectedRows;
});
}
Expand Down Expand Up @@ -735,6 +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.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.equal(post.previousChanged(), false);
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(), {});
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
40 changes: 13 additions & 27 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',
id: post.id,
isPrivate: false,
updatedAt: post.updatedAt,
createdAt: post.createdAt,
wordCount: 0,
authorId: null,
content: null,
deletedAt: null,
extra: null,
settings: null,
summary: null,
thumb: null,
title: null,
id: null,
isPrivate: null,
updatedAt: null,
createdAt: null,
wordCount: null,
});
post.content = 'a';
assert.deepEqual(post.previous(), {
title: 'By three they come',
id: post.id,
isPrivate: false,
updatedAt: post.updatedAt,
createdAt: post.createdAt,
wordCount: 0,
authorId: null,
content: null,
deletedAt: null,
extra: null,
settings: null,
summary: null,
thumb: null,
title: null,
id: null,
isPrivate: null,
updatedAt: null,
createdAt: null,
wordCount: null,
});
const prevUpdatedAt = post.updatedAt;
await post.update();
Expand Down