From 5186eeb84ed84b06ea8f03b5a35c328d4b26fbd8 Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 15 May 2018 14:31:54 +0200 Subject: [PATCH 1/3] Add Object.linkingObjectCount() --- CHANGELOG.md | 20 ++++++++++++++++++++ docs/object.js | 7 +++++++ lib/index.d.ts | 5 +++++ src/js_realm_object.hpp | 11 +++++++++++ tests/js/linkingobjects-tests.js | 25 +++++++++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 196acdf5..5f411232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,23 @@ +X.Y.Z Release notes () +============================================================= +### Compatibility +* Sync protocol: 24 +* Server-side history format: 4 +* File format: 7 +* Realm Object Server: 3.0.0 or later + +### Breaking changes +* None. + +### Enhancements +* Added `Object.linkingObjectsCount()` method, that returns total count of incoming links. + +### Bug fixes +* None + +### Internal +* None. + 2.5.0 Release notes (2018-5-14) ============================================================= ### Compatibility diff --git a/docs/object.js b/docs/object.js index 83cc33a5..c0603646 100644 --- a/docs/object.js +++ b/docs/object.js @@ -46,4 +46,11 @@ class Object { * @since 1.9.0 */ linkingObjects(objectType, property) {} + + /** + * Returns the total count of incoming links to this object + * @returns {number} number of links to this object. + * @since 2.6.0 + */ + linkingObjectsCount(objectType, property) {} } diff --git a/lib/index.d.ts b/lib/index.d.ts index 2f57990a..9bbf5eba 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -112,6 +112,11 @@ declare namespace Realm { * @returns Results */ linkingObjects(objectType: string, property: string): Results; + + /** + * @returns number + */ + linkingObjectsCount(): number; } const Object: { diff --git a/src/js_realm_object.hpp b/src/js_realm_object.hpp index b823d2c4..4ccf84b6 100644 --- a/src/js_realm_object.hpp +++ b/src/js_realm_object.hpp @@ -53,6 +53,7 @@ struct RealmObjectClass : ClassDefinition { static void is_valid(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); static void get_object_schema(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); static void linking_objects(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); + static void linking_objects_count(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); static void get_object_id(ContextType, ObjectType, Arguments, ReturnValue &); static void is_same_object(ContextType, ObjectType, Arguments, ReturnValue &); @@ -68,6 +69,7 @@ struct RealmObjectClass : ClassDefinition { {"isValid", wrap}, {"objectSchema", wrap}, {"linkingObjects", wrap}, + {"linkingObjectsCount", wrap}, {"_objectId", wrap}, {"_isSameObject", wrap}, }; @@ -200,6 +202,15 @@ void RealmObjectClass::is_same_object(ContextType ctx, ObjectType object, Arg return_value.set(self->row().get_table() == other->row().get_table() && self->row().get_index() == other->row().get_index()); } + +template +void RealmObjectClass::linking_objects_count(ContextType ctx, FunctionType, ObjectType object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { + auto realm_object = get_internal>(object); + const Row& row = realm_object->row(); + + return_value.set((uint32_t)row.get_backlink_count()); +} + } // js } // realm diff --git a/tests/js/linkingobjects-tests.js b/tests/js/linkingobjects-tests.js index a7e6fb6d..252be90e 100644 --- a/tests/js/linkingobjects-tests.js +++ b/tests/js/linkingobjects-tests.js @@ -195,4 +195,29 @@ module.exports = { TestCase.assertEqual(oliviersParents.length, 0); }); }, + + testLinkingObjectsCount: function() { + var realm = new Realm({schema: [schemas.PersonObject]}); + + var john; + realm.write(function () { + john = realm.create('PersonObject', { name: 'John', age: 50 }); + }); + + TestCase.assertEqual(john.linkingObjectsCount(), 0); + + var olivier; + realm.write(function() { + olivier = realm.create('PersonObject', {name: 'Olivier', age: 0}); + realm.create('PersonObject', {name: 'Christine', age: 25, children: [olivier]}); + }); + + TestCase.assertEqual(olivier.linkingObjectsCount(), 1); + + realm.write(function() { + john.children.push(olivier); + }); + + TestCase.assertEqual(olivier.linkingObjectsCount(), 2); + } }; From fbf4db56c513b04caf36a4d035144088d86db054 Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 15 May 2018 14:37:04 +0200 Subject: [PATCH 2/3] Fix Chrome debug API --- lib/browser/objects.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/browser/objects.js b/lib/browser/objects.js index aeea2b45..d9264de2 100644 --- a/lib/browser/objects.js +++ b/lib/browser/objects.js @@ -32,6 +32,7 @@ createMethods(RealmObject.prototype, objectTypes.OBJECT, [ 'isValid', 'objectSchema', 'linkingObjects', + 'linkingObjectsCount', '_objectId', '_isSameObject', ]); From 0783c9081c898430216d219abcdd4a0def5feb44 Mon Sep 17 00:00:00 2001 From: astigsen Date: Tue, 15 May 2018 19:46:20 +0200 Subject: [PATCH 3/3] Fixed method signature in docs --- docs/object.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/object.js b/docs/object.js index c0603646..785eb104 100644 --- a/docs/object.js +++ b/docs/object.js @@ -52,5 +52,5 @@ class Object { * @returns {number} number of links to this object. * @since 2.6.0 */ - linkingObjectsCount(objectType, property) {} + linkingObjectsCount() {} }