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.
101 lines (76 loc) • 1.86 kB
text/typescript
import {
Collection,
Entity,
helper,
ManyToOne,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Ref,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';
class Organization {
id!: number;
license!: Ref<License>;
workspaces = new Collection<Workspace>(this);
name!: string;
}
class License {
[OptionalProps]?: 'organization';
id!: number;
organization!: Ref<Organization>;
name!: string;
}
class Workspace {
[OptionalProps]?: 'organization';
id!: number;
name!: string;
organization!: Ref<Organization>;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Organization, License, Workspace],
dbName: ':memory:',
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close();
});
test('3941', async () => {
const organization = orm.em.create(Organization, {
name: 'Organization',
license: {
name: 'License',
},
workspaces: [
{ name: 'Workspace' },
],
});
await orm.em.flush();
orm.em.clear();
const workspace = await orm.em.findOneOrFail(Workspace, organization.workspaces[0].id);
const license = await orm.em.findOneOrFail(License, { organization: { workspaces: workspace } });
orm.em.getUnitOfWork().computeChangeSets();
expect(orm.em.getUnitOfWork().getChangeSets()).toHaveLength(0);
expect(helper(license.organization).__originalEntityData).toEqual({ id: 1, license: 1 });
});