Named backlink queries working with core 5.3.0

This commit is contained in:
James Stone 2018-02-19 16:14:10 -08:00
parent 6610971898
commit bd5c237b0f
2 changed files with 23 additions and 37 deletions

View File

@ -141,18 +141,16 @@ typename T::Object ResultsClass<T>::create_instance(ContextType ctx, SharedRealm
return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(realm, *table)); return create_object<T, ResultsClass<T>>(ctx, new realm::js::Results<T>(realm, *table));
} }
void alias_backlinks(parser::KeypathMapping &mapping, const realm::SharedRealm &realm) void alias_backlinks(parser::KeyPathMapping &mapping, const realm::SharedRealm &realm)
{ {
const realm::Schema &schema = realm->schema(); const realm::Schema &schema = realm->schema();
for (auto it = schema.begin(); it != schema.end(); ++it) { for (auto it = schema.begin(); it != schema.end(); ++it) {
for (const Property &property : it->computed_properties) { for (const Property &property : it->computed_properties) {
if (property.type == realm::PropertyType::LinkingObjects) { if (property.type == realm::PropertyType::LinkingObjects) {
auto target_object_schema = schema.find(property.object_type); auto target_object_schema = schema.find(property.object_type);
auto link_property = target_object_schema->property_for_name(property.link_origin_property_name);
const TableRef table = ObjectStore::table_for_object_type(realm->read_group(), target_object_schema->name); const TableRef table = ObjectStore::table_for_object_type(realm->read_group(), target_object_schema->name);
std::string native_name = "@links." + std::string(table->get_name()) + "." + property.link_origin_property_name; std::string native_name = "@links." + std::string(table->get_name()) + "." + property.link_origin_property_name;
//std::cout << "mapping named backlink: " << property.name << " to: " << native_name << std::endl; mapping.add_mapping(table, property.name, native_name);
mapping.add_mapping(table, property.name, native_backlink_name);
} }
} }
} }
@ -170,7 +168,7 @@ typename T::Object ResultsClass<T>::create_filtered(ContextType ctx, const U &co
auto const &realm = collection.get_realm(); auto const &realm = collection.get_realm();
auto const &object_schema = collection.get_object_schema(); auto const &object_schema = collection.get_object_schema();
DescriptorOrdering ordering; DescriptorOrdering ordering;
parser::KeypathMapping mapping; parser::KeyPathMapping mapping;
alias_backlinks(mapping, realm); alias_backlinks(mapping, realm);
parser::ParserResult result = parser::parse(query_string); parser::ParserResult result = parser::parse(query_string);

View File

@ -83,6 +83,26 @@ module.exports = {
TestCase.assertArraysEqual(names(resultsC), ['JP']); TestCase.assertArraysEqual(names(resultsC), ['JP']);
}, },
testFilteredLinkingObjectsByName: function() {
var realm = new Realm({schema: [schemas.PersonObject]});
var christine, olivier;
realm.write(function() {
olivier = realm.create('PersonObject', {name: 'Olivier', age: 0});
christine = realm.create('PersonObject', {name: 'Christine', age: 25, children: [olivier]});
realm.create('PersonObject', {name: 'JP', age: 28, children: [olivier]});
});
let people = realm.objects('PersonObject')
TestCase.assertEqual(people.filtered('parents.age > 25').length, 1);
TestCase.assertEqual(people.filtered('parents.age > 25')[0].name, 'Olivier');
TestCase.assertEqual(people.filtered('parents.@count == 2').length, 1);
TestCase.assertEqual(people.filtered('parents.name CONTAINS[c] "chris"').length, 1);
TestCase.assertEqual(people.filtered('parents.name.@size == 2').length, 1);
TestCase.assertEqual(people.filtered('25 IN parents.age').length, 1);
},
testMethod: function() { testMethod: function() {
var realm = new Realm({schema: [schemas.PersonObject]}); var realm = new Realm({schema: [schemas.PersonObject]});
@ -124,36 +144,4 @@ module.exports = {
TestCase.assertEqual(oliviersParents.length, 0); TestCase.assertEqual(oliviersParents.length, 0);
}); });
}, },
testFilterByLinkingObject: function() {
const Realm = require('realm')
const ProductSchema = {
name: 'Product',
primaryKey:'productId',
properties: {
productId:'int',
product:'string',
combinations: {type: 'linkingObjects', objectType: 'Combination', property: 'products'}
}
}
const CombinationSchema = {
name: 'Combination',
properties: {
onSale: {type: 'string'},
products: {type: 'list', objectType:'Product'}
}
}
Realm.open({
schema: [ProductSchema, CombinationSchema]
}).then(function (realm) {
let queryFilter1 = 'onSale = "yes" AND products.productId = 1'
let data1 = realm.objects('Combination').filtered(queryFilter1)
let queryFilter2 = 'combinations.onSale = "yes" AND productId = 1'
let data2 = realm.objects('Product').filtered(queryFilter2)
})
},
}; };