-
-
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(mongo): ensure
assign
on object properties won't ignore changes
Closes #5158
- Loading branch information
Showing
2 changed files
with
67 additions
and
2 deletions.
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,66 @@ | ||
import { | ||
Entity, | ||
JsonType, | ||
PrimaryKey, | ||
Property, | ||
} from '@mikro-orm/core'; | ||
import { MikroORM, ObjectId } from '@mikro-orm/mongodb'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey() | ||
_id!: ObjectId; | ||
|
||
@Property({ type: JsonType }) | ||
methods: { method1?: string; method2?: string } = {}; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: '5158', | ||
entities: [User], | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
test('should update json properties with assign()', async () => { | ||
// initialize user | ||
const user1 = new User(); | ||
user1.methods = { method1: '1', method2: '1' }; | ||
const em1 = orm.em.fork(); | ||
await em1.persistAndFlush(user1); | ||
|
||
// verify that it was persisted properly | ||
const emRead1 = orm.em.fork(); | ||
const result1 = await emRead1.findOneOrFail(User, user1._id); | ||
expect(result1.methods).toEqual({ method1: '1', method2: '1' }); | ||
|
||
// updating JSON property without assign() should work | ||
const em2 = orm.em.fork(); | ||
const user2 = await em2.findOneOrFail(User, user1._id); | ||
user2.methods = { method1: '1', method2: '2' }; | ||
await em2.persistAndFlush(user2); | ||
|
||
// verify that it was persisted properly | ||
const emRead2 = orm.em.fork(); | ||
const result2 = await emRead2.findOneOrFail(User, user1._id); | ||
expect(result2.methods).toEqual({ method1: '1', method2: '2' }); | ||
|
||
// updating JSON property with assign() should work | ||
const em3 = orm.em.fork(); | ||
const user3 = await em3.findOneOrFail(User, user1._id); | ||
em3.assign(user3, { methods: { method2: '3' } }); | ||
await em3.persistAndFlush(user3); | ||
|
||
// verify that it was persisted properly | ||
const emRead3 = orm.em.fork(); | ||
const result3 = await emRead3.findOneOrFail(User, user1._id); | ||
expect(result3.methods).toEqual({ method1: '1', method2: '3' }); | ||
}); |