Skip to content

Commit

Permalink
fix: the mounted property type of arbitrary join fix #417
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Feb 1, 2024
1 parent 47c56e6 commit a527d45
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,14 @@ class Spell {
if (qualifier in joins) {
throw new Error(`invalid join target. ${qualifier} already defined.`);
}
joins[qualifier] = { Model, on: parseConditions(Model, onConditions, ...values)[0] };

const association = this.Model.associations[qualifier] || this.Model.associations[pluralize(qualifier, 1)];

if (!association) {
joins[qualifier] = { Model, on: parseConditions(Model, onConditions, ...values)[0] };
} else {
joinAssociation(this, this.Model, this.Model.tableAlias, qualifier, { onConditions, values });
}
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions test/integration/suite/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ describe('=> Basic', () => {
}, /unable to use virtual attribute realname as condition in model User/);

await assert.rejects(async () => {
Post.hasOne('user', {
Post.belongsTo('user', {
foreignKey: 'realname'
});
}, /unable to use virtual attribute realname as foreign key in model User/);

Post.hasOne('user', {
Post.belongsTo('user', {
foreignKey: 'authorId'
});

Expand Down
53 changes: 49 additions & 4 deletions test/integration/suite/querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Like = require('../../models/like');
const Post = require('../../models/post');
const Tag = require('../../models/tag');
const TagMap = require('../../models/tagMap');
const User = require('../../models/user');
const { logger } = require('../../../src/utils');

describe('=> Query', function() {
Expand Down Expand Up @@ -502,11 +503,14 @@ describe('=> Count / Group / Having', function() {
describe('=> Group / Join / Subqueries', function() {
before(async function() {
await Post.remove({}, true);
await User.remove({}, true);
const user = await User.create({ name: 'Tyrael', nickname: 'Tyrael', email: '[email protected]' });
const user1 = await User.create({ name: 'Lazarus', nickname: 'Lazarus', email: '[email protected]' });
const posts = await Post.bulkCreate([
{ id: 1, title: 'New Post' },
{ id: 2, title: 'Archbishop Lazarus' },
{ id: 3, title: 'Archangel Tyrael' },
{ id: 4, title: 'New Post 2' },
{ id: 1, title: 'New Post', authorId: user.id },
{ id: 2, title: 'Archbishop Lazarus', authorId: user1.id },
{ id: 3, title: 'Archangel Tyrael', authorId: user.id },
{ id: 4, title: 'New Post 2', authorId: user1.id },
]);

await Attachment.bulkCreate([
Expand All @@ -530,6 +534,47 @@ describe('=> Group / Join / Subqueries', function() {
]);
});

it('Bone.find().join() with association', async function() {
// https://github.com/cyjake/leoric/issues/417
// SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `comments`.`article_id` = `posts`.`id` WHERE `posts`.`gmt_deleted` IS NULL
assert.equal(Post.find().join(Comment, 'comments.articleId = posts.id').toSqlString(), Post.include('comments').toSqlString());
const posts = await Post.find().join(Comment, 'comments.articleId = posts.id');
assert.equal(posts.length, 4);

assert.equal(posts[0].comments.length, 0);
assert.equal(posts[1].comments.length, 2);
assert.equal(posts[2].comments.length, 1);
assert.equal(posts[3].comments.length, 0);
assert.ok(posts[1].comments[0] instanceof Comment);
});

it('Bone.find().join().limit() with association', async function() {
// https://github.com/cyjake/leoric/issues/417
const posts = await Post.find().limit(1).join(Comment, 'comments.articleId = posts.id').where({
'posts.title': { $like: 'Archb%' },
});
assert.equal(posts.length, 1);
assert.ok(posts[0].comments[0] instanceof Comment);
});

it('Bone.find().join() without association', async function() {
// https://github.com/cyjake/leoric/issues/417
const posts = await Post.find().join(User, 'posts.authorId = users.id');
assert.equal(posts.length, 4);

assert.ok(posts[0].users);
assert.ok(posts[1].users instanceof User);
});

it('Bone.find().join().limit() without association', async function() {
// https://github.com/cyjake/leoric/issues/417
const posts = await Post.find().limit(1).join(User, 'posts.authorId = users.id').where({
'posts.title': { $like: 'Archb%' },
});
assert.equal(posts.length, 1);
assert.ok(posts[0].users instanceof User);
});

it('Bone.group() subquery', async function() {
const posts = await Post.find({
id: Comment.select('articleId').from(
Expand Down
4 changes: 2 additions & 2 deletions test/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function run {
args=("${args[@]:1}");
fi
echo "";
printf '"%s" ' "${args[@]}" | xargs echo "> DEBUG=leoric mocha --node-option require=ts-node/register,enable-source-maps -R dot --exit --timeout 5000 ${file}";
printf '"%s" ' "${args[@]}" | DEBUG=leoric xargs mocha --node-option require=ts-node/register,enable-source-maps -R dot --exit --timeout 5000 ${file} || exit $?;
printf '"%s" ' "${args[@]}" | xargs echo "> mocha --node-option require=ts-node/register,enable-source-maps -R spec --exit --timeout 5000 ${file}";
printf '"%s" ' "${args[@]}" | xargs mocha --node-option require=ts-node/register,enable-source-maps -R spec --exit --timeout 5000 ${file} || exit $?;
}

##
Expand Down

0 comments on commit a527d45

Please sign in to comment.