diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 417364a5..e3c33e04 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -246,12 +246,6 @@ inline typename T::Function Realm::create_constructor(ContextType ctx) { template void Realm::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::ObjectDefaultsMap defaults; typename Schema::ConstructorMap constructors; @@ -267,6 +261,7 @@ void Realm::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"); @@ -274,13 +269,19 @@ void Realm::constructor(ContextType ctx, ObjectType this_object, size_t argc, else { 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::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"); @@ -288,7 +289,8 @@ void Realm::constructor(ContextType ctx, ObjectType this_object, size_t argc, else { 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 +303,8 @@ void Realm::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); diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index b83462e3..c8df1596 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -119,6 +119,27 @@ 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]) + }); + 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.assertThrows(function() { + realm.write(function() {}); + }); + realm.close(); + + realm = new Realm({readOnly: true}); + TestCase.assertEqual(realm.schema.length, 1); + }, + testDefaultPath: function() { var defaultRealm = new Realm({schema: []}); TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);