아론 벤튼은 혁신적인 모바일 애플리케이션 개발을 위한 창의적인 솔루션을 전문으로 하는 숙련된 아키텍트입니다. 그는 10년 이상 ColdFusion, SQL, NoSQL, JavaScript, HTML 및 CSS를 포함한 전체 스택 개발 분야에서 경력을 쌓았습니다. 현재 노스캐롤라이나주 그린즈버러에 위치한 Shop.com의 애플리케이션 아키텍트인 Aaron은 카우치베이스 커뮤니티 챔피언.

페이크잇 시리즈 3/5: 정의를 통해 본 린 모델
이전 게시물에서 페이크잇 시리즈 2/5: 공유 데이터 및 종속성에서는 FakeIt으로 다중 모델 종속성을 만드는 방법을 살펴봤습니다. 오늘은 정의를 활용하여 동일하지만 더 작은 모델을 만드는 방법을 살펴보겠습니다.
정의는 모델 내에서 재사용 가능한 집합을 만드는 방법입니다. 이를 통해 속성/값 집합을 한 번 정의하여 모델 전체에서 여러 번 참조할 수 있습니다. FakeIt 모델에서의 정의는 정의가 Swagger / 오픈 API 사양.
사용자 모델
에서 정의한 users.yaml 모델부터 시작하겠습니다. 첫 번째 게시물.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
name: Users type: object key: _id properties: _id: type: string description: The document id built by the prefix "user_" and the users id data: post_build: `user_${this.user_id}` doc_type: type: string description: The document type data: value: user user_id: type: integer description: An auto-incrementing number data: build: document_index first_name: type: string description: The users first name data: build: faker.name.firstName() last_name: type: string description: The users last name data: build: faker.name.lastName() username: type: string description: The username data: build: faker.internet.userName() password: type: string description: The users password data: build: faker.internet.password() email_address: type: string description: The users email address data: build: faker.internet.email() created_on: type: integer description: An epoch time of when the user was created data: build: new Date(faker.date.past()).getTime() |
각 사용자에 대해 집과 회사 주소를 지원해야 하는 새로운 요구 사항이 있다고 가정해 보겠습니다. 이 요구 사항에 따라 집과 직장의 중첩된 속성을 포함하는 주소라는 최상위 속성을 만들기로 결정했습니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ ... "addresses": { "home": { "address_1": "123 Broadway St", "address_2": "Apt. C", "locality": "Greensboro", "region": "NC", "postal_code": "27409", "country": "US" }, "work": { "address_1": "321 Morningside Ave", "address_2": "", "locality": "Greensboro", "region": "NC", "postal_code": "27409", "country": "US" } } } |
이러한 새 주소 속성을 지원하려면 users.yaml 모델을 업데이트해야 합니다.
(간결성을 위해 다른 프로퍼티는 모델 정의에서 제외했습니다.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
... properties: ... addresses: type: object description: An object containing the home and work addresses for the user properties: home: type: object description: The users home address properties: address_1: type: string description: The address 1 data: build: `${faker.address.streetAddress()} ${faker.address.streetSuffix()}` address_2: type: string description: The address 2 data: build: chance.bool({ likelihood: 35 }) ? faker.address.secondaryAddress() : null locality: type: string description: The city / locality data: build: faker.address.city() region: type: string description: The region / state / province data: build: faker.address.stateAbbr() postal_code: type: string description: The zip code / postal code data: build: faker.address.zipCode() country: type: string description: The country code data: build: faker.address.countryCode() work: type: object description: The users home address properties: address_1: type: string description: The address 1 data: build: `${faker.address.streetAddress()} ${faker.address.streetSuffix()}` address_2: type: string description: The address 2 data: build: chance.bool({ likelihood: 35 }) ? faker.address.secondaryAddress() : null locality: type: string description: The city / locality data: build: faker.address.city() region: type: string description: The region / state / province data: build: faker.address.stateAbbr() postal_code: type: string description: The zip code / postal code data: build: faker.address.zipCode() country: type: string description: The country code data: build: faker.address.countryCode() |
보시다시피 집 주소와 회사 주소 속성에 정확히 동일한 중첩 속성이 포함되어 있습니다. 이러한 중복으로 인해 모델이 더 크고 장황해집니다. 또한 주소 요구 사항을 변경해야 하는 경우 어떻게 될까요? 구조를 동일하게 유지하려면 두 번의 업데이트를 수행해야 합니다. 이때 주소를 생성하고 참조하는 단일 방법을 정의하여 정의를 활용할 수 있습니다.
(간결성을 위해 다른 프로퍼티는 모델 정의에서 제외했습니다.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
... properties: ... addresses: type: object description: An object containing the home and work addresses for the user properties: home: description: The users home address schema: $ref: '#/definitions/Address' work: description: The users work address schema: $ref: '#/definitions/Address' definitions: Address: type: object properties: address_1: type: string description: The address 1 data: build: `${faker.address.streetAddress()} ${faker.address.streetSuffix()}` address_2: type: string description: The address 2 data: build: chance.bool({ likelihood: 35 }) ? faker.address.secondaryAddress() : null locality: type: string description: The city / locality data: build: faker.address.city() region: type: string description: The region / state / province data: build: faker.address.stateAbbr() postal_code: type: string description: The zip code / postal code data: build: faker.address.zipCode() country: type: string description: The country code data: build: faker.address.countryCode() |
정의는 정의: 속성을 지정한 다음 정의의 이름을 지정하여 모델의 루트에 정의됩니다. 정의는 $ref를 사용하여 참조되며, 값은 정의의 경로가 됩니다(예: #/definitions/Address). 정의를 사용하여 모델에 약 30줄의 코드를 저장하고 사용자 모델 내에서 주소를 정의하고 생성하는 방법을 한 곳에서 만들었습니다.
다음 명령을 사용하여 업데이트된 사용자 모델 주소의 출력을 테스트할 수 있습니다:
|
1 |
fakeit console --count 1 models/users.yaml |

이제 사용자의 기본 전화번호를 저장한 다음 집, 직장, 휴대폰, 팩스 등의 선택적 추가 전화번호를 저장해야 하는 요구사항이 있다고 가정해 보겠습니다. 이 변경을 위해 두 개의 새로운 최상위 프로퍼티인 main_phone과 배열인 additional_phones를 사용하겠습니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ ... "main_phone": { "phone_number": "7852322322", "extension": null }, "additional_phones": [ { "phone_number": "3368232032", "extension": "3233", "type": "Work" }, { "phone_number": "4075922921", "extension": null, "type": "Mobile" } ] } |
이것이 가장 실용적인 데이터 모델이나 개인적으로 이 데이터를 어떻게 모델링했을지는 모르겠지만, 이 예시를 다시 사용하여 FakeIt 모델 내에서 정의를 사용하는 이점을 어떻게 활용할 수 있는지 설명할 수 있습니다.
(간결성을 위해 다른 프로퍼티는 모델 정의에서 제외했습니다.)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
... properties: ... main_phone: description: The users main phone number schema: $ref: '#/definitions/Phone' data: post_build: | delete this.main_phone.type return this.main_phone additional_phones: type: array description: The users additional phone numbers items: $ref: '#/definitions/Phone' data: min: 1 max: 4 definitions: Phone: type: object properties: type: type: string description: The phone type data: build: faker.random.arrayElement([ 'Home', 'Work', 'Mobile', 'Other' ]) phone_number: type: string description: The phone number data: build: faker.phone.phoneNumber().replace(/[^0-9]+/g, '') extension: type: string description: The phone extension data: build: chance.bool({ likelihood: 30 }) ? chance.integer({ min: 1000, max: 9999 }) : null |
이 예제에서는 유형, 전화 번호 및 내선 번호의 세 가지 속성을 포함하는 전화 정의를 만들었습니다. main_phone 프로퍼티에 type 속성을 제거하는 post_build 함수를 정의한 것을 볼 수 있습니다. 이는 정의를 빌드 함수와 함께 사용하여 정의에서 반환되는 내용을 조작하는 방법을 보여줍니다. 추가_전화 속성은 1~4개의 전화를 생성하는 전화 정의의 배열입니다. 다음 명령을 사용하여 업데이트된 사용자 모델 전화 번호의 출력을 테스트할 수 있습니다:
|
1 |
fakeit console --count 1 models/users.yaml |

결론
정의를 사용하면 더 작고 효율적인 모델을 위해 재사용 가능한 코드 세트를 생성하여 FakeIt 모델을 간소화할 수 있습니다.