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', }, }; }
Uso
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.
Ejemplo
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
createProfileque realiza una transacción de escritura. Dentro - en esa transacción de escritura, creamos un objeto
Profilecon el valornamede la variable de estado "nombre" y un valor_idde un nuevo objetoUUID.
- Crea un método
- Renderiza un componente
TextInputque permite al usuario ingresar un nombre para - the profile. When the user presses the "Create Profile" button, the
createProfilemethod is called and creates aProfileobject.
- Renderiza un componente
1 const 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 );
1 const 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 );