-
Notifications
You must be signed in to change notification settings - Fork 25
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
JimmyDaddy
wants to merge
2
commits into
master
Choose a base branch
from
fix/417-the-mounted-property-type-of-arbitrary-join
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
@@ -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([ | ||
|
@@ -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); | ||
}); | ||
|
||
it('Bone.group() subquery', async function() { | ||
const posts = await Post.find({ | ||
id: Comment.select('articleId').from( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
"target": "ES2020", | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
"src/**/*", | ||
"test/**/*.ts" | ||
], | ||
"exclude": [ | ||
"dist", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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() 的时候,对关联的数据类型不容易预期
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实,我这里是如果原本有 association 关系绑定的话就按 association 来处理,否则统一按返回单条非数组来处理
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯呐,也是一个办法,就是也是一个 breaking change,我明天去 poll 一下我这边用户们的意见 :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实也比较隐式。。join 的返回可能是数组可能是单条。返回的结果类型因为其他条件而变化了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦对,我当时想到一个办法是加一个 joinMany(),后者返回数组,前者保持不变
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像 typeorm 和 sequelize 都是按我现在这个逻辑处理的
There was a problem hiding this comment.
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() 方法的目的是用来编写临时的联表查询,期望结果一对一或者一对多,都是有可能的