undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
102 lines (74 loc) • 1.94 kB
text/typescript
import { Entity, LoadStrategy, ManyToOne, MikroORM, OneToOne, PrimaryKey, Property, wrap } from '@mikro-orm/sqlite';
()
export class Image {
()
id!: number;
(() => Customer)
customer!: any;
}
()
export class Product {
()
id!: number;
()
title!: string;
(() => Image, {
strategy: LoadStrategy.JOINED,
eager: true,
nullable: true,
})
image?: Image;
(() => Customer)
customer!: any;
}
()
export class Comment {
()
id!: number;
()
title!: string;
(() => Customer)
customer!: any;
}
()
export class Customer {
()
id!: number;
()
name!: string;
(() => Product, product => product.customer, { eager: true })
product!: Product;
(() => 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:',
});
await orm.schema.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.comment.customer = c;
c.product = new Product();
c.product.title = 't';
c.product.image = new Image();
c.product.customer = c;
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);
});
});