Skip to content

Commit

Permalink
fix(core): hydrate collection items when merging data in `factory.cre…
Browse files Browse the repository at this point in the history
…ate`

Closes mikro-orm#4173
  • Loading branch information
B4nan authored and jsprw committed May 7, 2023
1 parent 41d5819 commit 0236000
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/issues/GH4173.test.ts
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();
});

0 comments on commit 0236000

Please sign in to comment.