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
/ /
데이터 유형

UUID - React Native SDK

버전 realm@10.5.0의 새로운 기능

UUID (범용 16 고유 식별자)는UUID 바이트 고유 값Realm.BSON.UUID 입니다. 은(는) BSON ()의 일부로 Realm 패키지 와 함께 번들로 제공됩니다.

객체의 고유 식별자로 UUID를 사용할 수 있습니다. UUID인덱싱이 가능하며 프라이머리 키로 사용할 수 있습니다.

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',
},
};
}

속성을 UUID으로 정의하려면 객체 모델에서 해당 유형을 "uuid" 로 설정합니다. 쓰기 트랜잭션(write transaction) 내에서 Realm 객체를 만듭니다. 객체의 고유 식별자 속성을 임의의 값으로 설정하려면 new UUID()를 호출합니다. 또는 문자열을 new UUID() 에 전달하여 고유 식별자 속성을 특정 값으로 설정합니다.

다음 CreateProfileInput 예제에서는 _id 필드에 대해 uuid 유형의 Profile Realm.Object 를 만듭니다.

CreateProfileInput 컴포넌트는 다음을 수행합니다.

  • useRealm() 훅을 호출하여 열린 영역 인스턴스에 대한 액세스 권한을 가져옵니다.

  • 이름 상태 변수를 생성합니다.
    프로필의 이름을 나타냅니다.
  • 쓰기 트랜잭션(write transaction)을 수행하는 createProfile 메서드를 만듭니다. 그리고
    이 쓰기 트랜잭션(write transaction)에서는 " name " 상태 변수의 name 값과 새 UUID 객체의 _id 값을 사용하여 Profile 객체를 만듭니다.
  • 사용자가 프로필 이름을 입력할 수 있는 TextInput 구성요소를
    렌더링합니다. 사용자가 " 프로필 생성" 버튼을 누르면 createProfile 메서드가 호출되고 Profile 객체가 생성됩니다.
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 );

돌아가기

혼합

이 페이지의 내용