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.
72 lines (55 loc) • 1.55 kB
text/typescript
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey } from '@mikro-orm/sqlite';
class Contract {
id!: number;
customer!: any;
}
class Customer {
id!: number;
contracts = new Collection<Contract>(this);
parentCustomer?: Customer;
childCustomers = new Collection<Customer>(this);
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Contract, Customer],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 3490`, async () => {
const c = orm.em.create(Contract, {
id: 1,
customer: {
childCustomers: [
{
contracts: [
{ id: 2 },
{ id: 3 },
{ id: 4 },
],
},
],
},
});
await orm.em.persist(c).flush();
orm.em.clear();
const contract = await orm.em.findOneOrFail(Contract, c.id,
{ populate: ['customer.childCustomers.contracts'] },
);
expect(contract.customer.childCustomers[0].contracts).toHaveLength(3);
expect(contract.customer.childCustomers[0].contracts[0].id).toBe(2);
expect(contract.customer.childCustomers[0].contracts[1].id).toBe(3);
expect(contract.customer.childCustomers[0].contracts[2].id).toBe(4);
});