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(core): hydrate collection items when merging data in `factory.cre…
…ate` Closes mikro-orm#4173
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
export class User { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@OneToMany(() => Post, post => post.user) | ||
posts = new Collection<Post>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@OneToMany(() => Comment, comment => comment.post) | ||
comments = new Collection<Comment>(this); | ||
|
||
@ManyToOne() | ||
user!: User; | ||
|
||
} | ||
|
||
@Entity() | ||
class Comment { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@ManyToOne() | ||
post!: Post; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [Comment, Post, User], | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
orm.em.create(User, { id: 'user1' }); | ||
orm.em.create(Post, { id: 'post1', user: 'user1' }); | ||
orm.em.create(Comment, { id: 'comment', post: 'post1' }); | ||
await orm.em.flush(); | ||
}); | ||
|
||
beforeEach(() => orm.em.clear()); | ||
afterAll(() => orm.close(true)); | ||
|
||
test('it should select comments', async () => { | ||
const users2 = await orm.em.qb(User) | ||
.select('*') | ||
.joinAndSelect('posts', 'p') | ||
.leftJoinAndSelect('p.comments', 'c'); | ||
expect(users2[0].posts[0].comments[0]).toBeDefined(); | ||
}); | ||
|
||
test('it should select comments even if posts have already been selected', async () => { | ||
await orm.em.qb(Post).select('*'); | ||
const users1 = await orm.em.qb(User) | ||
.select('*') | ||
.joinAndSelect('posts', 'p') | ||
.leftJoinAndSelect('p.comments', 'c'); | ||
expect(users1[0].posts[0].comments[0]).toBeDefined(); | ||
}); |