mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 06:46:03 +00:00
support for read-only realms
This commit is contained in:
parent
a2ed550dde
commit
b9b3812c86
@ -246,12 +246,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 +261,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 +270,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 +290,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 +303,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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user