Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Tipos de datos

UUID - React Native SDK

Novedades en la versión realm@10.5.0.

UUID (Identificador único universal) es un byte 16 valor único. UUID se incluye con el paquete Realm como parte de BSON ().Realm.BSON.UUID

Puedes usar UUID como identificador único para objetos. UUID es indexable y puede usarse como llave primaria.

class Profile extends Realm.Object {
static schema = {
name: 'Profile',
primaryKey: '_id',
properties: {
_id: 'uuid',
name: 'string',
},
};
}
class Profile extends Realm.Object<Profile> {
_id!: Realm.BSON.UUID;
name!: string;
static schema: ObjectSchema = {
name: 'Profile',
primaryKey: '_id',
properties: {
_id: 'uuid',
name: 'string',
},
};
}

Para definir una propiedad como un UUID, configure su tipo como "uuid" en su modelo de objeto. Cree un objeto Realm dentro de una transacción de escritura. Para asignar cualquier propiedad de identificador único de su objeto a un valor aleatorio, llame a new UUID(). Como alternativa, utilice una string en new UUID() para establecer la propiedad de identificador único en un valor específico.

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:

  • Obtiene acceso a la instancia Realm abierta llamando al hook useRealm().

  • Creates a name state variable
    called "name" that represents the name of the profile.
  • Crea un método createProfile que realiza una transacción de escritura. Dentro
    en esa transacción de escritura, creamos un objeto Profile con el valor name de la variable de estado "nombre" y un valor _id de un nuevo objeto UUID.
  • Renderiza un componente TextInput que permite al usuario ingresar un nombre para
    the profile. When the user presses the "Create Profile" button, the createProfile method is called and creates a Profile object.
1const CreateProfileInput = () => {
2 const realm = useRealm();
3 const [name, setName] = useState('');
4
5 // createProfile creates a new 'Profile' Realm Object with a new UUID based on user input
6 const createProfile = () => {
7 realm.write(() => {
8 realm.create('Profile', {
9 name,
10 _id: new Realm.BSON.UUID(),
11 });
12 });
13 };
14 return (
15 <View>
16 <TextInput
17 placeholder='Name'
18 onChangeText={setName}
19 />
20 <Button
21 title='Create Profile'
22 onPress={createProfile}
23 />
24 </View>
25 );
1const CreateProfileInput = () => {
2 const realm = useRealm();
3 const [name, setName] = useState('');
4
5 // createProfile creates a new 'Profile' Realm Object with a new UUID based on user input
6 const createProfile = () => {
7 realm.write(() => {
8 realm.create('Profile', {
9 name,
10 _id: new Realm.BSON.UUID(),
11 });
12 });
13 };
14 return (
15 <View>
16 <TextInput
17 placeholder='Name'
18 onChangeText={setName}
19 />
20 <Button
21 title='Create Profile'
22 onPress={createProfile}
23 />
24 </View>
25 );

Volver

Mezclado

En esta página