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.
88 lines (64 loc) • 2.43 kB
text/typescript
import 'reflect-metadata';
import { Collection, Entity, ManyToMany, ManyToOne, MikroORM, PrimaryKey, Property, wrap } from '@mikro-orm/postgresql';
({ tableName: 'auth.users' })
class TaskAssignee {
()
avatar: string;
({ name: 'first_name' })
firstName: string;
({ name: 'last_name' })
lastName: string;
({ name: 'id' })
userid!: number;
constructor(avatar: string, firstName: string, lastName: string) {
this.avatar = avatar;
this.firstName = firstName;
this.lastName = lastName;
}
}
({ tableName: 'operations.tasks' })
class Task {
({ entity: () => TaskAssignee, pivotTable: 'operations.task_assignees' })
assignees = new Collection<TaskAssignee>(this);
(() => TaskAssignee, { nullable: true })
assignee?: TaskAssignee;
()
id!: number;
}
describe('GH issue 450', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Task, TaskAssignee],
dbName: `mikro_orm_test_gh_450`,
});
await orm.schema.ensureDatabase();
await orm.em.getConnection().execute('drop schema if exists auth');
await orm.em.getConnection().execute('drop schema if exists operations');
await orm.em.getConnection().execute('set search_path to auth, operations, public');
await orm.schema.dropSchema();
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.schema.dropSchema({ wrap: true, dropMigrationsTable: true, dropDb: true });
await orm.schema.dropDatabase('auth');
await orm.schema.dropDatabase('operations');
await orm.close(true);
});
test(`multiple schemas and m:n collections`, async () => {
const t = new Task();
t.assignees.add(new TaskAssignee('avatar', 'first', 'last'));
await orm.em.persistAndFlush(t);
orm.em.clear();
const t1 = await orm.em.findOneOrFail(Task, t.id, { populate: ['assignees'] });
expect(t1.assignees.count()).toBe(1);
expect(t1.assignees[0]).toBeInstanceOf(TaskAssignee);
expect(wrap(t1.assignees[0]).isInitialized()).toBe(true);
t1.assignee = t1.assignees[0];
await orm.em.flush();
orm.em.clear();
const t2 = await orm.em.findOneOrFail(Task, t.id, { populate: ['assignee'] });
expect(t2.assignee).toBeInstanceOf(TaskAssignee);
expect(wrap(t2.assignee!).isInitialized()).toBe(true);
});
});