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.
77 lines (57 loc) • 1.49 kB
text/typescript
import { Embeddable, Embedded, Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';
()
class Nested {
()
field1: string;
()
field2: string;
()
field3: string;
constructor(field1: string, field2: string, field3: string) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
}
()
class Parent {
({ autoincrement: false })
id: number;
(() => Nested)
nested: Nested;
constructor(id: number, nested: Nested) {
this.id = id;
this.nested = nested;
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Parent],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(() => orm.close(true));
test('load embedded entity twice (GH #3134)', async () => {
// initial data
const nested = new Nested('A', 'B', 'C');
const parent = new Parent(1, nested);
await orm.em.fork().persistAndFlush(parent);
const em1 = orm.em.fork();
const p1 = await em1.findOneOrFail(Parent, 1);
expect(p1.nested.field1).toBe('A');
expect(p1.nested.field2).toBe('B');
expect(p1.nested.field3).toBe('C');
const em2 = orm.em.fork();
const p = await em2.findOneOrFail(Parent, 1);
p.nested.field1 = 'Z';
await em2.persistAndFlush(p);
await em1.refresh(p1);
expect(p1.nested).toEqual({
field1: 'Z',
field2: 'B',
field3: 'C',
});
});