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 (46 loc) • 1.49 kB
text/typescript
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/sqlite';
()
export class Author {
({ comment: 'PK' })
id!: bigint;
({ nullable: true })
name!: string;
('Post', 'author', {
orphanRemoval: true,
})
post = new Collection<Post>(this);
({ persist: false })
postTotal?: number;
}
()
export class Post {
({ comment: 'PK' })
id!: bigint;
({ nullable: true })
title!: string;
({ nullable: true })
body!: string;
(() => Author)
author!: Author;
}
describe('GH issue 1538', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author, Post],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(() => orm.close(true));
test(`sub-queries with custom type PK (bigint)`, async () => {
const knex = orm.em.getKnex();
const qb1 = orm.em.createQueryBuilder(Post, 'b')
.count('b.id', true)
.where({ author: knex.ref('a.id') })
.as('Author.postTotal');
const qb2 = orm.em.createQueryBuilder(Author, 'a');
qb2.select(['*', qb1]).orderBy({ postTotal: 'desc' });
expect(qb2.getFormattedQuery()).toBe('select `a`.*, (select count(distinct `b`.`id`) as `count` from `post` as `b` where `b`.`author_id` = `a`.`id`) as `post_total` from `author` as `a` order by `post_total` desc');
});
});