diff --git a/ReactNative/RealmRPC.mm b/ReactNative/RealmRPC.mm index 5bf78442..a60e98a9 100644 --- a/ReactNative/RealmRPC.mm +++ b/ReactNative/RealmRPC.mm @@ -90,6 +90,47 @@ static RPCObjectID s_id_counter = 0; s_objects.erase(oid); return ""; }; + s_requests["/get_objects"] = [=](NSDictionary *dict) { + RPCObjectID newOid = s_id_counter++; + RPCObjectID realmId = [dict[@"realmId"] longValue]; + + JSValueRef arguments[2]; + long argumentCount = 1; + arguments[0] = RJSValueForString(s_context, [dict[@"type"] UTF8String]); + + NSString *query = dict[@"predicate"]; + if (query) { + arguments[1] = RJSValueForString(s_context, query.UTF8String); + } + + JSValueRef exception = NULL; + JSValueRef results = RealmObjects(s_context, NULL, s_objects[realmId], argumentCount, arguments, &exception); + JSValueProtect(s_context, results); + s_objects[newOid] = (JSObjectRef)results; + return "{\"resultsId\":" + std::to_string(realmId) + "}"; + }; + s_requests["/get_results_size"] = [=](NSDictionary *dict) { + RPCObjectID resultsId = [dict[@"resultsId"] longValue]; + + JSValueRef exception = NULL; + static JSStringRef lengthPropertyName = JSStringCreateWithUTF8CString("length"); + JSValueRef lengthValue = ResultsGetProperty(s_context, s_objects[resultsId], lengthPropertyName, &exception); + return "{\"result\":" + std::to_string(JSValueToNumber(s_context, lengthValue, &exception)) + "}"; + }; + s_requests["/get_results_item"] = [=](NSDictionary *dict) { + RPCObjectID resultsId = [dict[@"resultsId"] longValue]; + long index = [dict[@"index"] longValue]; + + JSValueRef exception = NULL; + JSStringRef indexPropertyName = JSStringCreateWithUTF8CString(std::to_string(index).c_str()); + JSValueRef objectValue = ResultsGetProperty(s_context, s_objects[resultsId], indexPropertyName, &exception); + JSStringRelease(indexPropertyName); + + RPCObjectID newOid = s_id_counter++; + JSValueProtect(s_context, objectValue); + s_objects[newOid] = (JSObjectRef)objectValue; + return "{\"result\":" + std::to_string(newOid) + "}"; + }; // Add a handler to respond to GET requests on any URL [webServer addDefaultHandlerForMethod:@"POST" diff --git a/src/RJSRealm.hpp b/src/RJSRealm.hpp index 65abd744..f5090ec8 100644 --- a/src/RJSRealm.hpp +++ b/src/RJSRealm.hpp @@ -26,4 +26,5 @@ std::string RJSDefaultPath(); void RJSSetDefaultPath(std::string path); JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException); -JSValueRef RealmCreateObject(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException); \ No newline at end of file +JSValueRef RealmCreateObject(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException); +JSValueRef RealmObjects(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException); \ No newline at end of file diff --git a/src/RJSResults.hpp b/src/RJSResults.hpp index 98e16308..5ffb0d24 100644 --- a/src/RJSResults.hpp +++ b/src/RJSResults.hpp @@ -27,4 +27,5 @@ JSClassRef RJSResultsClass(); JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className); JSObjectRef RJSResultsCreate(JSContextRef ctx, realm::SharedRealm realm, std::string className, std::string query); -JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException); +JSValueRef ResultsGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException); +