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

RJS-2846: Fix path configuration when using sync #6753

Merged
merged 12 commits into from
Jun 25, 2024
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* None
* `path` option in the Realm configuration not being set when using a synced Realm. ([#6754](https://github.com/realm/realm-js/issues/6754), since v12.8.0). Note: if you have been using a custom path configuration with your synced Realm, this fix will lead to a re-download of its data in the custom path.

### Compatibility
* React Native >= v0.71.4
Expand Down
54 changes: 39 additions & 15 deletions integration-tests/tests/src/tests/sync/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2111,21 +2111,45 @@ describe("Realmtest", () => {
describe("with sync", () => {
importAppBefore(buildAppConfig("with-anon").anonAuth().partitionBasedSync());

it("data is deleted on realm with custom path", function (this: RealmContext & AppContext) {
return this.app.logIn(Realm.Credentials.anonymous()).then(async (user) => {
const config = {
schema: [TestObjectWithPkSchema],
sync: { user, partitionValue: '"Lolo"' },
};
const realm = new Realm(config);
const path = realm.path;
realm.close();
const pathExistBeforeDelete = await fs.exists(path);
expect(pathExistBeforeDelete).to.be.true;
Realm.deleteFile(config);
const pathExistAfterDelete = await fs.exists(path);
expect(pathExistAfterDelete).to.be.false;
});
it("custom path gets correctly set", async function (this: RealmContext & AppContext) {
const user = await this.app.logIn(Realm.Credentials.anonymous());
const testPath = "custom_path.realm";
const pathSeparator = "/";
const defaultDir =
Realm.defaultPath.substring(0, Realm.defaultPath.lastIndexOf(pathSeparator) + 1) +
"mongodb-realm" +
pathSeparator +
this.app.id +
pathSeparator +
user.id +
pathSeparator;

const realm = await Realm.open({
path: testPath,
schema: [TestObjectWithPkSchema],
sync: { user, partitionValue: '"Lolo"' },
});
expect(realm.path).equals(defaultDir + testPath);
const path = realm.path;
realm.close();
const pathExistAfterInitialization = await fs.exists(path);
expect(pathExistAfterInitialization).to.be.true;
});

it("data is deleted on realm with custom path", async function (this: RealmContext & AppContext) {
const user = await this.app.logIn(Realm.Credentials.anonymous());
const config = {
schema: [TestObjectWithPkSchema],
sync: { user, partitionValue: '"Lolo"' },
};
const realm = new Realm(config);
const path = realm.path;
realm.close();
const pathExistBeforeDelete = await fs.exists(path);
expect(pathExistBeforeDelete).to.be.true;
Realm.deleteFile(config);
const pathExistAfterDelete = await fs.exists(path);
expect(pathExistAfterDelete).to.be.false;
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@
return Realm.normalizePath(config.path);
} else {
const bindingSyncConfig = toBindingSyncConfig(config.sync);
return config.sync.user.internal.pathForRealm(bindingSyncConfig, undefined);
return config.sync.user.internal.pathForRealm(bindingSyncConfig, config.path);
}
} else {
return Realm.normalizePath(config.path);
Expand Down Expand Up @@ -839,7 +839,7 @@

/**
* Deletes a Realm model, including all of its objects.
* If called outside a migration function, {@link schema} and {@link schemaVersion} are updated.

Check warning on line 842 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'schemaVersion' is undefined
* @param name - The model name.
*/
deleteModel(name: string): void {
Expand Down Expand Up @@ -1001,7 +1001,7 @@
* Remove the listener {@link callback} for the specified event {@link eventName}.
* @param eventName - The event name.
* @param callback - Function that was previously added as a listener for this event through the {@link addListener} method.
* @throws an {@link Error} If an invalid event {@link eventName} is supplied, if Realm is closed or if {@link callback} is not a function.

Check warning on line 1004 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'addListener' is undefined
*/
removeListener(eventName: RealmEventName, callback: RealmListenerCallback): void {
assert.open(this);
Expand Down Expand Up @@ -1045,7 +1045,7 @@
}

/**
* Synchronously call the provided {@link callback} inside a write transaction. If an exception happens inside a transaction,

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'beginTransaction' is undefined

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'commitTransaction' is undefined

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'cancelTransaction' is undefined

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'commitTransaction' is undefined

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'write' is undefined

Check warning on line 1048 in packages/realm/src/Realm.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'write' is undefined
* you’ll lose the changes in that transaction, but the Realm itself won’t be affected (or corrupted).
* More precisely, {@link beginTransaction} and {@link commitTransaction} will be called
* automatically. If any exception is thrown during the transaction {@link cancelTransaction} will
Expand Down
Loading