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.
68 lines (49 loc) • 1.34 kB
text/typescript
import { Entity, PrimaryKey, Property, OneToOne, MikroORM } from '@mikro-orm/sqlite';
import { mockLogger } from '../helpers';
()
class Profile {
()
id!: number;
('User', 'profile')
user: any;
}
()
export class User {
()
id!: number;
()
name!: string;
(() => Profile)
profile!: Profile;
}
describe('GH issue 1704', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [User, Profile],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test('loading cached entity with relations', async () => {
const user = new User();
user.id = 1;
user.name = 'Foo';
user.profile = new Profile();
user.profile.id = 2;
await orm.em.fork().persistAndFlush(user);
const mock = mockLogger(orm, ['query']);
const getAndFlush = async (expected: number) => {
const em = orm.em.fork();
await em.findOneOrFail(Profile, 2, { cache: 1000 });
await em.findOneOrFail(User, 1);
await em.flush();
expect(mock.mock.calls).toHaveLength(expected);
};
await getAndFlush(2); // no cache hit
await getAndFlush(3); // cache hit, so 2 previous + 1 new query
});
});