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.
65 lines (47 loc) • 1.34 kB
text/typescript
import { Entity, ManyToOne, MikroORM, PrimaryKey, Property } from '@mikro-orm/sqlite';
import { mockLogger } from '../helpers';
()
class B {
({ type: 'number' })
id!: number;
({ type: 'string' })
name!: string;
}
()
export class A {
({ type: 'number' })
id!: number;
({ type: 'string' })
name!: string;
(() => B)
type!: B;
}
describe('GH issue 228', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [A, B],
dbName: ':memory:',
});
await orm.schema.dropSchema();
await orm.schema.createSchema();
});
afterAll(() => orm.close(true));
test('search by m:n', async () => {
const a = new A();
a.name = 'a';
a.type = new B();
a.type.name = 'b';
await orm.em.persistAndFlush(a);
orm.em.clear();
const mock = mockLogger(orm, ['query']);
await orm.em.findAndCount(A, {}, {
orderBy: { type: 'asc' },
populate: ['*'],
});
const queries: string[] = mock.mock.calls.map(c => c[0]).sort();
expect(queries).toHaveLength(3);
expect(queries[0]).toMatch('select `a0`.* from `a` as `a0` order by `a0`.`type_id` asc');
expect(queries[1]).toMatch('select `b0`.* from `b` as `b0` where `b0`.`id` in (?)');
});
});