Atlas Device SDK applications model data as objects composed of field-value pairs that each contain one or more supported data types.
Object Types and Schemas
Cada objeto de base de datos tiene un tipo de objeto que hace referencia a la clase del objeto. Los objetos del mismo tipo comparten un esquema de objeto que define las propiedades y relaciones de esos objetos.
Database Schema
Un esquema de base de datos es una lista de posibles esquemas de objetos que la base de datos puede contener. Cada objeto de base de datos debe ajustarse a un tipo de objeto Realm que esté incluido en el esquema de su base de datos.
Al abrir una base de datos, debes especificar qué modelos están disponibles pasando los modelos a la plantilla que utilizas para abrir la base de datos. Esos modelos deben tener esquemas y esta lista de esquemas se convierte en el esquema de la base de datos.
If the database already contains data when you open it, the SDK validates each object to ensure that an object schema was provided for its type and that it meets all of the constraints specified in the schema.
For more information about how to open the database, refer to Configure & Open a Realm - C++ SDK
Object Model
Your object model is the core structure that gives the database information about how to interpret and store the objects in your app. The C++ SDK object model is a regular C++ class or a struct that contains a collection of properties. The properties that you want to persist must use supported data types. Properties are also the mechanism for establishing relationships between object types.
When you define your C++ class or struct, you must also provide an object schema. The schema is a C++ macro that gives the SDK information about which properties to persist, and what type of database object it is.
Debe definir su modelo de objeto SDK dentro del realm namespace.
Object Schema
A C++ SDK object schema maps properties for a specific object type. The SDK schemas are macros that give the SDK the information it needs to store and retrieve the objects. A schema must accompany every object model you want to persist, and it may be one of:
REALM_SCHEMAREALM_EMBEDDED_SCHEMAREALM_ASYMMETRIC_SCHEMA
You must define the schema and your object model within the realm namespace.
Define a New Object Type
En el SDK de C++, puede definir sus modelos como estructuras o clases de C++. Proporcione un esquema de objeto con el nombre del tipo de objeto y los nombres de las propiedades que desea conservar en la base de datos. Al agregar el objeto a la base de datos, el SDK ignora las propiedades que omita del esquema.
You must declare your object and the schema within the realm namespace. You must then use the realm namespace when you initialize and perform CRUD operations with the object.
namespace realm { struct Dog { std::string name; int64_t age; }; REALM_SCHEMA(Dog, name, age) struct Person { realm::primary_key<int64_t> _id; std::string name; int64_t age; // Create relationships by pointing an Object field to another struct or class Dog *dog; }; REALM_SCHEMA(Person, _id, name, age, dog) } // namespace realm
Nota
Class names are limited to a maximum of 57 UTF-8 characters.
Specify a Primary Key
You can designate a property as the primary key of your object.
Primary keys allow you to efficiently find, update, and upsert objects.
Primary keys are subject to the following limitations:
You can define only one primary key per object model.
Primary key values must be unique across all instances of an object in the database. The C++ SDK throws an error if you try to insert a duplicate primary key value.
Los valores de la llave primaria son inmutables. Para cambiar el valor de la clave primaria de un objeto, se debe borrar el objeto original e insertar uno nuevo con un valor de clave primaria diferente.
Embedded objects cannot define a primary key.
If you are using Device Sync, your models must have a primary key named _id.
The C++ SDK supports primary keys of the following types, and their optional variants:
int64_trealm::object_idrealm::uuidstd::string
Additionally, a required realm::enum property can be a primary key, but realm::enum cannot be optional if it is used as a primary key.
Set a property as a primary key with the primary_key template:
struct Person { realm::primary_key<int64_t> _id; std::string name; int64_t age; // Create relationships by pointing an Object field to another struct or class Dog *dog; }; REALM_SCHEMA(Person, _id, name, age, dog)
Ignorar una propiedad
Tu modelo puede incluir propiedades que la base de datos no almacena.
La base de datos ignora cualquier propiedad que no esté incluida en el esquema del objeto.
namespace realm { struct Employee { realm::primary_key<int64_t> _id; std::string firstName; std::string lastName; // You can use this property as you would any other member // Omitting it from the schema means the SDK ignores it std::string jobTitle_notPersisted; }; // The REALM_SCHEMA omits the `jobTitle_notPersisted` property // The SDK does not store and cannot retrieve a value for this property REALM_SCHEMA(Employee, _id, firstName, lastName) } // namespace realm
Define an Embedded Object
Un objeto incrustado es un tipo especial de objeto que modela datos complejos sobre un objeto concreto. Los objetos incrustados son similares a las relaciones, pero ofrecen restricciones adicionales y reflejan de manera más natural el modelo de documento denormalizado de MongoDB.
The C++ SDK enforces unique ownership constraints that treat each embedded object as nested data inside of a single, specific parent object. An embedded object inherits the lifecycle of its parent object and cannot exist as an independent database object. The SDK automatically deletes embedded objects if their parent object is deleted or when overwritten by a new embedded object instance.
Puedes declarar un objeto como un objeto incrustado que no tenga un ciclo de vida independiente del objeto en el que está incrustado. Esto difiere de una relación de uno a uno o de uno a muchos, en la que los objetos relacionados tienen ciclos de vida independientes.
Provide a REALM_EMBEDDED_SCHEMA with the struct or class name and the names of any properties that you want the database to persist.
Define a property as an embedded object on the parent object by setting a pointer to the embedded object's type.
namespace realm { struct ContactDetails { // Because ContactDetails is an embedded object, it cannot have its own _id // It does not have a lifecycle outside of the top-level object std::string emailAddress; std::string phoneNumber; }; REALM_EMBEDDED_SCHEMA(ContactDetails, emailAddress, phoneNumber) struct Business { realm::object_id _id; std::string name; ContactDetails *contactDetails; }; REALM_SCHEMA(Business, _id, name, contactDetails) } // namespace realm
Definir un objeto asimétrico
Puede utilizar la ingesta de datos para sincronizar un objeto de forma unidireccional desde su dispositivo a la base de datos vinculada a su aplicación Atlas App Services.
An asymmetric_object broadly has the same supported types as realm::object, with a few exceptions:
Asymmetric objects can link to the following types: -
object-embedded_object-std::vector<embedded_object>
Asymmetric objects do not function in the same way as other database objects. You cannot:
Remove an asymmetric object from the device database
Query an asymmetric object from the device database
Solo puede crear un objeto asimétrico, que luego se sincroniza de forma unidireccional con la base de datos de Atlas vinculada a su aplicación con Device Sync.
For more information, see: Create an Asymmetric Object.
In the C++ SDK, define an asymmetric object the same way you would a regular C++ struct or class. Provide a REALM_ASYMMETRIC_SCHEMA with the struct or class name and the names of any properties that you want the database to persist.
struct WeatherSensorReading { realm::primary_key<realm::object_id> _id{realm::object_id::generate()}; std::string deviceId; double temperatureInFahrenheit; int64_t windSpeedInMph; }; REALM_ASYMMETRIC_SCHEMA(WeatherSensorReading, _id, deviceId, temperatureInFahrenheit, windSpeedInMph)