Skip to content

Commit

Permalink
fix(mongo): ensure JSON values are properly diffed
Browse files Browse the repository at this point in the history
Closes #5572
  • Loading branch information
B4nan committed May 15, 2024
1 parent 9485daa commit 577166a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/mongodb/src/MongoPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class MongoPlatform extends Platform {
}

override convertJsonToDatabaseValue(value: unknown): unknown {
return value;
return Utils.copy(value);
}

override convertJsonToJSValue(value: unknown): unknown {
Expand Down
40 changes: 40 additions & 0 deletions tests/features/custom-types/GH5572.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/mongodb';

@Entity()
class Dummy {

@PrimaryKey()
_id!: number;

@Property({ type: 'json' })
a!: { b: number };

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: '5572',
entities: [Dummy],
});
await orm.schema.refreshDatabase();
});

afterAll(async () => {
await orm.close(true);
});

test('deep update', async () => {
const usr1 = orm.em.create(Dummy, { _id: 1, a: { b: 1 } });
await orm.em.flush();
orm.em.clear();

const usr2 = await orm.em.findOneOrFail(Dummy, usr1._id);
usr2.a.b = 100;
await orm.em.flush();
orm.em.clear();

const usr3 = await orm.em.findOneOrFail(Dummy, usr1._id);
expect(usr3.a.b).toBe(100);
});

0 comments on commit 577166a

Please sign in to comment.