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.
86 lines (66 loc) • 2.21 kB
text/typescript
import { Entity, MikroORM, PrimaryKey, OneToMany, ManyToOne, Collection, ValidationError, ArrayCollection } from '@mikro-orm/sqlite';
()
class A {
()
id!: number;
(() => B, b => b.a)
bItems = new Collection<B>(this);
}
()
class B {
()
id!: number;
(() => A, { nullable: true })
a?: A;
}
describe('GH issue 949', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [A, B],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 949`, async () => {
let aEntity = new A();
aEntity.bItems.add(new B());
// Entity not managed yet
expect(() => aEntity.bItems.loadCount()).rejects.toThrow(ValidationError);
await orm.em.persistAndFlush(aEntity);
if (!aEntity) { return; }
const reloadedBook = await aEntity.bItems.loadCount();
expect(reloadedBook).toBe(1);
// Adding new items
const laterRemoved = new B();
aEntity.bItems.add(laterRemoved, new B());
const threeItms = await aEntity.bItems.loadCount();
expect(threeItms).toEqual(3);
// Force refresh
expect(await aEntity.bItems.loadCount(true)).toEqual(1);
// Testing array collection implementation
await orm.em.flush();
orm.em.clear();
// Updates when removing an item
aEntity = (await orm.em.findOne(A, aEntity.id))!;
expect(await aEntity.bItems.loadCount()).toEqual(3);
await aEntity.bItems.init();
aEntity.bItems.remove(aEntity.bItems[0]);
expect(await aEntity.bItems.loadCount()).toEqual(2);
expect(await aEntity.bItems.loadCount(true)).toEqual(3);
await orm.em.flush();
orm.em.clear();
// Resets the counter when hydrating
aEntity = (await orm.em.findOne(A, aEntity.id))!;
await aEntity.bItems.loadCount();
aEntity.bItems.hydrate([]);
expect(await aEntity.bItems.loadCount()).toEqual(0);
expect(await aEntity.bItems.loadCount(true)).toEqual(2);
// Code coverage ?
const arryCollection = new ArrayCollection(aEntity);
expect(await arryCollection.loadCount()).toEqual(0);
});
});