Skip to content

Commit

Permalink
Update custom user data examples to not use BSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc committed Jan 8, 2024
1 parent faaef3f commit f25afaa
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 58 deletions.
7 changes: 6 additions & 1 deletion examples/cpp/sync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ FetchContent_Declare(
GIT_REPOSITORY https://github.com/realm/realm-cpp.git
GIT_TAG 8eba9728ea535a6cd78beaef37ed6d22b73fe889
)
FetchContent_Declare(
json
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
)

FetchContent_MakeAvailable(Catch2 cpprealm)
FetchContent_MakeAvailable(Catch2 cpprealm json)

add_executable(examples-sync
app.cpp
Expand All @@ -32,3 +36,4 @@ add_executable(examples-sync

target_link_libraries(examples-sync PRIVATE Catch2::Catch2WithMain)
target_link_libraries(examples-sync PRIVATE cpprealm)
target_link_libraries(examples-sync PRIVATE nlohmann_json::nlohmann_json)
103 changes: 55 additions & 48 deletions examples/cpp/sync/custom-user-data.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
#include <catch2/catch_test_macros.hpp>
#include <cpprealm/sdk.hpp>
#include <future>
#include <nlohmann/json.hpp>

static const std::string APP_ID = "cpp-tester-uliix";

// This test is currently commented out because the SDK has removed the
// exposed Core headers that gave it access to a BSON library.
// See PR https://github.com/realm/realm-cpp/pull/123/
// Per Lee, a separate project will create a C++ SDK BSON library, but in
// the meantime, I'll need to use some other library to make this test work.
// I need to figure out how to create BSON strings in C++ and pass them
// instead of using realm::bson::Bson for the params.
// TODO: Figure out what library to use and how to make this test/example work.
#if 0
TEST_CASE("custom user data", "[realm][sync]")
{
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

// :snippet-start: create
auto user = app.login(realm::App::credentials::anonymous()).get();

// Functions take an argument of BsonArray, so initialize the custom data as a BsonDocument
auto customDataBson = realm::bson::BsonDocument({{"userId", user.identifier()}, {"favoriteColor", "gold"}});

// Call an Atlas Function to insert custom data for the user
auto result = user.call_function("updateCustomUserData", { customDataBson }).get();
// :snippet-end:
CHECK(result);

// :snippet-start: read
// Custom user data could be stale, so refresh it before reading it
user.refresh_custom_user_data().get();
CHECK((*user.custom_data())["favoriteColor"] == "gold");
// :snippet-end:

// :snippet-start: update
// Functions take an argument of BsonArray, so initialize the custom data as a BsonDocument
auto updatedDataBson = realm::bson::BsonDocument({{"userId", user.identifier()}, { "favoriteColor", "black" }});

// Call an Atlas Function to update custom data for the user
auto updateResult = user.call_function("updateCustomUserData", { updatedDataBson }).get();

// Refresh the custom user data before reading it to verify it succeeded
user.refresh_custom_user_data().get();
CHECK((*user.custom_data())["favoriteColor"] == "black");
// :snippet-end:
// :snippet-start: delete
auto deleteResult = user.call_function("deleteCustomUserData", {}).get();
// :snippet-end:
CHECK(deleteResult);
TEST_CASE("custom user data", "[realm][sync]") {
auto appConfig = realm::App::configuration();
appConfig.app_id = APP_ID;
auto app = realm::App(appConfig);

// :snippet-start: create
auto user = app.login(realm::App::credentials::anonymous()).get();

// Functions take a string argument. Any quotes within the array must be
// escaped.
auto customData =
"[{\"userId\":\"" + user.identifier() + "\",\"favoriteColor\":\"gold\"}]";

// Call an Atlas Function to insert custom data for the user
auto result = user.call_function("updateCustomUserData", customData).get();
// :snippet-end:
CHECK(result);

// :snippet-start: read
// Custom user data could be stale, so refresh it before reading it
user.refresh_custom_user_data().get();
auto userData = user.custom_data().value();

/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto userDataObject = nlohmann::json::parse(userData);
CHECK(userDataObject["favoriteColor"] == "gold");
// :snippet-end:

// :snippet-start: update
// Functions take a string argument. Any quotes within the array must be
// escaped.
auto updatedData = "[{\"userId\":\"" + user.identifier() +
"\",\"favoriteColor\":\"black\"}]";

// Call an Atlas Function to update custom data for the user
auto updateResult =
user.call_function("updateCustomUserData", updatedData).get();

// Refresh the custom user data before reading it to verify it succeeded
user.refresh_custom_user_data().get();
auto updatedUserData = user.custom_data().value();

/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto updatedUserDataObject = nlohmann::json::parse(updatedUserData);
CHECK(updatedUserDataObject["favoriteColor"] == "black");
// :snippet-end:
// :snippet-start: delete
auto deleteResult = user.call_function("deleteCustomUserData", "[]").get();
// :snippet-end:
CHECK(deleteResult);
}
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
auto user = app.login(realm::App::credentials::anonymous()).get();

// Functions take an argument of BsonArray, so initialize the custom data as a BsonDocument
auto customDataBson = realm::bson::BsonDocument({{"userId", user.identifier()}, {"favoriteColor", "gold"}});
// Functions take a string argument. Any quotes within the array must be
// escaped.
auto customData =
"[{\"userId\":\"" + user.identifier() + "\",\"favoriteColor\":\"gold\"}]";

// Call an Atlas Function to insert custom data for the user
auto result = user.call_function("updateCustomUserData", { customDataBson }).get();
auto result = user.call_function("updateCustomUserData", customData).get();
Original file line number Diff line number Diff line change
@@ -1 +1 @@
auto deleteResult = user.call_function("deleteCustomUserData", {}).get();
auto deleteResult = user.call_function("deleteCustomUserData", "[]").get();
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Custom user data could be stale, so refresh it before reading it
user.refresh_custom_user_data().get();
CHECK((*user.custom_data())["favoriteColor"] == "gold");
auto userData = user.custom_data().value();

/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto userDataObject = nlohmann::json::parse(userData);
CHECK(userDataObject["favoriteColor"] == "gold");
17 changes: 13 additions & 4 deletions source/examples/generated/cpp/custom-user-data.snippet.update.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
// Functions take an argument of BsonArray, so initialize the custom data as a BsonDocument
auto updatedDataBson = realm::bson::BsonDocument({{"userId", user.identifier()}, { "favoriteColor", "black" }});
// Functions take a string argument. Any quotes within the array must be
// escaped.
auto updatedData = "[{\"userId\":\"" + user.identifier() +
"\",\"favoriteColor\":\"black\"}]";

// Call an Atlas Function to update custom data for the user
auto updateResult = user.call_function("updateCustomUserData", { updatedDataBson }).get();
auto updateResult =
user.call_function("updateCustomUserData", updatedData).get();

// Refresh the custom user data before reading it to verify it succeeded
user.refresh_custom_user_data().get();
CHECK((*user.custom_data())["favoriteColor"] == "black");
auto updatedUserData = user.custom_data().value();

/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto updatedUserDataObject = nlohmann::json::parse(updatedUserData);
CHECK(updatedUserDataObject["favoriteColor"] == "black");

0 comments on commit f25afaa

Please sign in to comment.