diff --git a/src/RJSRealm.mm b/src/RJSRealm.mm index 4554686f..f64e47e8 100644 --- a/src/RJSRealm.mm +++ b/src/RJSRealm.mm @@ -257,7 +257,7 @@ JSValueRef RealmDelete(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO } if (!JSValueIsObjectOfClass(ctx, arguments[0], RJSObjectClass())) { - throw std::runtime_error("Argument to 'delete' must be a Realm object."); + throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects."); } Object *object = RJSGetInternal(RJSValidatedValueToObject(ctx, arguments[0])); @@ -281,6 +281,29 @@ JSValueRef RealmDelete(JSContextRef ctx, JSObjectRef function, JSObjectRef thisO } } +JSValueRef RealmDeleteAll(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { + try { + RJSValidateArgumentCount(argumentCount, 0); + + SharedRealm realm = *RJSGetInternal(thisObject); + + if (!realm->is_in_transaction()) { + throw std::runtime_error("Can only delete objects within a transaction."); + } + + for (auto objectSchema : *realm->config().schema) { + ObjectStore::table_for_object_type(realm->read_group(), objectSchema.first)->clear(); + } + return NULL; + } + catch (std::exception &exp) { + if (jsException) { + *jsException = RJSMakeError(ctx, exp); + } + return NULL; + } +} + JSValueRef RealmWrite(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { RJSValidateArgumentCount(argumentCount, 1); @@ -354,6 +377,7 @@ JSClassRef RJSRealmClass() { {"objects", RealmObjects, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"create", RealmCreateObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"delete", RealmDelete, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"deleteAll", RealmDeleteAll, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"write", RealmWrite, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"addNotification", RealmAddNotification, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL}, diff --git a/tests/RealmTests.js b/tests/RealmTests.js index 58ad0323..760ea37b 100644 --- a/tests/RealmTests.js +++ b/tests/RealmTests.js @@ -196,6 +196,28 @@ var RealmTests = { }); }, + testDeleteAll: function() { + var realm = new Realm({schema: [TestObjectSchema, IntPrimaryObjectSchema]}); + realm.write(function() { + realm.create('TestObject', [1]); + realm.create('TestObject', [2]); + realm.create('IntPrimaryObject', [2, 'value']); + }); + TestCase.assertEqual(realm.objects('TestObject').length, 2); + TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 1); + + TestCase.assertThrows(function() { + realm.deleteAll(); + }); + + realm.write(function() { + realm.deleteAll(); + }); + + TestCase.assertEqual(realm.objects('TestObject').length, 0); + TestCase.assertEqual(realm.objects('IntPrimaryObject').length, 0); + }, + testRealmObjects: function() { var realm = new Realm({schema: [PersonObject]}); realm.write(function() {