forked from mikro-orm/mikro-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(knex): fix populating M:N from inverse side with joined strategy
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Collection, Entity, LoadStrategy, PrimaryKey, ManyToMany } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToMany(() => Car) | ||
cars = new Collection<Car>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class Car { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToMany(() => User, u => u.cars) | ||
users = new Collection<User>(this); | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [User], | ||
loadStrategy: LoadStrategy.JOINED, | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('loading M:N via em.populate from inverse side with joined strategy', async () => { | ||
const u1 = orm.em.create(User, {}); | ||
const u2 = orm.em.create(User, {}); | ||
const c1 = orm.em.create(Car, {}); | ||
const c2 = orm.em.create(Car, {}); | ||
u1.cars.add(c1); | ||
u1.cars.add(c2); | ||
u2.cars.add(c1); | ||
u2.cars.add(c2); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
const cars = await orm.em.find(Car, {}); | ||
await orm.em.populate(cars, ['users']); | ||
|
||
expect(cars[0].users.toArray()).toEqual([ | ||
{ id: 1 }, | ||
{ id: 2 }, | ||
]); | ||
expect(cars[1].users.toArray()).toEqual([ | ||
{ id: 1 }, | ||
{ id: 2 }, | ||
]); | ||
}); |