Docs Menu

Docs HomeRealm

C++ SDK (Alpha)

On this page

  • Limitations
  • Install
  • Usage
  • Define Your Data Model
  • Use Realm
  • Further Reading

Warning

Alpha Release

This SDK is currently offered as an alpha release. We encourage you to try out the feature and give feedback. However, be aware that APIs and functionality are subject to change.

The Realm C++ SDK enables client applications written in C++ to access data stored in realms and sync data with Atlas.

The C++ SDK does not yet support all Realm database features or Atlas App Services integrations.

For installation instructions and minimum requirements, refer to Install Realm - C++ SDK.

You can define your models as classes or structs that inherit from realm::object.

You must declare any property you want to store (persist) in a realm as realm::persisted. Your model must also expose a public static constant expression (constexpr) member called schema. Use the realm::schema and realm::property templates to describe your schema.

Realm ignores any members not declared as realm::persisted and not declared in the schema.

// Define your models like regular structs.
struct Dog : realm::object {
realm::persisted<std::string> name;
realm::persisted<int> age;
static constexpr auto schema = realm::schema("Dog",
realm::property<&Dog::name>("name"),
realm::property<&Dog::age>("age"));
};
struct Person : realm::object {
realm::persisted<std::string> _id;
realm::persisted<std::string> name;
realm::persisted<int> age;
// Create relationships by pointing an Object field to another Class
realm::persisted<std::optional<Dog>> dog;
static constexpr auto schema = realm::schema("Person",
realm::property<&Person::_id, true>("_id"), // primary key
realm::property<&Person::name>("name"),
realm::property<&Person::age>("age"),
realm::property<&Person::dog>("dog"));
};

You can use your data models as you would normal classes, except:

  • You may not modify any properties outside of a write transaction.

A realm is a collection of persisted objects. When opening a realm, you must specify which models are available by passing the models into the template parameter list of the realm::open() function.

To open a write transaction, pass a function to realm.write(). Make all modifications within that function.

// Use Realm objects like regular objects.
auto dog = Dog { .name = "Rex", .age = 1 };
std::cout << "dog: " << dog << "\n";
// Get the default Realm with compile time schema checking.
auto realm = realm::open<Person, Dog>();
// Persist your data in a write transaction
realm.write([&realm, &dog] {
realm.add(dog);
});
←  Introduction to RealmInstall Realm - C++ SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.