Skip to content

Commit

Permalink
fix(belongsTo): set related many set correct relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 12, 2020
1 parent aa87408 commit d3db798
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
15 changes: 3 additions & 12 deletions src/Orm/Relations/BelongsTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,10 @@ export class BelongsTo implements RelationContract {
* Must be implemented by parent class
*/
public setRelatedMany (models: ModelContract[], related: ModelContract[]) {
/**
* Instead of looping over the model instances, we loop over the related model instances, since
* it can improve performance in some case. For example:
*
* - There are 10 parentInstances and we all of them to have one related instance, in
* this case we run 10 iterations.
* - There are 10 parentInstances and 8 of them have related instance, in this case we run 8
* iterations vs 10.
*/
related.forEach((one) => {
const relation = models.find((model) => model[this.foreignKey] === one[this.localKey])
models.forEach((model) => {
const relation = related.find((one) => one[this.localKey] === model[this.foreignKey])
if (relation) {
this.setRelated(relation, one)
this.setRelated(model, relation)
}
})
}
Expand Down
Binary file added test-helpers/tmp/db.sqlite
Binary file not shown.
49 changes: 47 additions & 2 deletions test/orm/model-belongs-to.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,53 @@ test.group('Model | Belongs To', (group) => {
public user: User
}

await db.insertQuery().table('users').insert([{ username: 'virk' }])

const users = await db.query().from('users')
await db.insertQuery().table('profiles').insert([
{
user_id: users[0].id,
display_name: 'virk',
},
{
user_id: users[0].id,
display_name: 'virk',
},
])

const profiles = await Profile.query().preload('user')
assert.instanceOf(profiles[0].user, User)
assert.instanceOf(profiles[1].user, User)
})

test('preload belongsTo relationship for many rows', async (assert) => {
class User extends BaseModel {
@column({ primary: true })
public id: number
}

class Profile extends BaseModel {
@column({ primary: true })
public id: number

@column()
public userId: number

@column()
public displayName: string

@belongsTo(() => User)
public user: User
}

await db.insertQuery().table('users').insert([{ username: 'virk' }, { username: 'nikk' }])

const users = await db.query().from('users')
await db.insertQuery().table('profiles').insert([
{
user_id: users[0].id,
display_name: 'virk',
},
{
user_id: users[0].id,
display_name: 'virk',
Expand All @@ -188,8 +231,10 @@ test.group('Model | Belongs To', (group) => {
},
])

const profile = await Profile.query().preload('user').where('display_name', 'virk').first()
assert.instanceOf(profile!.user, User)
const profiles = await Profile.query().preload('user')
assert.equal(profiles[0].user.id, 1)
assert.equal(profiles[1].user.id, 1)
assert.equal(profiles[2].user.id, 2)
})

test('raise exception when foreign key is not selected', async (assert) => {
Expand Down

0 comments on commit d3db798

Please sign in to comment.