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.
56 lines (41 loc) • 1.21 kB
text/typescript
import { Entity, PrimaryKey, Property, Unique } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';
()
({ properties: ['uniq1', 'uniq2'] })
class MyEntity1 {
()
id?: number;
()
uniq1!: number;
()
uniq2!: number;
()
name!: string;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: 'mikro_4153',
port: 3308,
entities: [MyEntity1],
});
await orm.schema.refreshDatabase();
});
afterAll(() => orm.close(true));
test('mapping PKs after upsert based on unique properties', async () => {
await orm.em.insertMany(MyEntity1, [
{ id: 1, uniq1: 1, uniq2: 1, name: 'first' },
{ id: 2, uniq1: 2, uniq2: 1, name: 'second' },
]);
const entity1 = new MyEntity1();
entity1.uniq1 = 1;
entity1.uniq2 = 1;
entity1.name = 'first updated';
const entity2 = new MyEntity1();
entity2.uniq1 = 2;
entity2.uniq2 = 1;
entity2.name = 'second updated';
const result = await orm.em.upsertMany([entity2, entity1]);
expect(result.find(v => v.uniq1 === 1 && v.uniq2 === 1)!.id).toBe(1);
expect(result.find(v => v.uniq1 === 2 && v.uniq2 === 1)!.id).toBe(2);
});