Skip to content

Commit

Permalink
Values of properties are replaced by default value when updating (#6132)
Browse files Browse the repository at this point in the history
* Values of properties are replaced by default value when updating
---------
Co-authored-by: LJ <[email protected]>
Co-authored-by: Kræn Hansen <[email protected]>
  • Loading branch information
kneth authored Sep 20, 2023
1 parent b807f3b commit f3fe1d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions integration-tests/tests/src/tests/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
});
});
17 changes: 9 additions & 8 deletions packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,15 @@ export class RealmObject<T = DefaultObject, RequiredProperties extends keyof Omi
result[propertyName] = propertyValue;
}
} else {
if (typeof defaultValue !== "undefined") {
result[propertyName] = typeof defaultValue === "function" ? defaultValue() : defaultValue;
} else if (
!(property.type & binding.PropertyType.Collection) &&
!(property.type & binding.PropertyType.Nullable) &&
created
) {
throw new Error(`Missing value for property '${propertyName}'`);
if (created) {
if (typeof defaultValue !== "undefined") {
result[propertyName] = typeof defaultValue === "function" ? defaultValue() : defaultValue;
} else if (
!(property.type & binding.PropertyType.Collection) &&
!(property.type & binding.PropertyType.Nullable)
) {
throw new Error(`Missing value for property '${propertyName}'`);
}
}
}
}
Expand Down

0 comments on commit f3fe1d4

Please sign in to comment.