UUID - React Native SDK
New in version realm@10.5.0
.
UUID
(Universal Unique Identifier) is a 16-byte unique value. UUID
is bundled with the Realm package as
part of BSON (Realm.BSON.UUID
).
You can use UUID
as an unique identifier for
objects. UUID
is indexable, and you can use it as a
primary key.
Usage
To define a property as a UUID
, set its type to "uuid"
in your
object model. Create a Realm object within
a write transaction. To set any unique identifier properties of your object to a
random value, call new UUID()
. Alternatively, pass a string to new
UUID()
to set the unique identifier property to a specific value.
Example
In the following CreateProfileInput
example, we create a Profile
Realm.Object with a uuid
type for the _id
field.
The CreateProfileInput
component does the following:
Gets access to the opened realm instance by calling the
useRealm()
hook.- Creates a name state variable
- called "name" that represents the name of the profile.
- Creates a
createProfile
method that performs a write transaction. Within - that write transaction, we create
a
Profile
object with thename
value of the "name" state variable and an_id
value of a newUUID
object.
- Creates a
- Renders a
TextInput
component that allows the user to enter a name for - the profile. When the user presses the "Create Profile" button, the
createProfile
method is called and creates aProfile
object.
- Renders a