1:1 Relationship
10 분
일대일 관계 소개 많은 백엔드의 핵심에는 데이터를 저장할 수 있는 기능이 있습니다 parse를 사용하면 데이터 객체를 저장하고 그들 간의 관계를 설정할 수 있습니다 데이터 관계는 각 데이터 객체가 다른 객체와 어떻게 관련되거나 연관되는지를 표준화합니다 이는 복잡한 쿼리를 구축하고 실행할 때 추가적인 힘을 줄 수 있습니다 세 가지 주요 관계 유형이 있습니다 \<font color="#2166ae">일대다\</font> , 하나의 객체가 여러 다른 객체와 관련될 수 있는 경우; \<font color="#2166ae">일대일\</font> , 두 객체 간의 직접적인 관계를 설정하고 오직 그들만의 관계를 형성하는 경우; \<font color="#2166ae">다대다\</font> , 여러 객체 간에 많은 복잡한 관계를 생성할 수 있는 경우 이 가이드에서는 일대일 관계에 초점을 맞출 것입니다 이러한 관계는 고유한 필드가 필요하고 이를 강제해야 하는 민감한 데이터 및 사용자 관리와 관련된 애플리케이션에서 일반적입니다 예를 들어 id 번호 및 전화번호와 같은 필드가 있습니다 데이터 저장 백엔드는 일반적으로 이러한 연관성에 대한 명시적인 선언을 요구하며, parse는 이러한 연관성을 달성하기 위한 자동 솔루션을 제공하지 않습니다 그러나 서버에서 \<font color="#2166ae">parse cloud\</font> 함수를 사용하여 parse에서 1 1 관계를 구현할 수 있는 방법이 있습니다 이는 데이터 저장 전에 테이블 관계가 고유하게 유지되도록 강제합니다 이는 관련 클래스 모두에서 \<font color="#2166ae">beforesave\</font> 함수를 생성하고, 부모 클래스가 자식 클래스에 이미 하나의 인스턴스를 소유하고 있는 경우 저장을 방지하며, 그 반대의 경우도 마찬가지입니다 애플리케이션 parse 코드에서 이러한 경우를 처리할 수도 있으며, 저장하기 전에 서버를 쿼리하여 해당 관계를 보장할 수 있습니다 이 가이드에서 보여줄 방법이지만, 클라우드 함수를 사용하는 것이 훨씬 깔끔하고 권장됩니다 이 가이드에서는 세 가지 주요 데이터 연관성을 포함하는 react native 도서 등록 애플리케이션을 구현할 것입니다 back4app과 react native를 사용하여 일대일 데이터 관계를 생성하고 쿼리하는 방법을 배우게 됩니다 언제든지 github 리포지토리를 통해 이 프로젝트에 접근하여 스타일과 전체 코드를 확인할 수 있습니다 javascript 예제 리포지토리 https //github com/templates back4app/react native js associations typescript 예제 리포지토리 https //github com/templates back4app/react native ts associations 전제 조건 이 튜토리얼을 완료하려면 다음이 필요합니다 back4app에 연결된 react native 앱이 필요합니다 back4app https //www back4app com/docs/react native/parse sdk/react native sdk 이 가이드에서 제공하는 화면 레이아웃을 테스트/사용하려면 \<font color="#2166ae">react native paper\</font> 라이브러리 https //github com/callstack/react native paper 를 설정해야 합니다 목표 현실적인 시나리오에서 parse를 사용하여 react native에서 일대일 데이터베이스 관계를 수행하고 시연합니다 1 book 클래스 이해하기 이 가이드에서는 도서 등록 애플리케이션 예제를 사용할 것이므로, 먼저 이 데이터베이스에서 객체 관계가 어떻게 구성되어 있는지 이해해야 합니다 사용하게 될 주요 객체 클래스는 \<font color="#2166ae">book\</font> 클래스이며, 이 클래스는 등록된 각 도서 항목을 저장합니다 다음은 다른 네 개의 객체 클래스입니다 \<font color="#2166ae">publisher\</font> 도서 출판사 이름, \<font color="#2166ae">book\</font> , 일대다 관계 \<font color="#2166ae">genre\</font> 도서 장르, \<font color="#2166ae">book\</font> , 일대다 관계 이 예제에서는 도서가 하나의 장르만 가질 수 있다고 가정합니다; \<font color="#2166ae">author\</font> 도서 저자, \<font color="#2166ae">book\</font> , 다대다 관계; 도서는 여러 저자를 가질 수 있고 저자도 여러 도서를 가질 수 있습니다; \<font color="#2166ae">isdb\</font> 도서 isdb 식별 번호, \<font color="#2166ae">book\</font> , 일대일 관계; 이 번호는 각 도서에 대해 고유합니다 다음은 이러한 데이터베이스 테이블의 시각적 표현입니다 간단히 하기 위해 각 객체 클래스가 문자열 유형의 \<font color="#2166ae">이름\</font> 속성( \<font color="#2166ae">제목\</font> )을 가지고 있다고 가정하겠습니다 \<font color="#2166ae">책\</font> 과 같은 추가적인 관계 속성은 제외합니다 2 일대다 관계 생성 이 단계로 들어가기 전에 react native library 앱 예제를 클론하고 실행할 것을 권장합니다 ( javascript 예제 저장소 https //github com/templates back4app/react native js associations , typescript 예제 저장소 https //github com/templates back4app/react native ts associations ) 이 애플리케이션은 등록된 책을 나열하는 화면과 새로운 책을 생성하는 화면의 두 가지 주요 화면이 있습니다 책 등록 양식에는 다른 관련 객체에 대한 직접 링크와 책의 isbd 값에 할당된 \<font color="#2166ae">텍스트 입력\</font> 필드가 있어 일대일 관계를 생성하는 데 사용됩니다 이 양식을 제출할 때 호출되는 책 생성 방법을 살펴보겠습니다 javascript 1 const createbook = async function () { 2 try { 3 // these values come from state variables linked to 4 // the screen form fields, retrieving the user choices 5 // as a complete parse object, when applicable; 6 const booktitlevalue = booktitle; 7 const bookisbdvalue = bookisbd; 8 // for example, bookpublisher holds the value from 9 // radiobutton group field with its options being every 10 // publisher parse object instance saved on server, which is 11 // queried on screen load via useeffect 12 const bookpublisherobject = bookpublisher; 13 const bookgenreobject = bookgenre; 14 // bookauthors can be an array of parse objects, since the book 15 // may have more than one author 16 const bookauthorsobjects = bookauthors; 17	 18 // creates a new parse object instance 19 let book = new parse object('book'); 20	 21 // set data to parse object 22 // simple title field 23 book set('title', booktitlevalue); 24	 25 // 1 1 relation, need to check for uniqueness of value before creating a new isbd object 26 let isbdquery = new parse query('isbd'); 27 isbdquery equalto('name', bookisbdvalue); 28 let isbdqueryresult = await isbdquery first(); 29 if (isbdqueryresult !== null && isbdqueryresult !== undefined) { 30 // if first returns a valid object instance, it means that there 31 // is at least one instance of isbd with the informed value 32 alert alert( 33 'error!', 34 'there is already an isbd instance with this value!', 35 ); 36 return false; 37 } else { 38 // create a new isbd object instance to create a one to one relation on saving 39 let isbd = new parse object('isbd'); 40 isbd set('name', bookisbdvalue); 41 isbd = await isbd save(); 42 // set the new object to the new book object isbd field 43 book set('isbd', isbd); 44 } 45	 46 // one to many relations can be set in two ways 47 // add direct object to field (parse will convert to pointer on save) 48 book set('publisher', bookpublisherobject); 49 // or add pointer to field 50 book set('genre', bookgenreobject topointer()); 51	 52 // many to many relation 53 // create a new relation so data can be added 54 let authorsrelation = book relation('authors'); 55 // bookauthorsobjects is an array of parse objects, 56 // you can add to relation by adding the whole array or object by object 57 authorsrelation add(bookauthorsobjects); 58	 59 // after setting the values, save it on the server 60 try { 61 await book save(); 62 // success 63 alert alert('success!'); 64 navigation goback(); 65 return true; 66 } catch (error) { 67 // error can be caused by lack of internet connection 68 alert alert('error!', error message); 69 return false; 70 } 71 } catch (error) { 72 // error can be caused by lack of value selection 73 alert alert( 74 'error!', 75 'make sure to select valid choices in publisher, genre and author fields!', 76 ); 77 return false; 78 } 79 }; todolist tsx 1 const createbook = async function () promise\<boolean> { 2 try { 3 // these values come from state variables linked to 4 // the screen form fields, retrieving the user choices 5 // as a complete parse object, when applicable; 6 const booktitlevalue string = booktitle; 7 const bookisbdvalue string = bookisbd; 8 // for example, bookpublisher holds the value from 9 // radiobutton group field with its options being every 10 // publisher parse object instance saved on server, which is 11 // queried on screen load via useeffect 12 const bookpublisherobject parse object = bookpublisher; 13 const bookgenreobject parse object = bookgenre; 14 // bookauthors can be an array of parse objects, since the book 15 // may have more than one author 16 const bookauthorsobjects \[parse object] = bookauthors; 17	 18 // creates a new parse object instance 19 let book parse object = new parse object('book'); 20	 21 // set data to parse object 22 // simple title field 23 book set('title', booktitlevalue); 24	 25 // 1 1 relation, need to check for uniqueness of value before creating a new isbd object 26 let isbdquery parse query = new parse query('isbd'); 27 isbdquery equalto('name', bookisbdvalue); 28 let isbdqueryresult parse object = await isbdquery first(); 29 if (isbdqueryresult !== null && isbdqueryresult !== undefined) { 30 // if first returns a valid object instance, it means that there 31 // is at least one instance of isbd with the informed value 32 alert alert( 33 'error!', 34 'there is already an isbd instance with this value!', 35 ); 36 return false; 37 } else { 38 // create a new isbd object instance to create a one to one relation on saving 39 let isbd parse object = new parse object('isbd'); 40 isbd set('name', bookisbdvalue); 41 isbd = await isbd save(); 42 // set the new object to the new book object isbd field 43 book set('isbd', isbd); 44 } 45	 46 // one to many relations can be set in two ways 47 // add direct object to field (parse will convert to pointer on save) 48 book set('publisher', bookpublisherobject); 49 // or add pointer to field 50 book set('genre', bookgenreobject topointer()); 51	 52 // many to many relation 53 // create a new relation so data can be added 54 let authorsrelation = book relation('authors'); 55 // bookauthorsobjects is an array of parse objects, 56 // you can add to relation by adding the whole array or object by object 57 authorsrelation add(bookauthorsobjects); 58	 59 // after setting the values, save it on the server 60 try { 61 await book save(); 62 // success 63 alert alert('success!'); 64 navigation goback(); 65 return true; 66 } catch (error) { 67 // error can be caused by lack of internet connection 68 alert alert('error!', error message); 69 return false; 70 } 71 } catch (error) { 72 // error can be caused by lack of value selection 73 alert alert( 74 'error!', 75 'make sure to select valid choices in publisher, genre and author fields!', 76 ); 77 return false; 78 } 79 }; 새 책 \<font color="#2166ae">bookisbdvalue\</font> 가 설정된 방법을 보세요 \<font color="#2166ae">parse object\</font> 인스턴스를 생성하고 저장하는 것은 일대일 및 일대다 관계를 만드는 유사한 과정입니다 이 과정에서는 \<font color="#2166ae">parse object\</font> 인스턴스를 인수로 전달합니다 \<font color="#2166ae">parse object set\</font> 메서드를 사용하여 두 개의 인수를 받습니다 필드 이름과 설정할 값입니다 여기서 주의할 점은, 저장하기 전에 데이터베이스에 알려진 isbd id 문자열 값을 포함하는 \<font color="#2166ae">isbd\</font> 객체가 없고, 또한 이미 관련된 \<font color="#2166ae">book\</font> 객체가 없다는 것을 보장해야 한다는 것입니다 두 번째 부분은 항상 참이 될 것입니다 왜냐하면 매번 새로운 \<font color="#2166ae">book\</font> 객체를 생성하고 있기 때문입니다 \<font color="#2166ae">isbd\</font> 의 고유성을 보장하는 것은 다음과 같은 강조된 쿼리를 사용하여 달성할 수 있습니다 javascript 1 let isbdquery = new parse query('isbd'); 2 isbdquery equalto('name', bookisbdvalue); 3 let isbdqueryresult = await isbdquery first(); 4 if (isbdqueryresult !== null && isbdqueryresult !== undefined) { 5 // if first returns a valid object instance, it means that there 6 // is at least one instance of isbd with the informed value 7 alert alert( 8 'error!', 9 'there is already an isbd instance with this value!', 10 ); 11 return false; 12 } todolist tsx 1 let isbdquery parse query = new parse query('isbd'); 2 isbdquery equalto('name', bookisbdvalue); 3 let isbdqueryresult parse object = await isbdquery first(); 4 if (isbdqueryresult !== null && isbdqueryresult !== undefined) { 5 // if first returns a valid object instance, it means that there 6 // is at least one instance of isbd with the informed value 7 alert alert( 8 'error!', 9 'there is already an isbd instance with this value!', 10 ); 11 return false; 12 } 객체를 성공적으로 저장한 후, parse는 포인터 데이터 유형 열을 생성하고 대시보드에 직접 링크를 만들어 내부에서 빠르게 접근할 수 있도록 합니다 3 일대일 관계 쿼리하기 일대일 관련 객체를 쿼리하는 것은 parse에서 대부분 처리되기 때문에 매우 간단합니다 책 등록 목록 화면의 쿼리 기능을 살펴보세요 javascript 1 const querybooks = async function () { 2 // these values come from state variables linked to 3 // the screen query radiobutton group fields, with its options being every 4 // parse object instance saved on server from the referred class, which is 5 // queried on screen load via useeffect; these variables retrievie the user choices 6 // as a complete parse object; 7 const querypublishervalue = querypublisher; 8 const querygenrevalue = querygenre; 9 const queryauthorvalue = queryauthor; 10 const queryisbdvalue = queryisbd; 11	 12 // reading parse objects is done by using parse query 13 const parsequery = new parse query('book'); 14	 15 // one to many queries 16 if (querypublishervalue !== '') { 17 parsequery equalto('publisher', querypublishervalue); 18 } 19 if (querygenrevalue !== '') { 20 parsequery equalto('genre', querygenrevalue); 21 } 22	 23 // one to one query 24 if (queryisbdvalue !== '') { 25 parsequery equalto('isbd', queryisbdvalue); 26 } 27	 28 // many to many query 29 // in this case, we need to retrieve books related to the chosen author 30 if (queryauthorvalue !== '') { 31 parsequery equalto('authors', queryauthorvalue); 32 } 33	 34 try { 35 let books = await parsequery find(); 36 // many to many objects retrieval 37 // in this example we need to get every related author parse object 38 // and add it to our query result objects 39 for (let book of books) { 40 // this query is done by creating a relation and querying it 41 let bookauthorsrelation = book relation('authors'); 42 book authorsobjects = await bookauthorsrelation query() find(); 43 } 44 setqueriedbooks(books); 45 return true; 46 } catch (error) { 47 // error can be caused by lack of internet connection 48 alert alert('error!', error message); 49 return false; 50 } 51 }; todolist tsx 1 const querybooks = async function () promise\<boolean> { 2 // these values come from state variables linked to 3 // the screen query radiobutton group fields, with its options being every 4 // parse object instance saved on server from the referred class, which is 5 // queried on screen load via useeffect; these variables retrievie the user choices 6 // as a complete parse object; 7 const querypublishervalue parse object = querypublisher; 8 const querygenrevalue parse object = querygenre; 9 const queryauthorvalue parse object = queryauthor; 10 const queryisbdvalue parse object = queryisbd; 11	 12 // reading parse objects is done by using parse query 13 const parsequery parse query = new parse query('book'); 14	 15 // one to many queries 16 if (querypublishervalue !== '') { 17 parsequery equalto('publisher', querypublishervalue); 18 } 19 if (querygenrevalue !== '') { 20 parsequery equalto('genre', querygenrevalue); 21 } 22	 23 // one to one query 24 if (queryisbdvalue !== '') { 25 parsequery equalto('isbd', queryisbdvalue); 26 } 27	 28 // many to many query 29 // in this case, we need to retrieve books related to the chosen author 30 if (queryauthorvalue !== '') { 31 parsequery equalto('authors', queryauthorvalue); 32 } 33	 34 try { 35 let books \[parse object] = await parsequery find(); 36 // many to many objects retrieval 37 // in this example we need to get every related author parse object 38 // and add it to our query result objects 39 for (let book of books) { 40 // this query is done by creating a relation and querying it 41 let bookauthorsrelation = book relation('authors'); 42 book authorsobjects = await bookauthorsrelation query() find(); 43 } 44 setqueriedbooks(books); 45 return true; 46 } catch (error) { 47 // error can be caused by lack of internet connection 48 alert alert('error!', error message); 49 return false; 50 } 51 }; 이 경우, 특정 isbd와 관련된 책을 쿼리하려면 \<font color="#2166ae">parse query equalto\</font> 메서드를 호출하고 \<font color="#2166ae">parse object\</font> 인스턴스를 매개변수로 전달하면 됩니다 쿼리 후, parse는 결과 객체 내부에 모든 일대일 관계 필드의 완전한 인스턴스를 저장합니다 이러한 객체 인스턴스에서 데이터를 검색하고 표시하려면 다음과 같이 \<font color="#2166ae">parse object get\</font> 메서드를 체이닝할 수 있습니다 \<font color="#2166ae">bookparseobject get(('isbd') get('name')\</font> 다음은 이러한 getter를 사용하여 목록 항목에서 isbd 이름을 검색하는 목록 화면의 모습입니다 결론 이 가이드의 끝에서, react native에서 parse의 일대일 관계를 생성하고 쿼리하는 방법을 배웠습니다 다음 가이드에서는 사용자를 등록하는 방법을 보여드리겠습니다