Aaron Benton is an experienced architect who specializes in creative solutions to develop innovative mobile applications. He has over 10 years experience in full stack development, including ColdFusion, SQL, NoSQL, JavaScript, HTML, and CSS. Aaron is currently an Applications Architect for Shop.com in Greensboro, North Carolina and is a Couchbase Community Champion.

FakeIt Series 4 of 5: Working with Existing Data
So far in our FakeIt series we’ve seen how we can Generate Fake Data, Share Data and Dependencies, and use Definitions for smaller models. Today we are going to look at the last major feature of FakeIt, which is working with existing data through inputs.
Rarely as developers do we get the advantage of working on greenfield applications, our domains are more often than not a comprised of different legacy databases and applications. As we are modeling and building new applications, we need to reference and use this existing data. FakeIt allows you to provide existing data to your models through JSON, CSV or CSON files. This data is exposed as an inputs variable in each of a models *run and *build functions.
Users Model
We will start with our users.yaml model that we updated to in our most recent post to use Address 그리고 Phone definitions.
|
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
name: Users type: object 열쇠: _id 데이터: min: 1000 max: 2000 properties: _id: type: string description: 그 document 아이디 built by the prefix “user_” 그리고 the users 아이디 데이터: post_build: “`user_${this.user_id}`” 문서 유형: type: string description: 그 document type 데이터: 가치: “user” user_id: type: integer description: An auto–incrementing number 데이터: build: document_index first_name: type: string description: 그 users first name 데이터: build: faker.name.firstName() last_name: type: string description: 그 users last name 데이터: build: faker.name.lastName() 사용자 이름: type: string description: 그 사용자 이름 데이터: build: faker.internet.userName() 비밀번호: type: string description: 그 users 비밀번호 데이터: build: faker.internet.비밀번호() email_address: type: string description: 그 users email address 데이터: build: faker.internet.email() created_on: type: integer description: An epoch time of when the user was created 데이터: build: 새로운 날짜(faker.날짜.past()).시간 가져오기() addresses: type: object description: An object containing the home 그리고 work addresses ~를 위해 the user properties: home: description: 그 users home address schema: $ref: ‘#/definitions/Address’ work: description: 그 users work address schema: $ref: ‘#/definitions/Address’ main_phone: description: 그 users main phone number schema: $ref: ‘#/definitions/Phone’ 데이터: post_build: | delete this.main_phone.type 반환 this.main_phone additional_phones: type: array description: 그 users additional phone numbers items: $ref: ‘#/definitions/Phone’ 데이터: min: 1 max: 4 definitions: Phone: type: object properties: type: type: string description: 그 phone type 데이터: build: faker.random.arrayElement([ ‘Home’, ‘Work’, ‘Mobile’, ‘Other’ ]) phone_number: type: string description: 그 phone number 데이터: build: faker.phone.phoneNumber().교체하다(/[^0–9]+/g, ”) 확장 프로그램: type: string description: 그 phone 확장 프로그램 데이터: build: chance.bool({ likelihood: 30 }) ? chance.integer({ min: 1000, max: 9999 }) : null Address: type: object properties: address_1: type: string description: 그 address 1 데이터: build: `${faker.address.streetAddress()} ${faker.address.streetSuffix()}` address_2: type: string description: 그 address 2 데이터: build: chance.bool({ likelihood: 35 }) ? faker.address.secondaryAddress() : null locality: type: string description: 그 city / locality 데이터: build: faker.address.city() 지역: type: string description: 그 지역 / 상태 / province 데이터: build: faker.address.stateAbbr() postal_code: type: string description: 그 zip code / postal code 데이터: build: faker.address.zipCode() country: type: string description: 그 country code 데이터: build: faker.address.countryCode() |
Currently, our Address definition is generating a random country. What if our ecommerce site only supports a small subset of the 195 countries? Let’s say we support six countries to start with: US, CA, MX, UK, ES, DE. We could update the definitions country property to grab a random array element:
(For brevity the other properties have been left off of the model definition)
|
1 2 3 4 5 6 |
... country: type: string description: 그 country code 데이터: build: faker.random.arrayElement([‘US’, ‘CA’, ‘MX’, ‘UK’, ‘ES’, ‘DE’]); |
While this would work, what if we have other models that rely on this same country info, we would have to duplicate this logic. We can achieve this same thing by creating a countries.json file, and adding an inputs property to the data property that can be an absolute or relative path to our input. When are model is generated, our countries.json file will be exposed to each of the models build functions via the inputs argument as inputs.countries
(For brevity the other properties have been left off of the model definition)
|
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 |
name: Users type: object 열쇠: _id 데이터: min: 1000 max: 2000 inputs: ./countries.json properties: ... definitions: ... country: type: string description: 그 country code 데이터: build: faker.random.arrayElement(inputs.countries); countries.json [ “US”, “CA”, “MX”, “UK”, “ES”, “DE” ] |
By changing one existing line and adding another line in model we have provided existing data to our Users model. We can still generate a random country, based on the countries our application supports. Lets test our changes by using the following command:
|
1 |
fakeit 콘솔 —count 1 models/users.yaml |

