2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-10-28 17:37:17 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2016-05-06 22:42:17 +00:00
|
|
|
import { keys, objectTypes } from './constants';
|
2016-03-09 07:47:08 +00:00
|
|
|
import Collection, * as collections from './collections';
|
|
|
|
import List, { createList } from './lists';
|
|
|
|
import Results, { createResults } from './results';
|
2016-05-06 22:42:17 +00:00
|
|
|
import RealmObject, * as objects from './objects';
|
2017-01-31 13:07:29 +00:00
|
|
|
import User, { createUser } from './user';
|
2017-02-01 13:18:59 +00:00
|
|
|
import Session, { createSession } from './session';
|
2016-02-28 20:36:30 +00:00
|
|
|
import * as rpc from './rpc';
|
|
|
|
import * as util from './util';
|
2017-01-31 21:56:09 +00:00
|
|
|
import { static as staticUserMethods } from '../user-methods';
|
2016-02-28 20:36:30 +00:00
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
const {debugHosts, debugPort} = NativeModules.Realm;
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-03-09 07:47:08 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.LIST, createList);
|
|
|
|
rpc.registerTypeConverter(objectTypes.RESULTS, createResults);
|
2016-05-06 22:42:17 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.OBJECT, objects.createObject);
|
2016-04-29 20:01:01 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.REALM, createRealm);
|
2017-01-31 13:07:29 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.USER, createUser);
|
2017-02-01 13:18:59 +00:00
|
|
|
rpc.registerTypeConverter(objectTypes.SESSION, createSession);
|
2016-04-29 20:01:01 +00:00
|
|
|
|
|
|
|
function createRealm(_, info) {
|
|
|
|
let realm = Object.create(Realm.prototype);
|
|
|
|
|
|
|
|
setupRealm(realm, info.id);
|
|
|
|
return realm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupRealm(realm, realmId) {
|
|
|
|
realm[keys.id] = realmId;
|
|
|
|
realm[keys.realm] = realmId;
|
|
|
|
realm[keys.type] = objectTypes.REALM;
|
|
|
|
|
|
|
|
[
|
2017-07-12 11:02:21 +00:00
|
|
|
'empty',
|
2016-04-29 20:01:01 +00:00
|
|
|
'path',
|
|
|
|
'readOnly',
|
2017-09-13 18:42:23 +00:00
|
|
|
'inMemory',
|
2016-04-29 20:01:01 +00:00
|
|
|
'schema',
|
|
|
|
'schemaVersion',
|
2017-02-01 13:18:59 +00:00
|
|
|
'syncSession',
|
2017-08-21 15:48:53 +00:00
|
|
|
'isInTransaction',
|
2017-10-02 18:29:36 +00:00
|
|
|
'subscribeToObjects',
|
2016-04-29 20:01:01 +00:00
|
|
|
].forEach((name) => {
|
|
|
|
Object.defineProperty(realm, name, {get: util.getterForProperty(name)});
|
|
|
|
});
|
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2016-06-09 20:07:05 +00:00
|
|
|
function getObjectType(realm, type) {
|
|
|
|
if (typeof type == 'function') {
|
|
|
|
return objects.typeForConstructor(realm[keys.realm], type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2016-02-28 20:36:30 +00:00
|
|
|
export default class Realm {
|
2015-10-02 05:56:47 +00:00
|
|
|
constructor(config) {
|
2016-02-18 19:09:51 +00:00
|
|
|
let schemas = typeof config == 'object' && config.schema;
|
2016-05-06 22:42:17 +00:00
|
|
|
let constructors = schemas ? {} : null;
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
for (let i = 0, len = schemas ? schemas.length : 0; i < len; i++) {
|
|
|
|
let item = schemas[i];
|
2015-11-03 10:41:52 +00:00
|
|
|
|
2016-02-18 19:09:51 +00:00
|
|
|
if (typeof item == 'function') {
|
|
|
|
let schema = item.schema;
|
|
|
|
if (!schema || typeof schema != 'object') {
|
2017-09-15 20:16:42 +00:00
|
|
|
throw new Error("Realm object constructor must have a 'schema' property.");
|
2016-02-18 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let {name, properties} = schema;
|
|
|
|
if (!name || typeof name != 'string') {
|
2017-09-15 20:16:42 +00:00
|
|
|
throw new Error(`Failed to read ObjectSchema: name must be of type 'string', got (${typeof name})`);
|
2016-02-18 19:09:51 +00:00
|
|
|
} else if (!properties || typeof properties != 'object') {
|
2017-09-15 20:16:42 +00:00
|
|
|
throw new Error(`Failed to read ObjectSchema: properties must be of type 'object', got (${typeof properties})`);
|
2016-02-18 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
schemas.splice(i, 1, schema);
|
|
|
|
constructors[name] = item;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let realmId = rpc.createRealm(Array.from(arguments));
|
2016-04-29 20:01:01 +00:00
|
|
|
setupRealm(this, realmId);
|
2016-05-06 22:42:17 +00:00
|
|
|
|
|
|
|
// This will create mappings between the id, path, and potential constructors.
|
|
|
|
objects.registerConstructors(realmId, this.path, constructors);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 01:34:20 +00:00
|
|
|
create(type, ...args) {
|
|
|
|
let method = util.createMethod(objectTypes.REALM, 'create', true);
|
2016-06-09 20:07:05 +00:00
|
|
|
return method.apply(this, [getObjectType(this, type), ...args]);
|
2016-02-25 01:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objects(type, ...args) {
|
|
|
|
let method = util.createMethod(objectTypes.REALM, 'objects');
|
2016-06-09 20:07:05 +00:00
|
|
|
return method.apply(this, [getObjectType(this, type), ...args]);
|
2016-02-25 01:34:20 +00:00
|
|
|
}
|
2016-06-03 23:43:36 +00:00
|
|
|
|
|
|
|
objectForPrimaryKey(type, ...args) {
|
|
|
|
let method = util.createMethod(objectTypes.REALM, 'objectForPrimaryKey');
|
2016-06-09 20:07:05 +00:00
|
|
|
return method.apply(this, [getObjectType(this, type), ...args]);
|
2016-06-03 23:43:36 +00:00
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 17:21:32 +00:00
|
|
|
// Non-mutating methods:
|
2015-10-19 23:19:43 +00:00
|
|
|
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
2016-05-02 19:41:53 +00:00
|
|
|
'addListener',
|
|
|
|
'removeListener',
|
|
|
|
'removeAllListeners',
|
2015-10-14 23:06:20 +00:00
|
|
|
'close',
|
2017-10-11 16:29:53 +00:00
|
|
|
'_waitForDownload',
|
2017-11-08 10:22:59 +00:00
|
|
|
'_objectForObjectId',
|
2015-10-28 17:21:32 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Mutating methods:
|
|
|
|
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
2015-10-08 16:43:38 +00:00
|
|
|
'delete',
|
2017-09-11 05:45:08 +00:00
|
|
|
'deleteModel',
|
2015-10-08 16:43:38 +00:00
|
|
|
'deleteAll',
|
2016-05-02 19:41:53 +00:00
|
|
|
'write',
|
2017-08-18 12:22:29 +00:00
|
|
|
'compact',
|
2017-08-21 15:48:53 +00:00
|
|
|
'beginTransaction',
|
|
|
|
'commitTransaction',
|
|
|
|
'cancelTransaction',
|
2015-10-28 17:21:32 +00:00
|
|
|
], true);
|
2015-10-08 16:43:38 +00:00
|
|
|
|
2017-01-31 13:07:29 +00:00
|
|
|
const Sync = {
|
2017-02-01 13:18:59 +00:00
|
|
|
User,
|
|
|
|
Session
|
2017-01-31 13:07:29 +00:00
|
|
|
};
|
|
|
|
|
2015-10-19 23:59:04 +00:00
|
|
|
Object.defineProperties(Realm, {
|
2016-03-09 07:47:08 +00:00
|
|
|
Collection: {
|
|
|
|
value: Collection,
|
|
|
|
},
|
2016-02-28 20:35:54 +00:00
|
|
|
List: {
|
2016-03-09 07:47:08 +00:00
|
|
|
value: List,
|
2016-02-28 20:35:54 +00:00
|
|
|
},
|
|
|
|
Results: {
|
2016-03-09 07:47:08 +00:00
|
|
|
value: Results,
|
2016-02-28 20:35:54 +00:00
|
|
|
},
|
2016-04-27 00:32:55 +00:00
|
|
|
Object: {
|
|
|
|
value: RealmObject,
|
|
|
|
},
|
2017-01-31 13:07:29 +00:00
|
|
|
Sync: {
|
|
|
|
value: Sync,
|
|
|
|
},
|
2015-10-19 23:59:04 +00:00
|
|
|
defaultPath: {
|
2015-10-20 22:10:22 +00:00
|
|
|
get: util.getterForProperty('defaultPath'),
|
|
|
|
set: util.setterForProperty('defaultPath'),
|
2015-10-19 23:59:04 +00:00
|
|
|
},
|
2016-03-18 16:51:04 +00:00
|
|
|
schemaVersion: {
|
2016-11-14 18:48:50 +00:00
|
|
|
value: function(_path, _encryptionKey) {
|
2016-03-18 16:51:04 +00:00
|
|
|
return rpc.callMethod(undefined, Realm[keys.id], 'schemaVersion', Array.from(arguments));
|
|
|
|
}
|
|
|
|
},
|
2017-08-30 04:55:30 +00:00
|
|
|
deleteFile: {
|
|
|
|
value: function(config) {
|
|
|
|
return rpc.callMethod(undefined, Realm[keys.id], 'deleteFile', Array.from(arguments));
|
|
|
|
}
|
|
|
|
},
|
2016-05-13 20:05:39 +00:00
|
|
|
copyBundledRealmFiles: {
|
|
|
|
value: function() {
|
|
|
|
return rpc.callMethod(undefined, Realm[keys.id], 'copyBundledRealmFiles', []);
|
|
|
|
}
|
|
|
|
},
|
2015-10-19 23:59:04 +00:00
|
|
|
clearTestState: {
|
2015-11-03 00:29:13 +00:00
|
|
|
value: function() {
|
2016-03-09 07:47:08 +00:00
|
|
|
collections.clearMutationListeners();
|
2016-05-06 22:42:17 +00:00
|
|
|
objects.clearRegisteredConstructors();
|
2015-11-03 00:29:13 +00:00
|
|
|
rpc.clearTestState();
|
|
|
|
},
|
2015-10-19 23:59:04 +00:00
|
|
|
},
|
2015-10-15 10:12:28 +00:00
|
|
|
});
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
for (let i = 0, len = debugHosts.length; i < len; i++) {
|
|
|
|
try {
|
2017-02-03 15:40:13 +00:00
|
|
|
let refreshAccessTokenCallback = staticUserMethods._refreshAccessToken.bind(User);
|
2016-02-29 11:24:51 +00:00
|
|
|
// The session ID refers to the Realm constructor object in the RPC server.
|
2017-02-03 15:40:13 +00:00
|
|
|
Realm[keys.id] = rpc.createSession(refreshAccessTokenCallback, debugHosts[i] + ':' + debugPort);
|
2016-02-29 11:24:51 +00:00
|
|
|
break;
|
|
|
|
} catch (e) {
|
|
|
|
// Only throw exception after all hosts have been tried.
|
|
|
|
if (i < len - 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log the original exception for debugging purposes.
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
throw new Error(
|
|
|
|
'Realm failed to connect to the embedded debug server inside the app. ' +
|
|
|
|
'If attempting to use Chrome debugging from a device, ensure the device is ' +
|
|
|
|
'reachable on the same network as this machine.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|