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.
81 lines (58 loc) • 1.39 kB
text/typescript
import { Collection, Entity, ManyToMany, ManyToOne, MikroORM, PrimaryKey, wrap } from '@mikro-orm/sqlite';
class School {
id!: number;
}
class User {
id!: number;
roles = new Collection<Role>(this);
school?: School;
}
class Role {
id!: number;
users = new Collection<User>(this);
}
class UserRole {
user!: User;
role!: Role;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [User],
dbName: ':memory:',
ensureDatabase: { create: true },
});
});
afterAll(() => orm.close());
test('lazy em.populate on m:n', async () => {
const a = orm.em.create(User, {
id: 1,
roles: [{}, {}],
school: {},
});
await orm.em.flush();
orm.em.clear();
const user = await orm.em.findOneOrFail(User, { id: 1 }, { populate: ['school'] });
await orm.em.populate(user, ['roles']);
const dto = wrap(user).toObject();
expect(dto).toEqual({
id: 1,
roles: [{ id: 1 }, { id: 2 }],
school: { id: 1 },
});
});