Add `Realm.empty`

This commit is contained in:
Yavor Georgiev 2017-07-12 13:02:21 +02:00
parent ced6df9819
commit 5dd9264225
No known key found for this signature in database
GPG Key ID: 83FC145DA0CCA9C3
6 changed files with 31 additions and 1 deletions

View File

@ -4,7 +4,7 @@ vNext Release notes (TBD)
* None * None
### Enhancements ### Enhancements
* None * Added `Realm.prototype.empty` which is a property that indicates whether or not the realm has any objects in it.
### Bug fixes ### Bug fixes
* Fix crash on Node.js when a listener callback throws an error. * Fix crash on Node.js when a listener callback throws an error.

View File

@ -23,6 +23,14 @@
* ``` * ```
*/ */
class Realm { 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. * The path to the file where this Realm is stored.
* @type {string} * @type {string}

View File

@ -52,6 +52,7 @@ function setupRealm(realm, realmId) {
realm[keys.type] = objectTypes.REALM; realm[keys.type] = objectTypes.REALM;
[ [
'empty',
'path', 'path',
'readOnly', 'readOnly',
'schema', 'schema',

1
lib/index.d.ts vendored
View File

@ -352,6 +352,7 @@ declare namespace Realm.Sync {
declare class Realm { declare class Realm {
static defaultPath: string; static defaultPath: string;
readonly empty: boolean;
readonly path: string; readonly path: string;
readonly readOnly: boolean; readonly readOnly: boolean;
readonly schema: Realm.ObjectSchema[]; readonly schema: Realm.ObjectSchema[];

View File

@ -178,6 +178,7 @@ public:
static void close(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void close(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
// properties // properties
static void get_empty(ContextType, ObjectType, ReturnValue &);
static void get_path(ContextType, ObjectType, ReturnValue &); static void get_path(ContextType, ObjectType, ReturnValue &);
static void get_schema_version(ContextType, ObjectType, ReturnValue &); static void get_schema_version(ContextType, ObjectType, ReturnValue &);
static void get_schema(ContextType, ObjectType, ReturnValue &); static void get_schema(ContextType, ObjectType, ReturnValue &);
@ -225,6 +226,7 @@ public:
}; };
PropertyMap<T> const properties = { PropertyMap<T> const properties = {
{"empty", {wrap<get_empty>, nullptr}},
{"path", {wrap<get_path>, nullptr}}, {"path", {wrap<get_path>, nullptr}},
{"schemaVersion", {wrap<get_schema_version>, nullptr}}, {"schemaVersion", {wrap<get_schema_version>, nullptr}},
{"schema", {wrap<get_schema>, nullptr}}, {"schema", {wrap<get_schema>, nullptr}},
@ -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")); 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> template<typename T>
void RealmClass<T>::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) { void RealmClass<T>::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) {
std::string path = get_internal<T, RealmClass<T>>(object)->get()->config().path; std::string path = get_internal<T, RealmClass<T>>(object)->get()->config().path;

View File

@ -932,5 +932,16 @@ module.exports = {
var p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children }); var p3 = realm.create('PersonObject', { name: 'Wendy', age: 52, children: p2.children });
TestCase.assertEqual(p3.children.length, 1); 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);
} }
}; };