Products Model
Our ecommerce application is using a separate system for categorization, we need to expose that data to our randomly generated products so that we are using valid category information. We will start with the products.yaml that we defined in the FakeIt Series 2 of 5: Shared Data and Dependencies post.
|
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
products.yaml name: 제품 type: object 열쇠: _id 데이터: min: 4000 max: 5000 properties: _id: type: string description: 그 document 아이디 데이터: post_build: `product_${this.product_id}` 문서 유형: type: string description: 그 document type 데이터: 가치: product product_id: type: string description: Unique identifier representing a specific product 데이터: build: faker.random.uuid() 가격: type: double description: 그 product 가격 데이터: build: chance.floating({ min: 0, max: 150, fixed: 2 }) sale_price: type: double description: 그 product 가격 데이터: post_build: | 하다 sale_price = 0; 만약 (chance.bool({ likelihood: 30 })) { sale_price = chance.floating({ min: 0, max: this.price * chance.floating({ min: 0, max: 0.99, fixed: 2 }), fixed: 2 }); } 반환 sale_price; display_name: type: string description: Display name of product. 데이터: build: faker.commerce.productName() short_description: type: string description: Description of product. 데이터: build: faker.lorem.paragraphs(1) long_description: type: string description: Description of product. 데이터: build: faker.lorem.paragraphs(5) keywords: type: array description: An array of keywords items: type: string 데이터: min: 0 max: 10 build: faker.random.word() availability: type: string description: 그 availability status of the product 데이터: build: | 하다 availability = ‘In-Stock’; 만약 (chance.bool({ likelihood: 40 })) { availability = faker.random.arrayElement([ ‘Preorder’, ‘Out of Stock’, ‘Discontinued’ ]); } 반환 availability; availability_date: type: integer description: An epoch time of when the product is available 데이터: build: faker.날짜.recent() post_build: 새로운 날짜(this.availability_date).시간 가져오기() product_slug: type: string description: 그 URL friendly version of the product name 데이터: post_build: faker.helpers.slugify(this.display_name).toLowerCase() category: type: string description: Category ~를 위해 the Product 데이터: build: faker.commerce.department() category_slug: type: string description: 그 URL friendly version of the category name 데이터: post_build: faker.helpers.slugify(this.category).toLowerCase() image: type: string description: Image URL representing the product. 데이터: build: faker.image.image() alternate_images: type: array description: An array of alternate images ~를 위해 the product items: type: string 데이터: min: 0 max: 4 build: faker.image.image() |
Our existing categories data has been provided in CSV format.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
categories.csv “category_id”,“category_name”,“category_slug” 23,“Electronics”,“electronics” 1032,“Office Supplies”,“office-supplies” 983,“Clothing & Apparel”,“clothing-and-apparel” 483,“Movies, Music & Books”,“movies-music-and-books” 3023,“Sports & Fitness”,“sports-and-fitness” 4935,“Automotive”,“automotive” 923,“Tools”,“tools” 5782,“Home Furniture”,“home-furniture” 9783,“Health & Beauty”,“health-and-beauty” 2537,“Toys”,“toys” 10,“Video Games”,“video-games” 736,“Pet Supplies”,“pet-supplies” |
Now we need to update our products.yaml model to use this existing data.
(For brevity the other properties have been left off of the model definition)
|
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 |
name: 제품 type: object 열쇠: _id 데이터: min: 4000 max: 5000 inputs: – ./categories.csv pre_build: globals.current_category = faker.random.arrayElement(inputs.categories); properties: ... category_id: type: integer description: 그 Category ID ~를 위해 the Product 데이터: build: globals.current_category.category_아이디 category: type: string description: Category ~를 위해 the Product 데이터: build: globals.current_category.category_name category_slug: type: string description: 그 URL friendly version of the category name 데이터: post_build: globals.current_category.category_slug ... |
There are a few things to notice about how we’ve updated our products.yaml model.
- inputs: is defined as an array not a string. While we are only using a single input, you can provide as many input files to your model as necessary.
- A pre_build function is defined at the root of the model. This is because we cannot grab a random array element for each of our three category properties as the values would not match. Each time an individual document is generated for our model, this pre_build function will run first.
- Each of our category properties build functions reference the global variable set by the pre_build function on our model.
We can test our changes by using the following command:
|
1 |
fakeit 콘솔 —count 1 models/products.yaml |

결론
Being able to work with existing data is an extremely powerful feature of FakeIt. It can be used to maintain the integrity of randomly generated documents to work with existing system, and can even be used to transform existing data and import it into Couchbase Server.
Up Next
Previous
- FakeIt Series 1 of 5: Generating Fake Data
- FakeIt Series 2 of 5: Shared Data and Dependencies
- FakeIt Series 3 of 5: Lean Models through Definitions

This post is part of the Couchbase Community Writing Program

댓글 남기기
댓글을 달기 위해서는 로그인해야합니다.