-
Notifications
You must be signed in to change notification settings - Fork 2
/
kin-examples.html
283 lines (255 loc) · 59.9 KB
/
kin-examples.html
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!DOCTYPE html> <html> <head> <title>kin-examples.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> kin-examples.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="s1">'use strict'</span>
<span class="kd">var</span> <span class="nx">Kin</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'../../lib/kin'</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">Faker</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'Faker'</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">assert</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'assert'</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'underscore'</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'node-uuid'</span><span class="p">).</span><span class="nx">v1</span>
<span class="kd">var</span> <span class="nx">kin</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Kin</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h2>Fixed Values</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Create a User blueprint with fixed values for properties</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">username</span><span class="o">:</span> <span class="s1">'joe'</span><span class="p">,</span>
<span class="nx">email</span><span class="o">:</span> <span class="s1">'joe@example.com'</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Generate a User object. The returned user will have the properties
supplied in the template.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'joe'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'joe@example.com'</span><span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>As expected, if we generate another User, we'll get the same values every time.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">anotherUser</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">anotherUser</span><span class="p">.</span><span class="nx">username</span><span class="p">,</span> <span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">anotherUser</span><span class="p">.</span><span class="nx">email</span><span class="p">,</span> <span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <h2>Overriding Values at Generation Time</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>You can optionally pass a second parameter to <code>generate</code>. These values
will override any values provided in the blueprint.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'bill'</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">,</span> <span class="s1">'bill'</span><span class="p">)</span> <span class="c1">// uses the overridden value</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">,</span> <span class="s1">'joe@example.com'</span><span class="p">)</span> <span class="c1">// uses the blueprint value</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Overriding properties can also contain keys that aren't specified in
the original blueprint.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="p">{</span><span class="nx">manager</span><span class="o">:</span> <span class="s1">'alice'</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">,</span> <span class="s1">'joe'</span><span class="p">)</span> <span class="c1">// uses the blueprint value</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">,</span> <span class="s1">'joe@example.com'</span><span class="p">)</span> <span class="c1">// uses the blueprint value</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">manager</span><span class="p">,</span> <span class="s1">'alice'</span><span class="p">)</span> <span class="c1">// non-blueprint key</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <h2>Instantiating Custom Models</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Kin can optionally generate custom models for you, so long as your model's
constructor takes a set of properties.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>For instance, you might use this as your model's constructor:</p> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/* User Constructor */</span>
<span class="kd">var</span> <span class="nx">UserB</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">properties</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">properties</span> <span class="o">=</span> <span class="nx">properties</span> <span class="o">||</span> <span class="p">{}</span>
<span class="k">this</span><span class="p">.</span><span class="nx">username</span> <span class="o">=</span> <span class="nx">properties</span><span class="p">.</span><span class="nx">username</span>
<span class="k">this</span><span class="p">.</span><span class="nx">email</span> <span class="o">=</span> <span class="nx">properties</span><span class="p">.</span><span class="nx">email</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">username</span> <span class="o">+</span> <span class="s1">'@example.com'</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Specify the constructor you'd like to use with the special <code>_model</code> property.
<em>Note:</em> you won't find the _model property on the generated object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserB'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="nx">UserB</span><span class="p">,</span>
<span class="nx">username</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">userName</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Kin will use the supplied constructor when generating this model,
passing-in any generated values as the first (and only) argument.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserB'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">username</span> <span class="o">&&</span> <span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">,</span> <span class="nx">user</span><span class="p">.</span><span class="nx">username</span> <span class="o">+</span> <span class="s1">'@example.com'</span><span class="p">)</span> <span class="c1">// Test against constructor behaviour to ensure it was used. </span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">user</span> <span class="k">instanceof</span> <span class="nx">UserB</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">_model</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">)</span> <span class="c1">// _model is not stored on the object</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <h2>Dynamic Values</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Generating fixed values isn't much fun. We can also
define asyncronous functions as dynamic generators for properties.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserC'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">username</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">userName</span><span class="p">())</span>
<span class="p">},</span>
<span class="nx">email</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">email</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserC'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">)</span> <span class="c1">// some random username as defined by Faker, eg "Rupert_Mertz"</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">)</span> <span class="c1">// some random email as defined by Faker, eg "Brook_Bednar@price.us"</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Generating another user should always run the matching generation
function, generating different data each time</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserC'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserC'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">anotherUser</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">notEqual</span><span class="p">(</span><span class="nx">anotherUser</span><span class="p">.</span><span class="nx">username</span><span class="p">,</span> <span class="nx">user</span><span class="p">.</span><span class="nx">username</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">notEqual</span><span class="p">(</span><span class="nx">anotherUser</span><span class="p">.</span><span class="nx">email</span><span class="p">,</span> <span class="nx">user</span><span class="p">.</span><span class="nx">email</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p><em>Remember</em> When using a generation function, you must always use a callback to
return the value, in the standard <code>err, value</code> format:
<code>callback(err, value1[, value2, value3... valueN])</code></p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <h2>Multiple Values</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>Sometimes we need to generate multiple values for a parent object.
For example, we might want to generate many tags for a <code>Document</code>
We can setup our <code>tags</code> property to create a single 'tag'.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Lorem</span><span class="p">.</span><span class="nx">sentence</span><span class="p">())</span> <span class="c1">// example content</span>
<span class="p">},</span>
<span class="nx">tags</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Lorem</span><span class="p">.</span><span class="nx">words</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">pop</span><span class="p">())</span> <span class="c1">// generate a single word</span>
<span class="p">}</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Then, you can specify the number you'd like to create when calling <code>generate</code>. If the blueprint
property is a function, and the overridden property is a Number it will call the property
generator function N times, and will return the results as an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">,</span> <span class="p">{</span><span class="nx">tags</span><span class="o">:</span> <span class="mi">5</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nb">document</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">tags</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="c1">// generates a document with 5 tags</span>
<span class="p">})</span>
<span class="cm">/* Example output for `kin.generate('Document', {tags: 5})` */</span>
<span class="kd">var</span> <span class="nx">GeneratedDocumentExample</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'perferendis reiciendis sequi qui eum labore'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span>
<span class="s1">'magnam'</span><span class="p">,</span>
<span class="s1">'inventore'</span><span class="p">,</span>
<span class="s1">'facere'</span><span class="p">,</span>
<span class="s1">'ut'</span><span class="p">,</span>
<span class="s1">'rerum'</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">,</span> <span class="p">{</span><span class="nx">tags</span><span class="o">:</span> <span class="mi">30</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nb">document</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">tags</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span> <span class="c1">// generates a document with 30 tags</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <h2>Nesting Models</h2>
<p>A common use case for generating multiple values is creating
multi-level model hierarchies.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserD'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">username</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">userName</span><span class="p">())</span>
<span class="p">},</span>
<span class="nx">email</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">email</span><span class="p">())</span>
<span class="p">},</span>
<span class="nx">documents</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">,</span> <span class="p">{</span><span class="nx">tags</span><span class="o">:</span> <span class="mi">3</span><span class="p">},</span> <span class="nx">callback</span><span class="p">)</span> <span class="c1">// Use Kin to generate a document</span>
<span class="p">}</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>We can easily generate a <code>User</code> with some number of <code>Document</code>s by
simply passing a number as the value of the documents property.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserD'</span><span class="p">,</span> <span class="p">{</span><span class="nx">documents</span><span class="o">:</span> <span class="mi">5</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">documents</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="p">})</span>
<span class="cm">/* Example output for kin.generate('User', {documents: 5}) */</span>
<span class="kd">var</span> <span class="nx">GeneratedUserWithDocumentsExample</span> <span class="o">=</span>
<span class="p">{</span>
<span class="nx">username</span><span class="o">:</span> <span class="s1">'Lydia'</span><span class="p">,</span>
<span class="nx">email</span><span class="o">:</span> <span class="s1">'Jaron@angie.biz'</span><span class="p">,</span>
<span class="nx">documents</span><span class="o">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'velit at earum aut molestiae odio'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s1">'voluptatem'</span><span class="p">,</span> <span class="s1">'sunt'</span><span class="p">,</span> <span class="s1">'modi'</span> <span class="p">]</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'eum facilis corrupti possimus qui quia'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s1">'illo'</span><span class="p">,</span> <span class="s1">'qui'</span><span class="p">,</span> <span class="s1">'dignissimos'</span> <span class="p">]</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'debitis et nisi aut vero illo rem'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s1">'magnam'</span><span class="p">,</span> <span class="s1">'sunt'</span><span class="p">,</span> <span class="s1">'quia'</span> <span class="p">]</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'non iusto ratione sed'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s1">'earum'</span><span class="p">,</span> <span class="s1">'provident'</span><span class="p">,</span> <span class="s1">'voluptatem'</span> <span class="p">]</span>
<span class="p">},</span>
<span class="p">{</span>
<span class="nx">content</span><span class="o">:</span> <span class="s1">'aut maxime aspernatur expedita aut voluptates'</span><span class="p">,</span>
<span class="nx">tags</span><span class="o">:</span> <span class="p">[</span> <span class="s1">'voluptas'</span><span class="p">,</span> <span class="s1">'quo'</span><span class="p">,</span> <span class="s1">'et'</span> <span class="p">]</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>In a real environment, you might persist your generated models to the database and
instead of embedding entire objects, you only store a reference to
them via their <code>_id</code>.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p><em>Note:</em> Kin has built-in support for Mongoose models, you can specify Model
types by the name you provided when you generated the mongoose.model.
So long as Kin can find the collection via mongoose.models, you can
use a String instead of a Model reference. This is implemented to
save you having to manually <code>require</code> every model in your system.</p> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/* Saving items might look something like this if you're using an ORM like Mongoose. */</span>
<span class="kd">var</span> <span class="nx">mongoose</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'mongoose'</span><span class="p">)</span>
<span class="nx">mongoose</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s1">'kin_examples'</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>If you want to use mongoose with Kin, you need to pass your reference to mongoose to Kin</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">mongoose</span> <span class="o">=</span> <span class="nx">mongoose</span>
<span class="cm">/* Some fixtures we prepared earlier */</span>
<span class="kd">var</span> <span class="nx">User</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'../fixtures'</span><span class="p">).</span><span class="nx">User</span> <span class="c1">// Same as our user before, except as a Mongoose model</span>
<span class="kd">var</span> <span class="nx">Stream</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'../fixtures'</span><span class="p">).</span><span class="nx">Stream</span> <span class="c1">// Imagine a stream is like an RSS feed containing 'Activities'</span>
<span class="kd">var</span> <span class="nx">Activity</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'../fixtures'</span><span class="p">).</span><span class="nx">Activity</span> <span class="c1">// each Activity references a Stream ID</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'Stream'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="s1">'Stream'</span><span class="p">,</span>
<span class="nx">title</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Lorem</span><span class="p">.</span><span class="nx">words</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">pop</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserE'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="s1">'User'</span><span class="p">,</span> <span class="c1">// Note use of a String here</span>
<span class="nx">username</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">userName</span><span class="p">())</span>
<span class="p">},</span>
<span class="nx">email</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Internet</span><span class="p">.</span><span class="nx">email</span><span class="p">())</span>
<span class="p">},</span>
<span class="nx">streams</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// references to streams</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Stream'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stream</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">stream</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">savedStream</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// save the generated stream</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">stream</span><span class="p">.</span><span class="nx">_id</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserE'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">streams</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">streamId</span><span class="p">)</span> <span class="p">{</span>
<span class="cm">/* The streams we generated should be saved in the DB */</span>
<span class="nx">Stream</span><span class="p">.</span><span class="nx">findById</span><span class="p">(</span><span class="nx">streamId</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">found</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">found</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <h2>Referencing other properties</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>If you want to reference another property from within a generator,
use <code>Kin.get(this, 'someProperty')</code>. </p>
<p>Because we 'simultaneously' execute each generator function, there's no guarantee that the generator
will have run by the time we're referencing it within another property generator.
<code>Kin.get</code> ensures the property's generator has returned a value.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <h2>Hidden or Meta properties</h2>
<p>all properties beginning with an _ are not saved on the object, and
instead are returned in the <code>generate</code> callback as properties of a
third parameter, without the _ prefix.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">ObjectId</span> <span class="o">=</span> <span class="nx">mongoose</span><span class="p">.</span><span class="nx">Types</span><span class="p">.</span><span class="nx">ObjectId</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserF'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_documents</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">,</span> <span class="p">{</span><span class="nx">tags</span><span class="o">:</span> <span class="mi">3</span><span class="p">},</span> <span class="nx">callback</span><span class="p">)</span>
<span class="p">},</span>
<span class="nx">title</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Lorem</span><span class="p">.</span><span class="nx">words</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">pop</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserF'</span><span class="p">,</span> <span class="p">{</span><span class="nx">_documents</span><span class="o">:</span> <span class="mi">4</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">_documents</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">)</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">meta</span><span class="p">.</span><span class="nx">documents</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <h2>Applying post processing to a generator</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>Sometimes you always want to apply a certain action to every model generated
by your kin instance. e.g. saving a model. Kin allows you to apply 'post'
functions for exactly this situation. <em>Remember</em> you must call the callback
with the 'meta' property if you want to keep your meta data.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">ObjectId</span> <span class="o">=</span> <span class="nx">mongoose</span><span class="p">.</span><span class="nx">Types</span><span class="p">.</span><span class="nx">ObjectId</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">blueprint</span><span class="p">(</span><span class="s1">'UserG'</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="s1">'User'</span><span class="p">,</span>
<span class="nx">_streams</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'Stream'</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
<span class="p">},</span>
<span class="nx">title</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">Faker</span><span class="p">.</span><span class="nx">Lorem</span><span class="p">.</span><span class="nx">words</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">pop</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'UserG'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">user</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">saveErr</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="nx">saveErr</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserG'</span><span class="p">,</span> <span class="p">{</span><span class="nx">_streams</span><span class="o">:</span> <span class="mi">0</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">User</span><span class="p">.</span><span class="nx">findById</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">_id</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">found</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">found</span><span class="p">)</span> <span class="c1">// ensure user was saved</span>
<span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Example: <code>save</code> all generated Users and Streams</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">kin</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'UserG'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">user</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">'Stream'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">stream</span><span class="p">,</span> <span class="nx">meta</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">stream</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stream</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stream</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">})</span>
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserG'</span><span class="p">,</span> <span class="p">{</span><span class="nx">_streams</span><span class="o">:</span> <span class="mi">3</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">User</span><span class="p">.</span><span class="nx">findById</span><span class="p">(</span><span class="nx">user</span><span class="p">.</span><span class="nx">_id</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">found</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">ok</span><span class="p">(</span><span class="nx">found</span><span class="p">)</span> <span class="c1">// ensure user was saved</span>
<span class="p">})</span>
<span class="nx">Stream</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">found</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">found</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1">// ensure our 3 streams were saved</span>
<span class="p">})</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <h2>Generator functions</h2>
<p>Generator functions can be created so you can apply specific changes
to a generator, in a certain situation. To create a generator function
simply call <code>generate</code> with no callback. You can use generator functions
just like normal, or use their additional properties to make modifications.</p> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/* Simple example */</span>
<span class="kd">var</span> <span class="nx">generateUser</span> <span class="o">=</span> <span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">)</span>
<span class="nx">generateUser</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'joe'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'joe@example.com'</span><span class="p">})</span> <span class="c1">// as normal</span>
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>Pass override properties when creating the generator function or when
generating objects</p> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * All Users generated with this function will by default have username: bill,</span>
<span class="cm"> * overriding the value `joe` provided in the blueprint.</span>
<span class="cm"> */</span>
<span class="kd">var</span> <span class="nx">generateUser</span> <span class="o">=</span> <span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">'UserA'</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'bill'</span><span class="p">})</span>
<span class="nx">generateUser</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'bill'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'joe@example.com'</span><span class="p">})</span> <span class="c1">// as normal</span>
<span class="p">})</span>
<span class="nx">generateUser</span><span class="p">({</span><span class="nx">email</span><span class="o">:</span> <span class="s1">'bill@example.com'</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">user</span><span class="p">,</span> <span class="nx">meta</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">user</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">'bill'</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">'bill@example.com'</span><span class="p">})</span> <span class="c1">// as normal</span>
<span class="p">})</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>