mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-22 12:30:58 +00:00
add Realm.delete
This commit is contained in:
parent
ca4dfa052c
commit
dc0839772f
@ -169,9 +169,8 @@ template<> size_t RJSAccessor::to_object_index(JSContextRef ctx, SharedRealm &re
|
||||
return RJSGetInternal<Object *>(object)->row.get_index();
|
||||
}
|
||||
|
||||
static JSStringRef arrayString = JSStringCreateWithUTF8CString("Array");
|
||||
ObjectSchema &object_schema = realm->config().schema->at(prop.object_type);
|
||||
if (RJSIsValueObjectOfType(ctx, object, arrayString)) {
|
||||
if (RJSIsValueArray(ctx, object)) {
|
||||
object = RJSDictForPropertyArray(ctx, object_schema, object);
|
||||
}
|
||||
|
||||
|
@ -211,13 +211,8 @@ JSValueRef RealmCreateObject(JSContextRef ctx, JSObjectRef function, JSObjectRef
|
||||
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
|
||||
ObjectSchema &object_schema = sharedRealm->config().schema->at(className);
|
||||
|
||||
JSObjectRef object = JSValueToObject(ctx, arguments[1], jsException);
|
||||
if (!object) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static JSStringRef arrayString = JSStringCreateWithUTF8CString("Array");
|
||||
if (RJSIsValueObjectOfType(ctx, arguments[1], arrayString)) {
|
||||
JSObjectRef object = RJSValidatedValueToObject(ctx, arguments[1]);
|
||||
if (RJSIsValueArray(ctx, arguments[1])) {
|
||||
object = RJSDictForPropertyArray(ctx, object_schema, object);
|
||||
}
|
||||
|
||||
@ -231,21 +226,58 @@ JSValueRef RealmCreateObject(JSContextRef ctx, JSObjectRef function, JSObjectRef
|
||||
}
|
||||
}
|
||||
|
||||
JSValueRef RealmDelete(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
||||
try {
|
||||
RJSValidateArgumentCount(argumentCount, 1);
|
||||
|
||||
if (RJSIsValueArray(ctx, arguments[0])) {
|
||||
JSObjectRef array = RJSValidatedValueToObject(ctx, arguments[0]);
|
||||
size_t length = RJSValidatedArrayLength(ctx, array);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
JSValueRef object = RJSValidatedObjectAtIndex(ctx, array, (unsigned int)i);
|
||||
RealmDelete(ctx, function, thisObject, 1, &object, jsException);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!JSValueIsObjectOfClass(ctx, arguments[0], RJSObjectClass())) {
|
||||
throw std::runtime_error("Argument to 'delete' must be a Realm object.");
|
||||
}
|
||||
|
||||
Object *object = RJSGetInternal<Object *>(RJSValidatedValueToObject(ctx, arguments[0]));
|
||||
|
||||
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
|
||||
|
||||
if (!realm->is_in_transaction()) {
|
||||
throw std::runtime_error("Can only delete objects within a transaction.");
|
||||
}
|
||||
|
||||
realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object->object_schema.name);
|
||||
table->remove(object->row.get_index());
|
||||
|
||||
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);
|
||||
|
||||
JSObjectRef object = JSValueToObject(ctx, arguments[0], jsException);
|
||||
if (object) {
|
||||
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
|
||||
realm->begin_transaction();
|
||||
JSObjectCallAsFunction(ctx, object, thisObject, 0, NULL, jsException);
|
||||
if (*jsException) {
|
||||
realm->cancel_transaction();
|
||||
}
|
||||
else {
|
||||
realm->commit_transaction();
|
||||
}
|
||||
JSObjectRef object = RJSValidatedValueToObject(ctx, arguments[0]);
|
||||
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
|
||||
realm->begin_transaction();
|
||||
JSObjectCallAsFunction(ctx, object, thisObject, 0, NULL, jsException);
|
||||
if (*jsException) {
|
||||
realm->cancel_transaction();
|
||||
}
|
||||
else {
|
||||
realm->commit_transaction();
|
||||
}
|
||||
}
|
||||
catch (std::exception &exp) {
|
||||
@ -269,11 +301,7 @@ JSValueRef RealmAddNotification(JSContextRef ctx, JSObjectRef function, JSObject
|
||||
try {
|
||||
RJSValidateArgumentCount(argumentCount, 1);
|
||||
|
||||
JSObjectRef user_function = JSValueToObject(ctx, arguments[0], jsException);
|
||||
if (!user_function) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JSObjectRef user_function = RJSValidatedValueToObject(ctx, arguments[0]);
|
||||
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
|
||||
JSGlobalContextRef gCtx = JSGlobalContextRetain(JSContextGetGlobalContext(ctx));
|
||||
Realm::NotificationFunction func = std::make_shared<std::function<void(const std::string)>>([=](std::string notification_name) {
|
||||
@ -309,6 +337,7 @@ JSClassRef RJSRealmClass() {
|
||||
const JSStaticFunction realmFuncs[] = {
|
||||
{"objects", RealmObjects},
|
||||
{"create", RealmCreateObject},
|
||||
{"delete", RealmDelete},
|
||||
{"write", RealmWrite},
|
||||
{"addNotification", RealmAddNotification},
|
||||
{NULL, NULL},
|
||||
|
@ -172,3 +172,4 @@ static inline bool RJSIsValueObjectOfType(JSContextRef ctx, JSValueRef value, JS
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool RJSIsValueArray(JSContextRef ctx, JSValueRef value);
|
||||
|
@ -79,3 +79,8 @@ JSValueRef RJSValueForString(JSContextRef ctx, const std::string &str) {
|
||||
JSStringRelease(jsStr);
|
||||
return value;
|
||||
}
|
||||
|
||||
bool RJSIsValueArray(JSContextRef ctx, JSValueRef value) {
|
||||
static JSStringRef arrayString = JSStringCreateWithUTF8CString("Array");
|
||||
return RJSIsValueObjectOfType(ctx, value, arrayString);
|
||||
}
|
@ -84,6 +84,35 @@ var RealmTests = {
|
||||
TestCase.assertEqual(objects[1].doubleCol, 2, 'wrong object property value');
|
||||
},
|
||||
|
||||
testRealmDelete: function() {
|
||||
var realm = new Realm({schema: [TestObjectSchema]});
|
||||
realm.write(function() {
|
||||
realm.create('TestObject', [1]);
|
||||
realm.create('TestObject', [2]);
|
||||
realm.create('TestObject', [3]);
|
||||
realm.create('TestObject', [4]);
|
||||
});
|
||||
|
||||
var objects = realm.objects('TestObject');
|
||||
TestCase.assertEqual(objects.length, 4, 'wrong object count');
|
||||
TestCase.assertThrows(function() {
|
||||
realm.delete(objects[0]);
|
||||
}, "can only delete in a write transaction");
|
||||
|
||||
realm.write(function() {
|
||||
TestCase.assertThrows(function() {
|
||||
realm.delete();
|
||||
});
|
||||
|
||||
realm.delete(objects[0]);
|
||||
TestCase.assertEqual(objects.length, 3, 'wrong object count');
|
||||
TestCase.assertEqual(objects[0].doubleCol, 2);
|
||||
|
||||
realm.delete([objects[0], objects[1]]);
|
||||
TestCase.assertEqual(objects.length, 1, 'wrong object count');
|
||||
});
|
||||
},
|
||||
|
||||
testRealmObjects: function() {
|
||||
var realm = new Realm({schema: [TestObjectSchema]});
|
||||
realm.write(function() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user