Adding optional user as argument to Realm.automaticSyncConfiguration. (#1709)

This commit is contained in:
Kenneth Geisshirt 2018-03-14 08:26:41 +01:00 committed by GitHub
parent 0efc4fbedb
commit 10480c1afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 20 deletions

View File

@ -1,3 +1,17 @@
X.Y.Z Release notes
=============================================================
### Breaking changes
* None.
### Enhancements
* Added an optional user as argument to `Realm.automaticSyncConfiguration` (#1708).
### Bug fixes
* None.
### Internal
* None.
2.3.0 Release notes (2018-3-13)
=============================================================
### Breaking changes

View File

@ -120,13 +120,14 @@ class Realm {
static openAsync(config, callback, progressCallback) {}
/**
* Return a configuration for a default synced Realm. The server URL for the current user will be used as base for
* the URL for the synced Realm.
* Return a configuration for a default synced Realm. The server URL for the user will be used as base for
* the URL for the synced Realm. If no user is supplied, the current user will be used.
* @param {Realm.Sync.User} - an optional sync user
* @throws {Error} if zero or multiple users are logged in
* @returns {Realm~Configuration} - a configuration matching a default synced Realm.
* @since 2.3.0
*/
static automaticSyncConfiguration() {}
static automaticSyncConfiguration(user) {}
/**
* Closes this Realm so it may be re-opened with a newer schema version.

View File

@ -153,24 +153,34 @@ module.exports = function(realmConstructor) {
// A configuration for a default Realm
realmConstructor.automaticSyncConfiguration = function() {
let users = this.Sync.User.all;
let identities = Object.keys(users);
if (identities.length === 1) {
let user = users[identities[0]];
let url = new URL(user.server);
let secure = (url.protocol === 'https:')?'s':'';
let port = (url.port === undefined)?'9080':url.port
let realmUrl = `realm${secure}://${url.hostname}:${port}/~/default`;
let user;
let config = {
sync: {
user,
url: realmUrl
}
};
return config;
if (arguments.length === 0) {
let users = this.Sync.User.all;
let identities = Object.keys(users);
if (identities.length === 1) {
user = users[identities[0]];
} else {
new Error(`One and only one user should be logged in but found ${users.length} users.`);
}
} else if (arguments.length === 1) {
user = arguments[0];
} else {
new Error(`Zero or one argument expected.`);
}
new Error(`One and only one user should be logged in but found ${users.length} users.`);
let url = new URL(user.server);
let secure = (url.protocol === 'https:')?'s':'';
let port = (url.port === undefined)?'9080':url.port
let realmUrl = `realm${secure}://${url.hostname}:${port}/~/default`;
let config = {
sync: {
user,
url: realmUrl
}
};
return config;
}
if (realmConstructor.Sync._setFeatureToken) {

3
lib/index.d.ts vendored
View File

@ -568,8 +568,9 @@ declare class Realm {
/**
* Return a configuration for a default Realm.
* @param {Realm.Sync.User} optional user.
*/
static automaticSyncConfiguration(): string;
static automaticSyncConfiguration(user?: Realm.Sync.User): string;
/**
* Delete the Realm file for the given configuration.

View File

@ -193,6 +193,50 @@ module.exports = {
});
},
testDefaultRealmFromUser() {
if (!isNodeProccess) {
return;
}
const username = uuid();
const realmName = 'default';
const expectedObjectsCount = 3;
let user;
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
.then(u => {
user = u;
return Realm.open(Realm.automaticSyncConfiguration(user));
})
.then(realm => {
let actualObjectsCount = realm.objects('Dog').length;
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
const session = realm.syncSession;
TestCase.assertInstanceOf(session, Realm.Sync.Session);
TestCase.assertEqual(session.user.identity, user.identity);
TestCase.assertEqual(session.state, 'active');
});
},
testDefaultRealmInvalidArguments() {
if (!isNodeProccess) {
return;
}
const username = uuid();
const realmName = 'default';
const expectedObjectsCount = 3;
let user;
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
.then(u => {
TestCase.assertThrows(() => Realm.automaticSyncConfiguration('foo', 'bar')); // too many arguments
})
},
testRealmOpenWithExistingLocalRealm() {
if (!isNodeProccess) {
return;