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: skip connecting if models are synchronized already #200

Merged
merged 1 commit into from
Oct 18, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Node.js Package

on:
release:
types: [created]
types: [published]

jobs:
build:
Expand Down
3 changes: 3 additions & 0 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,9 @@ class Bone {
* @param {boolean} opts.hasMany
*/
static associate(name, opts = {}) {
if (name in this.associations) {
throw new Error(`duplicated association "${name}" on model ${this.name}`);
cyjake marked this conversation as resolved.
Show resolved Hide resolved
}
const { className } = opts;
const Model = this.models[className];
if (!Model) throw new Error(`unable to find model "${className}"`);
Expand Down
2 changes: 1 addition & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Realm {
}

if (models.length > 0) {
await loadModels(this.Bone, models, this.options);
await loadModels(this.Bone, models.filter(model => !model.synchronized), this.options);
}
this.connected = true;
return this.Bone;
Expand Down
6 changes: 6 additions & 0 deletions test/integration/suite/associations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe('=> Associations', function() {
]);
});

it('Bone.hasOne should throw if association exists', async function() {
assert.throws(function() {
Post.hasOne('attachment'); // exists
}, /duplicated association/);
});

it('Bone.hasOne', async function() {
const post = await Post.first.with('attachment');
expect(post.attachment).to.be.a(Attachment);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/realm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,4 +860,34 @@ describe('=> Realm', () => {
});
});
});

describe('realm.connect', function() {
/**
* If models are cached and connected already, skip connecting them again because it would raise issues like duplicated associations or redundant class property definition etc.
*/
it('should skip synchronized models', async function() {
class User extends Bone {}
const realm = new Realm({
port: process.env.MYSQL_PORT,
user: 'root',
database: 'leoric',
models: [ User ],
});
await realm.connect();

class Post extends Bone {
static table = 'articles'
}
await assert.doesNotReject(async function() {
const realm2 = new Realm({
port: process.env.MYSQL_PORT,
user: 'root',
database: 'leoric',
models: [ User, Post ],
});
await realm2.connect();
});
assert.ok(Post.synchronized);
});
});
});
42 changes: 28 additions & 14 deletions test/unit/spell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,32 +725,46 @@ describe('=> Spell', function() {

describe('silent should work', function() {
it('update', function () {
assert.equal(Post.update({ id: 1 }, { title: 'hello' }, { silent: true }).toString(),
"UPDATE `articles` SET `title` = 'hello' WHERE `id` = 1 AND `gmt_deleted` IS NULL");
assert.equal(
Post.update({ id: 1 }, { title: 'hello' }, { silent: true }).toString(),
"UPDATE `articles` SET `title` = 'hello' WHERE `id` = 1 AND `gmt_deleted` IS NULL"
);
});

it('increment, decrement', function () {
assert.equal(Book.find({ name: 'hello' }).increment('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` + 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(
Book.find({ name: 'hello' }).increment('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` + 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);

assert.equal(Book.find({ name: 'hello' }).decrement('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(
Book.find({ name: 'hello' }).decrement('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);

const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
const clock = sinon.useFakeTimers(fakeDate);
const spell = Book.find({ name: 'hello' });
spell.silent = false;
assert.equal(spell.decrement('price', 1).toString(),
"UPDATE `books` SET `price` = `price` - 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(spell.decrement('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(
spell.decrement('price', 1).toString(),
"UPDATE `books` SET `price` = `price` - 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);
assert.equal(
spell.decrement('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);

const spell1 = Book.find({ name: 'hello' });
spell1.silent = true;
assert.equal(spell1.decrement('price', 1).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(spell1.decrement('price', 1, { silent: false }).toString(),
"UPDATE `books` SET `price` = `price` - 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
assert.equal(
spell1.decrement('price', 1).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);
assert.equal(
spell1.decrement('price', 1, { silent: false }).toString(),
"UPDATE `books` SET `price` = `price` - 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `name` = 'hello' AND `gmt_deleted` IS NULL"
);
clock.restore();
});
});
Expand Down