Merge pull request #405 from realm/al-read-only

Support for readOnly realms
This commit is contained in:
Ari Lazier 2016-04-29 13:53:15 -07:00
commit 29c214021a
6 changed files with 54 additions and 13 deletions

View File

@ -54,7 +54,7 @@ export function fireMutationListeners(realmId) {
export function createCollection(prototype, realmId, info, mutable) {
let collection = Object.create(prototype);
let size = 0;
let size;
Object.defineProperties(collection, {
'length': {
@ -70,7 +70,10 @@ export function createCollection(prototype, realmId, info, mutable) {
length = collection.length;
}
if (length == size) {
return;
return; // Nothing has changed.
}
if (size == null) {
size = 0; // This is first pass.
}
let props = {};
@ -79,7 +82,7 @@ export function createCollection(prototype, realmId, info, mutable) {
for (let i = size; i < length; i++) {
props[i] = {
get: getterForProperty(i),
set: mutable ? setterForProperty(i) : undefined,
set: setterForProperty(i),
enumerable: true,
configurable: true,
};
@ -93,7 +96,8 @@ export function createCollection(prototype, realmId, info, mutable) {
// Helpfully throw an exception on attempts to set to one past the last index.
props[length] = {
value: undefined,
get: getterForProperty(length),
set: setterForProperty(length),
configurable: true,
};

View File

@ -71,6 +71,8 @@ export default class Realm {
[
'path',
'readOnly',
'schema',
'schemaVersion',
].forEach((name) => {
Object.defineProperty(this, name, {get: util.getterForProperty(name)});

View File

@ -152,7 +152,7 @@ function deserializeDict(realmId, info) {
let object = {};
for (let i = 0, len = keys.length; i < len; i++) {
object[keys[i]] = values[i];
object[keys[i]] = deserialize(realmId, values[i]);
}
return object;

View File

@ -148,6 +148,7 @@ class Realm {
static void get_path(ContextType, ObjectType, ReturnValue &);
static void get_schema_version(ContextType, ObjectType, ReturnValue &);
static void get_schema(ContextType, ObjectType, ReturnValue &);
static void get_read_only(ContextType, ObjectType, ReturnValue &);
// static methods
static void constructor(ContextType, ObjectType, size_t, const ValueType[]);
@ -224,6 +225,7 @@ struct RealmClass : ClassDefinition<T, SharedRealm> {
{"path", {wrap<Realm::get_path>, nullptr}},
{"schemaVersion", {wrap<Realm::get_schema_version>, nullptr}},
{"schema", {wrap<Realm::get_schema>, nullptr}},
{"readOnly", {wrap<Realm::get_read_only>, nullptr}},
};
};
@ -246,12 +248,6 @@ inline typename T::Function Realm<T>::create_constructor(ContextType ctx) {
template<typename T>
void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[]) {
static const String path_string = "path";
static const String schema_string = "schema";
static const String schema_version_string = "schemaVersion";
static const String encryption_key_string = "encryptionKey";
static const String migration_string = "migration";
realm::Realm::Config config;
typename Schema<T>::ObjectDefaultsMap defaults;
typename Schema<T>::ConstructorMap constructors;
@ -267,6 +263,7 @@ void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc,
else if (Value::is_object(ctx, value)) {
ObjectType object = Value::validated_to_object(ctx, value);
static const String path_string = "path";
ValueType path_value = Object::get_property(ctx, object, path_string);
if (!Value::is_undefined(ctx, path_value)) {
config.path = Value::validated_to_string(ctx, path_value, "path");
@ -275,12 +272,18 @@ void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc,
config.path = js::default_path();
}
static const String read_only_string = "readOnly";
ValueType read_only_value = Object::get_property(ctx, object, read_only_string);
config.read_only = Value::is_undefined(ctx, read_only_value) ? false : Value::validated_to_boolean(ctx, read_only_value, "readOnly");
static const String schema_string = "schema";
ValueType schema_value = Object::get_property(ctx, object, schema_string);
if (!Value::is_undefined(ctx, schema_value)) {
ObjectType schema_object = Value::validated_to_object(ctx, schema_value, "schema");
config.schema.reset(new realm::Schema(Schema<T>::parse_schema(ctx, schema_object, defaults, constructors)));
}
static const String schema_version_string = "schemaVersion";
ValueType version_value = Object::get_property(ctx, object, schema_version_string);
if (!Value::is_undefined(ctx, version_value)) {
config.schema_version = Value::validated_to_number(ctx, version_value, "schemaVersion");
@ -289,6 +292,7 @@ void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc,
config.schema_version = 0;
}
static const String migration_string = "migration";
ValueType migration_value = Object::get_property(ctx, object, migration_string);
if (!Value::is_undefined(ctx, migration_value)) {
FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration");
@ -301,6 +305,8 @@ void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc,
};
}
static const String encryption_key_string = "encryptionKey";
ValueType encryption_key_value = Object::get_property(ctx, object, encryption_key_string);
if (!Value::is_undefined(ctx, encryption_key_value)) {
std::string encryption_key = NativeAccessor::to_binary(ctx, encryption_key_value);
@ -383,6 +389,11 @@ void Realm<T>::get_schema(ContextType ctx, ObjectType object, ReturnValue &retur
return_value.set(Schema<T>::object_for_schema(ctx, *schema));
}
template<typename T>
void Realm<T>::get_read_only(ContextType ctx, ObjectType object, ReturnValue &return_value) {
return_value.set(get_internal<T, RealmClass<T>>(object)->get()->config().read_only);
}
template<typename T>
void Realm<T>::objects(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 1);

View File

@ -275,7 +275,7 @@ json RPCServer::serialize_json_value(JSValueRef js_value) {
JSValueRef js_value = jsc::Object::get_property(m_context, js_object, js_key);
keys.push_back(js_key);
values.push_back(js_value);
values.push_back(serialize_json_value(js_value));
}
return {

View File

@ -119,6 +119,30 @@ module.exports = BaseTest.extend({
}, 'The schema should be an array of ObjectSchema objects');
},
testRealmConstructorReadOnly: function() {
var realm = new Realm({schema: [schemas.TestObject]});
realm.write(function() {
realm.create('TestObject', [1])
});
TestCase.assertEqual(realm.readOnly, false);
realm.close();
realm = new Realm({readOnly: true, schema: [schemas.TestObject]});
var objects = realm.objects('TestObject');
TestCase.assertEqual(objects.length, 1);
TestCase.assertEqual(objects[0].doubleCol, 1.0);
TestCase.assertEqual(realm.readOnly, true);
TestCase.assertThrows(function() {
realm.write(function() {});
});
realm.close();
realm = new Realm({readOnly: true});
TestCase.assertEqual(realm.schema.length, 1);
TestCase.assertEqual(realm.readOnly, true);
},
testDefaultPath: function() {
var defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);