This is the API reference documentation for the Ditto C++ SDK.
#include "Ditto.h"
#include <chrono>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
constexpr const char *DITTO_APP_ID = "replace with your app ID";
constexpr const char *DITTO_AUTH_URL = "replace with your authorization URL";
constexpr const char *DITTO_ONLINE_PLAYGROUND_TOKEN =
"replace with your playground token";
constexpr const char *PERSISTENCE_DIR = "/tmp/ditto-cpp-testapp";
constexpr const char *LOG_EXPORT_PATH = "./ditto-logexport.jsonl.gz";
void export_ditto_log() noexcept {
try {
if (std::filesystem::exists(LOG_EXPORT_PATH)) {
std::filesystem::remove(LOG_EXPORT_PATH);
}
std::cerr << "info: exported log to " << LOG_EXPORT_PATH << std::endl;
} catch (const std::exception &err) {
std::cerr << "error: unable to export log dump: " << err.what()
<< std::endl;
}
}
int main(int argc, const char **argv) {
try {
std::cerr << "info: running Ditto test app with SDK version "
auto config = ditto::Ditto::Config::default_config()
.set_database_id(DITTO_APP_ID)
.set_server_connect(DITTO_AUTH_URL)
.set_persistence_directory(PERSISTENCE_DIR);
ditto->get_auth()->set_expiration_handler(
[login_token = DITTO_ONLINE_PLAYGROUND_TOKEN](
try {
[&](std::unique_ptr<std::string> client_info,
std::unique_ptr<ditto::DittoError> err) {
if (err != nullptr) {
ditto::Logger::error("expiration handler: login error:" +
string(err->what()));
}
});
} catch (const std::exception &e) {
ditto::Logger::error("expiration handler: " + string(e.what()));
}
});
ditto->get_sync().start();
std::mutex mtx;
auto observer =
ditto->get_store().register_observer(
"SELECT * FROM tasks WHERE isDeleted = false",
const auto items = query_result.
items();
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Update for collection 'tasks':\n";
for (const auto &item : items) {
std::cout << " " << item.json_string() << "\n";
}
std::cout << "End of update for collection 'tasks'\n";
});
auto subscription =
ditto->get_sync().register_subscription(
"SELECT * FROM tasks");
{
std::lock_guard<std::mutex> lock(mtx);
std::cout << "(Waiting for 15 seconds...)" << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(15));
subscription->cancel();
observer->cancel();
ditto->get_sync().stop();
export_ditto_log();
} catch (const std::exception &e) {
std::cerr << "error: " << e.what() << std::endl;
export_ditto_log();
std::exit(EXIT_FAILURE);
}
return 0;
}
static std::string get_development_provider()
Get the built-in development authentication provider to be used with development authentication token...
The entrypoint to the Ditto SDK.
Definition Ditto.hpp:38
static std::string get_version()
Returns a string containing the semantic version of the Ditto SDK.
static std::shared_ptr< Ditto > open(DittoConfig config=DittoConfig::default_config())
Create a new Ditto instance.
static std::future< uint64_t > export_to_file(std::string const &file_path)
Exports collected logs to a compressed and JSON-encoded file on the local file system.
static void set_minimum_log_level(LogLevel log_level)
Represents the returned results when executing a DQL query, containing a QueryResultItem for each mat...
Definition QueryResult.hpp:125
std::vector< QueryResultItem > items() const
Namespace for the Ditto C++ SDK types and functions.
Definition any.hpp:9