diff --git a/CHANGELOG.md b/CHANGELOG.md index 37741608b4..4f41435022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added `Realm.Sync.Session.reconnect()` to help force a reconnection to Atlas Device Sync. ([#6123](https://github.com/realm/realm-js/issues/6123)) ### Fixed +* Fixed values of properties being replaced by default value when updating. ([#6129](https://github.com/realm/realm-js/issues/6129), since v12.0.0) * Fixed that value for `Realm.schemaVersion` wasn't propagated correctly for non-existing files. ([#6119](https://github.com/realm/realm-js/issues/6119), since v12.0.0) ### Compatibility diff --git a/integration-tests/tests/src/tests/objects.ts b/integration-tests/tests/src/tests/objects.ts index 0946458aae..4eec3cebbc 100644 --- a/integration-tests/tests/src/tests/objects.ts +++ b/integration-tests/tests/src/tests/objects.ts @@ -1335,4 +1335,38 @@ describe("Realm.Object", () => { expect(objFromKey?.age).equals(7); }); }); + + // from https://github.com/realm/realm-js/issues/6129 + describe("Primary key and property with default value", () => { + const PrimaryAndDefaultSchema: Realm.ObjectSchema = { + name: "MySchema", + primaryKey: "id", + properties: { + id: { type: "int" }, + fieldOne: { type: "string" }, + fieldTwo: { type: "string", default: "DEFAULT_VALUE" }, + }, + }; + openRealmBeforeEach({ schema: [PrimaryAndDefaultSchema] }); + + for (const updateMode of [Realm.UpdateMode.All, Realm.UpdateMode.Modified]) { + it(`updates properties (updateMode = ${updateMode})`, async function (this: Mocha.Context & RealmContext) { + this.realm.write(() => { + this.realm.create(PrimaryAndDefaultSchema.name, { + id: 1337, + fieldOne: "SOME_VALUE", + fieldTwo: "NOT_DEFAULT_VALUE", + }); + }); + + this.realm.write(() => { + this.realm.create(PrimaryAndDefaultSchema.name, { id: 1337, fieldOne: "SOME_OTHER_VALUE" }, updateMode); + }); + + const obj = this.realm.objectForPrimaryKey(PrimaryAndDefaultSchema.name, 1337); + expect(obj?.fieldOne).equals("SOME_OTHER_VALUE"); + expect(obj?.fieldTwo).equals("NOT_DEFAULT_VALUE"); + }); + } + }); }); diff --git a/packages/realm/src/Object.ts b/packages/realm/src/Object.ts index 85be7f37f6..596cfb7e22 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -232,14 +232,15 @@ export class RealmObject