Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values of properties are replaced by default value when updating #6132

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -141,7 +141,7 @@
* }
* ```
* @see {@link ObjectSchema}
* @typeParam `T` - The type of this class (e.g. if your class is `Person`,

Check warning on line 144 in packages/realm/src/Object.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'ObjectSchema' is undefined
* `T` should also be `Person` - this duplication is required due to how
* TypeScript works)
* @typeParam `RequiredProperties` - The names of any properties of this
Expand Down Expand Up @@ -232,14 +232,15 @@
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
Loading