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.
91 lines (67 loc) • 1.89 kB
text/typescript
import { MikroORM } from '@mikro-orm/mysql';
import { Collection, LoadStrategy, Entity, ManyToMany, PrimaryKey, Property, Type } from '@mikro-orm/core';
import { parse, stringify, v1 } from 'uuid';
let orm: MikroORM;
class UuidBinaryType extends Type<string, Buffer> {
override convertToDatabaseValue(uuid: string) {
return Buffer.from(parse(uuid));
}
override convertToJSValue(bin: Buffer) {
return stringify(bin);
}
override getColumnType() {
return 'binary(16)';
}
}
()
export class Customer {
({ type: UuidBinaryType })
uuid: string = v1();
()
name!: string;
(() => Role)
roles = new Collection<Role>(this);
}
()
export class Role {
({ type: UuidBinaryType })
uuid: string = v1();
()
name!: string;
(() => Customer, 'roles')
customers = new Collection<Customer>(this);
}
beforeAll(async () => {
orm = await MikroORM.init({
dbName: 'gh-4219',
entities: [Customer, Role],
port: 3308,
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 4219`, async () => {
const roles = [
orm.em.create(Role, { name: 'customer' }),
orm.em.create(Role, { name: 'reseller' }),
];
const customers = Array(1000)
.fill(0)
.map((el, index) =>
orm.em.create(Customer, {
name: `customer-${index}`,
roles: [roles[index % roles.length]],
}),
);
await orm.em.flush();
const newEm = orm.em.fork();
const customersByJoined = await newEm
.getRepository(Customer)
.findAll({ strategy: LoadStrategy.JOINED, populate: ['roles.name'] });
const customersBySelectIn = await newEm
.getRepository(Customer)
.findAll({ populate: ['roles.name'] });
expect(customersByJoined.length).toBe(customersBySelectIn.length);
});