From 45077d63b82826a3e702af4c4b1c43294c652607 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Thu, 14 Sep 2023 18:06:40 +0200 Subject: [PATCH 1/4] Values of properties are replaced by default value when updating --- CHANGELOG.md | 1 + integration-tests/tests/src/tests/objects.ts | 34 ++++++++++++++++++++ packages/realm/src/Object.ts | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37741608b4..c74e5f511f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixed * 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) +* Values of properties are replaced by default value when updating. ([#6129](https://github.com/realm/realm-js/issues/6129), since v12.0.0) ### Compatibility * React Native >= v0.71.4 diff --git a/integration-tests/tests/src/tests/objects.ts b/integration-tests/tests/src/tests/objects.ts index 0946458aae..c2b2e25b52 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] }); + + [Realm.UpdateMode.All, Realm.UpdateMode.Modified].forEach((updateMode) => { + it(`Update 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..91df9251e6 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -232,7 +232,7 @@ export class RealmObject Date: Mon, 18 Sep 2023 15:46:20 +0200 Subject: [PATCH 2/4] Update CHANGELOG.md Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c74e5f511f..4f41435022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,8 @@ * 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) -* Values of properties are replaced by default value when updating. ([#6129](https://github.com/realm/realm-js/issues/6129), since v12.0.0) ### Compatibility * React Native >= v0.71.4 From 014796d5169679e3680f3d5ec91a4cdccc4af67b Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 20 Sep 2023 18:37:45 +0200 Subject: [PATCH 3/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kræn Hansen --- integration-tests/tests/src/tests/objects.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/tests/src/tests/objects.ts b/integration-tests/tests/src/tests/objects.ts index c2b2e25b52..2e7aa04748 100644 --- a/integration-tests/tests/src/tests/objects.ts +++ b/integration-tests/tests/src/tests/objects.ts @@ -1349,8 +1349,8 @@ describe("Realm.Object", () => { }; openRealmBeforeEach({ schema: [PrimaryAndDefaultSchema] }); - [Realm.UpdateMode.All, Realm.UpdateMode.Modified].forEach((updateMode) => { - it(`Update properties (updateMode = ${updateMode})`, async function (this: Mocha.Context & RealmContext) { + 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, From 4aa9a9de7c70a03741d3bf2b42301a37140a51ff Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 20 Sep 2023 19:03:05 +0200 Subject: [PATCH 4/4] smaller refactoring --- integration-tests/tests/src/tests/objects.ts | 2 +- packages/realm/src/Object.ts | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/integration-tests/tests/src/tests/objects.ts b/integration-tests/tests/src/tests/objects.ts index 2e7aa04748..4eec3cebbc 100644 --- a/integration-tests/tests/src/tests/objects.ts +++ b/integration-tests/tests/src/tests/objects.ts @@ -1367,6 +1367,6 @@ describe("Realm.Object", () => { 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 91df9251e6..596cfb7e22 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -232,14 +232,15 @@ export class RealmObject