2016-10-10 14:17:05 +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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-01-31 21:56:09 +00:00
|
|
|
let getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function(obj) {
|
|
|
|
return Object.getOwnPropertyNames(obj).reduce(function (descriptors, name) {
|
|
|
|
descriptors[name] = Object.getOwnPropertyDescriptor(obj, name);
|
|
|
|
return descriptors;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2017-02-02 01:41:11 +00:00
|
|
|
function setConstructorOnPrototype(klass) {
|
|
|
|
if (klass.prototype.constructor !== klass) {
|
|
|
|
Object.defineProperty(klass.prototype, 'constructor', { value: klass, configurable: true, writable: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 14:17:05 +00:00
|
|
|
module.exports = function(realmConstructor) {
|
|
|
|
// Add the specified Array methods to the Collection prototype.
|
|
|
|
Object.defineProperties(realmConstructor.Collection.prototype, require('./collection-methods'));
|
|
|
|
|
2017-02-02 01:41:11 +00:00
|
|
|
setConstructorOnPrototype(realmConstructor.Collection);
|
|
|
|
setConstructorOnPrototype(realmConstructor.List);
|
|
|
|
setConstructorOnPrototype(realmConstructor.Results);
|
|
|
|
setConstructorOnPrototype(realmConstructor.Object);
|
|
|
|
|
2017-05-06 23:26:14 +00:00
|
|
|
//Add async open API
|
|
|
|
Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({
|
|
|
|
open(config) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const realm = new Realm(config);
|
|
|
|
realm.wait((error) => {
|
|
|
|
if (error) {
|
|
|
|
reject(new Error("Cannot asynchronously open synced Realm, because the associated session previously experienced a fatal error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(realm);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
openAsync(config, callback) {
|
|
|
|
const realm = new Realm(config);
|
|
|
|
realm.wait((error) => {
|
|
|
|
if (error) {
|
|
|
|
callback(new Error("Cannot asynchronously open synced Realm, because the associated session previously experienced a fatal error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, realm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2016-10-10 14:17:05 +00:00
|
|
|
// Add sync methods
|
|
|
|
if (realmConstructor.Sync) {
|
2017-01-31 21:56:09 +00:00
|
|
|
let userMethods = require('./user-methods');
|
|
|
|
Object.defineProperties(realmConstructor.Sync.User, getOwnPropertyDescriptors(userMethods.static));
|
|
|
|
Object.defineProperties(realmConstructor.Sync.User.prototype, getOwnPropertyDescriptors(userMethods.instance));
|
2017-02-08 12:36:43 +00:00
|
|
|
Object.defineProperty(realmConstructor.Sync.User, '_realmConstructor', { value: realmConstructor });
|
2017-01-31 21:56:09 +00:00
|
|
|
|
2016-10-24 22:12:12 +00:00
|
|
|
realmConstructor.Sync.AuthError = require('./errors').AuthError;
|
2016-10-25 22:04:58 +00:00
|
|
|
|
|
|
|
if (realmConstructor.Sync.cleanup) {
|
2016-11-14 18:48:50 +00:00
|
|
|
// FIXME: DOES THIS WORK ON BOTH NODE AND REACT NATIVE?
|
|
|
|
process.on('exit', realmConstructor.Sync.cleanup);
|
2016-11-20 18:34:13 +00:00
|
|
|
process.on('SIGINT', function () {
|
|
|
|
realmConstructor.Sync.cleanup();
|
|
|
|
process.exit(2);
|
|
|
|
});
|
|
|
|
process.on('uncaughtException', function(e) {
|
|
|
|
realmConstructor.Sync.cleanup();
|
2016-11-21 23:33:37 +00:00
|
|
|
/* eslint-disable no-console */
|
2016-11-20 18:34:13 +00:00
|
|
|
console.log(e.stack);
|
|
|
|
process.exit(99);
|
|
|
|
});
|
2016-10-25 22:04:58 +00:00
|
|
|
}
|
2017-02-02 01:41:11 +00:00
|
|
|
|
|
|
|
setConstructorOnPrototype(realmConstructor.Sync.User);
|
|
|
|
setConstructorOnPrototype(realmConstructor.Sync.Session);
|
2017-03-22 12:52:41 +00:00
|
|
|
} else {
|
|
|
|
Object.defineProperty(realmConstructor, 'Sync', {
|
|
|
|
get: function () {
|
|
|
|
throw new Error("Realm.Sync is not available. Note that the developer edition of the Node.JS SDK for Realm does not include sync on Linux.");
|
|
|
|
}
|
|
|
|
})
|
2016-10-10 14:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove this now useless object.
|
|
|
|
var types = Object.freeze({
|
|
|
|
'BOOL': 'bool',
|
|
|
|
'INT': 'int',
|
|
|
|
'FLOAT': 'float',
|
|
|
|
'DOUBLE': 'double',
|
|
|
|
'STRING': 'string',
|
|
|
|
'DATE': 'date',
|
|
|
|
'DATA': 'data',
|
|
|
|
'OBJECT': 'object',
|
|
|
|
'LIST': 'list',
|
|
|
|
});
|
|
|
|
Object.defineProperty(realmConstructor, 'Types', {
|
|
|
|
get: function() {
|
|
|
|
if (typeof console != 'undefined') {
|
|
|
|
/* global console */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
var stack = new Error().stack.split("\n").slice(2).join("\n");
|
|
|
|
var msg = '`Realm.Types` is deprecated! Please specify the type name as lowercase string instead!\n'+stack;
|
|
|
|
if (console.warn != undefined) {
|
|
|
|
console.warn(msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
}
|
|
|
|
return types;
|
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
2017-03-22 12:52:41 +00:00
|
|
|
}
|