-
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): register entity to identity map as early as possible
Closes #2777
- Loading branch information
Showing
4 changed files
with
107 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
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,103 @@ | ||
import { Entity, LoadStrategy, ManyToOne, MikroORM, OneToOne, PrimaryKey, Property, wrap } from '@mikro-orm/core'; | ||
|
||
@Entity() | ||
export class Image { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@OneToOne(() => Customer) | ||
customer!: any; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Product { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
title!: string; | ||
|
||
@ManyToOne(() => Image, { | ||
strategy: LoadStrategy.JOINED, | ||
eager: true, | ||
nullable: true, | ||
}) | ||
image?: Image; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@OneToOne(() => Customer) | ||
customer!: any; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Comment { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
title!: string; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@OneToOne(() => Customer) | ||
customer!: any; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Customer { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@OneToOne(() => Product, product => product.customer, { eager: true }) | ||
product!: Product; | ||
|
||
@OneToOne(() => Comment, comment => comment.customer, { eager: true }) | ||
comment!: Comment; | ||
|
||
} | ||
|
||
describe('GH issue 2777', () => { | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Customer, Comment, Product, Image], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test(`GH issue 2777`, async () => { | ||
const c = new Customer(); | ||
c.comment = new Comment(); | ||
c.comment.title = 'c'; | ||
c.product = new Product(); | ||
c.product.title = 't'; | ||
c.product.image = new Image(); | ||
c.product.image.customer = c; | ||
c.name = 'f'; | ||
await orm.em.fork().persistAndFlush(c); | ||
const ret = await orm.em.find(Customer, {}); | ||
expect(ret[0]).toBe(ret[0].product.image!.customer); | ||
expect(wrap(ret[0].product).isInitialized()).toBe(true); | ||
expect(wrap(ret[0].product.image).isInitialized()).toBe(true); | ||
expect(wrap(ret[0].product.image!.customer).isInitialized()).toBe(true); | ||
}); | ||
|
||
}); |