-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): do not mutate data provided to
em.upsert/Many
Closes #5136
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Property, Entity, PrimaryKey } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property() | ||
name: string; | ||
|
||
@Property({ unique: true }) | ||
email: string; | ||
|
||
constructor(name: string, email: string) { | ||
this.name = name; | ||
this.email = email; | ||
} | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [User], | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await orm.schema.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('upsert should not modify data object unless it is an entity', async () => { | ||
const data = { name: 'Foo', email: 'foo' }; | ||
orm.em.create(User, data); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
expect(data).toMatchObject({ name: 'Foo', email: 'foo' }); | ||
|
||
data.name = 'Bar'; | ||
const newUser = await orm.em.upsert(User, data); | ||
|
||
expect(data).toMatchObject({ name: 'Bar', email: 'foo' }); | ||
expect({ name: 'Bar', email: 'foo' }).toMatchObject(data); | ||
}); | ||
|
||
test('upsertMany should not modify data object unless it is an entity', async () => { | ||
const data1 = { name: 'Foo1', email: 'foo1' }; | ||
const data2 = { name: 'Foo2', email: 'foo2' }; | ||
orm.em.create(User, data1); | ||
orm.em.create(User, data2); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
expect(data1).toMatchObject({ name: 'Foo1', email: 'foo1' }); | ||
expect(data2).toMatchObject({ name: 'Foo2', email: 'foo2' }); | ||
|
||
data1.name = 'Bar1'; | ||
data2.name = 'Bar2'; | ||
const [newUser1, newUser2] = await orm.em.upsertMany(User, [data1, data2]); | ||
|
||
expect(data1).toMatchObject({ name: 'Bar1', email: 'foo1' }); | ||
expect(data2).toMatchObject({ name: 'Bar2', email: 'foo2' }); | ||
expect({ name: 'Bar1', email: 'foo1' }).toMatchObject(data1); | ||
expect({ name: 'Bar2', email: 'foo2' }).toMatchObject(data2); | ||
}); |