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: the mounted property type of arbitrary join fix #417 #418

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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').order('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%' },
}).order('posts.id');
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').order('posts.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%' },
}).order('posts.id');
assert.equal(posts.length, 1);
assert.ok(posts[0].users instanceof User);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#417 比较棘手的地方就在与 join(OtherModel) 的时候,这个关联的结果是应该返回一条还是多条,之前是只返回一条,从 join 语义上来说应该默认返回多条,改成数组,但是这会是个 breaking change

在你的用例里面,能 work 的原因是 comment association 可能提前声明过,所以会按照声明的关联关系 hasMany: true 变成多条;但是我有些担心这样是不是太隐晦了,相当于 .join() 的时候,对关联的数据类型不容易预期

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实,我这里是如果原本有 association 关系绑定的话就按 association 来处理,否则统一按返回单条非数组来处理

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯呐,也是一个办法,就是也是一个 breaking change,我明天去 poll 一下我这边用户们的意见 :-/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯呐,也是一个办法,就是也是一个 breaking change,我明天去 poll 一下我这边用户们的意见 :-/

其实也比较隐式。。join 的返回可能是数组可能是单条。返回的结果类型因为其他条件而变化了

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦对,我当时想到一个办法是加一个 joinMany(),后者返回数组,前者保持不变

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦对,我当时想到一个办法是加一个 joinMany(),后者返回数组,前者保持不变

好像 typeormsequelize 都是按我现在这个逻辑处理的

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不太一样吧?引用的这两个文档都是讲述关联关系配置的,也就是 leoric 的预知关联方式 https://leoric.js.org/zh/associations

.join() 方法的目的是用来编写临时的联表查询,期望结果一对一或者一对多,都是有可能的


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
4 changes: 2 additions & 2 deletions test/unit/spell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ describe('=> Spell', function() {
it('arbitrary join', function() {
assert.equal(
Post.join(Comment, 'comments.articleId = posts.id').toString(),
'SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `comments`.`article_id` = `posts`.`id` WHERE `posts`.`gmt_deleted` IS NULL'
'SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `posts`.`id` = `comments`.`article_id` AND `comments`.`gmt_deleted` IS NULL WHERE `posts`.`gmt_deleted` IS NULL'
);

assert.equal(Post.include('comments').limit(1).toSqlString(), heresql(function () {
Expand Down Expand Up @@ -615,7 +615,7 @@ describe('=> Spell', function() {

assert.equal(
Post.join(Comment, 'comments.articleId = posts.id').limit(1).toString(),
'SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `comments`.`article_id` = `posts`.`id` WHERE `posts`.`gmt_deleted` IS NULL LIMIT 1'
'SELECT `posts`.*, `comments`.* FROM `articles` AS `posts` LEFT JOIN `comments` AS `comments` ON `posts`.`id` = `comments`.`article_id` AND `comments`.`gmt_deleted` IS NULL WHERE `posts`.`gmt_deleted` IS NULL LIMIT 1'
);

});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"target": "ES2020",
},
"include": [
"src/**/*"
"src/**/*",
"test/**/*.ts"
],
"exclude": [
"dist",
Expand Down
Loading