Skip to content

Commit

Permalink
Merge pull request #1138 from realm/yg/realm-is-empty
Browse files Browse the repository at this point in the history
Add `Realm.empty`
  • Loading branch information
fealebenpae authored Jul 12, 2017
2 parents ced6df9 + 5dd9264 commit 479e8ee
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions docs/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function setupRealm(realm, realmId) {
realm[keys.type] = objectTypes.REALM;

[
'empty',
'path',
'readOnly',
'schema',
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
9 changes: 9 additions & 0 deletions src/js_realm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class RealmClass : public ClassDefinition<T, SharedRealm, ObservableClass<T>> {
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 &);
Expand Down Expand Up @@ -225,6 +226,7 @@ class RealmClass : public ClassDefinition<T, SharedRealm, ObservableClass<T>> {
};

PropertyMap<T> const properties = {
{"empty", {wrap<get_empty>, nullptr}},
{"path", {wrap<get_path>, nullptr}},
{"schemaVersion", {wrap<get_schema_version>, nullptr}},
{"schema", {wrap<get_schema>, nullptr}},
Expand Down Expand Up @@ -501,6 +503,13 @@ void RealmClass<T>::set_default_path(ContextType ctx, ObjectType object, ValueTy
js::set_default_path(Value::validated_to_string(ctx, value, "defaultPath"));
}

template<typename T>
void RealmClass<T>::get_empty(ContextType ctx, ObjectType object, ReturnValue &return_value) {
SharedRealm& realm = *get_internal<T, RealmClass<T>>(object);
bool is_empty = ObjectStore::is_empty(realm->read_group());
return_value.set(is_empty);
}

template<typename T>
void RealmClass<T>::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) {
std::string path = get_internal<T, RealmClass<T>>(object)->get()->config().path;
Expand Down
11 changes: 11 additions & 0 deletions tests/js/realm-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

0 comments on commit 479e8ee

Please sign in to comment.