From 5dd926422529d129a74a56e528fb2516f76663c3 Mon Sep 17 00:00:00 2001 From: Yavor Georgiev Date: Wed, 12 Jul 2017 13:02:21 +0200 Subject: [PATCH] Add `Realm.empty` --- CHANGELOG.md | 2 +- docs/realm.js | 8 ++++++++ lib/browser/index.js | 1 + lib/index.d.ts | 1 + src/js_realm.hpp | 9 +++++++++ tests/js/realm-tests.js | 11 +++++++++++ 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d45805..f8cd8ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ vNext Release notes (TBD) * None ### Enhancements -* None +* Added `Realm.prototype.empty` which is a property that indicates whether or not the realm has any objects in it. ### Bug fixes * Fix crash on Node.js when a listener callback throws an error. diff --git a/docs/realm.js b/docs/realm.js index 20fc99e3..0d7b5fca 100644 --- a/docs/realm.js +++ b/docs/realm.js @@ -23,6 +23,14 @@ * ``` */ class Realm { + /** + * Indicates if this Realm contains any objects. + * @type {boolean} + * @readonly + * @since 1.10.0 + */ + get empty() {} + /** * The path to the file where this Realm is stored. * @type {string} diff --git a/lib/browser/index.js b/lib/browser/index.js index e66ab2b9..81b2a804 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -52,6 +52,7 @@ function setupRealm(realm, realmId) { realm[keys.type] = objectTypes.REALM; [ + 'empty', 'path', 'readOnly', 'schema', diff --git a/lib/index.d.ts b/lib/index.d.ts index 2fe62125..196b83d5 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -352,6 +352,7 @@ declare namespace Realm.Sync { declare class Realm { static defaultPath: string; + readonly empty: boolean; readonly path: string; readonly readOnly: boolean; readonly schema: Realm.ObjectSchema[]; diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 7614667d..2d9ac368 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -178,6 +178,7 @@ public: static void close(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); // properties + static void get_empty(ContextType, ObjectType, ReturnValue &); static void get_path(ContextType, ObjectType, ReturnValue &); static void get_schema_version(ContextType, ObjectType, ReturnValue &); static void get_schema(ContextType, ObjectType, ReturnValue &); @@ -225,6 +226,7 @@ public: }; PropertyMap const properties = { + {"empty", {wrap, nullptr}}, {"path", {wrap, nullptr}}, {"schemaVersion", {wrap, nullptr}}, {"schema", {wrap, nullptr}}, @@ -501,6 +503,13 @@ void RealmClass::set_default_path(ContextType ctx, ObjectType object, ValueTy js::set_default_path(Value::validated_to_string(ctx, value, "defaultPath")); } +template +void RealmClass::get_empty(ContextType ctx, ObjectType object, ReturnValue &return_value) { + SharedRealm& realm = *get_internal>(object); + bool is_empty = ObjectStore::is_empty(realm->read_group()); + return_value.set(is_empty); +} + template void RealmClass::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { std::string path = get_internal>(object)->get()->config().path; diff --git a/tests/js/realm-tests.js b/tests/js/realm-tests.js index 7ec5c8dd..a2c335e0 100644 --- a/tests/js/realm-tests.js +++ b/tests/js/realm-tests.js @@ -932,5 +932,16 @@ module.exports = { var p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children }); TestCase.assertEqual(p3.children.length, 1); }); + }, + + testEmpty: function() { + const realm = new Realm({schema: [schemas.PersonObject]}); + TestCase.assertTrue(realm.empty); + + realm.write(() => realm.create('PersonObject', { name: 'Ari', age: 10 })); + TestCase.assertTrue(!realm.empty); + + realm.write(() => realm.delete(realm.objects('PersonObject'))); + TestCase.assertTrue(realm.empty); } };