From bd5c237b0f5bbaab1496171919bce1d6c17bf46b Mon Sep 17 00:00:00 2001 From: James Stone Date: Mon, 19 Feb 2018 16:14:10 -0800 Subject: [PATCH] Named backlink queries working with core 5.3.0 --- src/js_results.hpp | 8 ++--- tests/js/linkingobjects-tests.js | 52 ++++++++++++-------------------- 2 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/js_results.hpp b/src/js_results.hpp index 9a7b380b..f825692f 100644 --- a/src/js_results.hpp +++ b/src/js_results.hpp @@ -141,18 +141,16 @@ typename T::Object ResultsClass::create_instance(ContextType ctx, SharedRealm return create_object>(ctx, new realm::js::Results(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(); for (auto it = schema.begin(); it != schema.end(); ++it) { for (const Property &property : it->computed_properties) { if (property.type == realm::PropertyType::LinkingObjects) { 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); 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_backlink_name); + mapping.add_mapping(table, property.name, native_name); } } } @@ -170,7 +168,7 @@ typename T::Object ResultsClass::create_filtered(ContextType ctx, const U &co auto const &realm = collection.get_realm(); auto const &object_schema = collection.get_object_schema(); DescriptorOrdering ordering; - parser::KeypathMapping mapping; + parser::KeyPathMapping mapping; alias_backlinks(mapping, realm); parser::ParserResult result = parser::parse(query_string); diff --git a/tests/js/linkingobjects-tests.js b/tests/js/linkingobjects-tests.js index 943a6aac..d5aff0e9 100644 --- a/tests/js/linkingobjects-tests.js +++ b/tests/js/linkingobjects-tests.js @@ -83,6 +83,26 @@ module.exports = { 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() { var realm = new Realm({schema: [schemas.PersonObject]}); @@ -124,36 +144,4 @@ module.exports = { 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) - }) - }, };