mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-30 08:15:55 +00:00
Merge pull request #369 from realm/al-create-optionals
Don't require optional or link properties when creating objects
This commit is contained in:
commit
b820b28688
@ -208,11 +208,13 @@ namespace realm {
|
|||||||
case PropertyTypeArray: {
|
case PropertyTypeArray: {
|
||||||
realm::LinkViewRef link_view = m_row.get_linklist(column);
|
realm::LinkViewRef link_view = m_row.get_linklist(column);
|
||||||
link_view->clear();
|
link_view->clear();
|
||||||
|
if (!Accessor::is_null(ctx, value)) {
|
||||||
size_t count = Accessor::list_size(ctx, value);
|
size_t count = Accessor::list_size(ctx, value);
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
ValueType element = Accessor::list_value_at_index(ctx, value, i);
|
ValueType element = Accessor::list_value_at_index(ctx, value, i);
|
||||||
link_view->add(Accessor::to_object_index(ctx, m_realm, element, property.object_type, try_update));
|
link_view->add(Accessor::to_object_index(ctx, m_realm, element, property.object_type, try_update));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -310,6 +312,9 @@ namespace realm {
|
|||||||
if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) {
|
if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) {
|
||||||
object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update);
|
object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update);
|
||||||
}
|
}
|
||||||
|
else if (prop.is_nullable || prop.type == PropertyTypeArray) {
|
||||||
|
object.set_property_value_impl(ctx, prop, Accessor::null_value(ctx), try_update);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw MissingPropertyValueException(object_schema.name, prop.name,
|
throw MissingPropertyValueException(object_schema.name, prop.name,
|
||||||
"Missing property value for property " + prop.name);
|
"Missing property value for property " + prop.name);
|
||||||
|
@ -163,7 +163,7 @@ module.exports = BaseTest.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
testRealmCreate: function() {
|
testRealmCreate: function() {
|
||||||
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
|
var realm = new Realm({schema: [schemas.TestObject]});
|
||||||
|
|
||||||
TestCase.assertThrows(function() {
|
TestCase.assertThrows(function() {
|
||||||
realm.create('TestObject', {doubleCol: 1});
|
realm.create('TestObject', {doubleCol: 1});
|
||||||
@ -178,8 +178,11 @@ module.exports = BaseTest.extend({
|
|||||||
TestCase.assertEqual(objects.length, 2, 'wrong object count');
|
TestCase.assertEqual(objects.length, 2, 'wrong object count');
|
||||||
TestCase.assertEqual(objects[0].doubleCol, 1, 'wrong object property value');
|
TestCase.assertEqual(objects[0].doubleCol, 1, 'wrong object property value');
|
||||||
TestCase.assertEqual(objects[1].doubleCol, 2, 'wrong object property value');
|
TestCase.assertEqual(objects[1].doubleCol, 2, 'wrong object property value');
|
||||||
|
},
|
||||||
|
|
||||||
|
testRealmCreatePrimaryKey: function() {
|
||||||
|
var realm = new Realm({schema: [schemas.IntPrimary]});
|
||||||
|
|
||||||
// test int primary object
|
|
||||||
realm.write(function() {
|
realm.write(function() {
|
||||||
var obj0 = realm.create('IntPrimaryObject', {
|
var obj0 = realm.create('IntPrimaryObject', {
|
||||||
primaryCol: 0,
|
primaryCol: 0,
|
||||||
@ -212,8 +215,24 @@ module.exports = BaseTest.extend({
|
|||||||
realm.create('IntPrimaryObject', {primaryCol: 0}, true);
|
realm.create('IntPrimaryObject', {primaryCol: 0}, true);
|
||||||
TestCase.assertEqual(obj0.valueCol, 'newVal0');
|
TestCase.assertEqual(obj0.valueCol, 'newVal0');
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// test upsert with all type and string primary object
|
testRealmCreateOptionals: function() {
|
||||||
|
var realm = new Realm({schema: [schemas.NullableBasicTypes, schemas.LinkTypes, schemas.TestObject]});
|
||||||
|
var basic, links;
|
||||||
|
realm.write(function() {
|
||||||
|
basic = realm.create('NullableBasicTypesObject', {});
|
||||||
|
links = realm.create('LinkTypesObject', {});
|
||||||
|
});
|
||||||
|
for (var name in schemas.NullableBasicTypes.properties) {
|
||||||
|
TestCase.assertEqual(basic[name], null);
|
||||||
|
}
|
||||||
|
TestCase.assertEqual(links.objectCol, null);
|
||||||
|
TestCase.assertEqual(links.arrayCol.length, 0);
|
||||||
|
},
|
||||||
|
|
||||||
|
testRealmCreateUpsert: function() {
|
||||||
|
var realm = new Realm({schema: [schemas.IntPrimary, schemas.AllTypes, schemas.TestObject]});
|
||||||
realm.write(function() {
|
realm.write(function() {
|
||||||
var values = {
|
var values = {
|
||||||
primaryCol: '0',
|
primaryCol: '0',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user