Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update readme

  • Loading branch information...
commit 077c1cf0e39a960604d76af9c3aaa2f0356c4679 1 parent 7f700e8
@timoxley timoxley authored
Showing with 168 additions and 113 deletions.
  1. +107 −71 README.md
  2. +61 −42 docs/kin-examples.html
View
178 README.md
@@ -13,29 +13,26 @@
# Examples
We recommend the use of [Faker](https://github.com/marak/Faker.js/) for generating test data.
-# kin-examples.js
-
## Fixed Values
-Create a User blueprint with fixed values for properties.
+Create a User blueprint with fixed values for properties
```javascript
-kin.blueprint('User', {
+kin.blueprint('UserA', {
username: 'joe',
email: 'joe@example.com'
})
```
-
Generate a User object. The returned user will have the properties supplied in
the template.
```javascript
-kin.generate('User', function(err, user) {
+kin.generate('UserA', function(err, user) {
assert.deepEqual(user, {username: 'joe', email: 'joe@example.com'})
})
```
@@ -46,8 +43,8 @@ As expected, if we generate another User, we'll get the same values every
time.
```javascript
-kin.generate('User', function(err, user) {
- kin.generate('User', function(err, anotherUser) {
+kin.generate('UserA', function(err, user) {
+ kin.generate('UserA', function(err, anotherUser) {
assert.equal(anotherUser.username, user.username)
assert.equal(anotherUser.email, user.email)
})
@@ -64,7 +61,7 @@ You can optionally pass a second parameter to `generate`. These values will
override any values provided in the blueprint.
```javascript
-kin.generate('User', {username: 'bill'}, function(err, user) {
+kin.generate('UserA', {username: 'bill'}, function(err, user) {
assert.equal(user.username, 'bill') // uses the overridden value
assert.equal(user.email, 'joe@example.com') // uses the blueprint value
})
@@ -76,7 +73,7 @@ Overriding properties can also contain keys that aren't specified in the
original blueprint.
```javascript
-kin.generate('User', {manager: 'alice'}, function(err, user) {
+kin.generate('UserA', {manager: 'alice'}, function(err, user) {
assert.equal(user.username, 'joe') // uses the blueprint value
assert.equal(user.email, 'joe@example.com') // uses the blueprint value
assert.equal(user.manager, 'alice') // non-blueprint key
@@ -98,7 +95,7 @@ For instance, you might use this as your model's constructor:
```javascript
/* User Constructor */
-var User = function(properties) {
+var UserB = function(properties) {
properties = properties || {}
this.username = properties.username
this.email = properties.email || this.username + '@example.com'
@@ -111,8 +108,8 @@ Specify the constructor you'd like to use with the special `_model` property.
_Note:_ you won't find the _model property on the generated object.
```javascript
-kin.blueprint('User', {
- _model: User,
+kin.blueprint('UserB', {
+ _model: UserB,
username: function(callback) {
callback(null, Faker.Internet.userName())
}
@@ -125,15 +122,16 @@ Kin will use the supplied constructor when generating this model, passing-in
any generated values as the first (and only) argument.
```javascript
-kin.generate('User', function(err, user) {
+kin.generate('UserB', function(err, user) {
assert.ok(user.username && user.username.length)
assert.equal(user.email, user.username + '@example.com') // Test against constructor behaviour to ensure it was used.
- assert.ok(user instanceof User)
+ assert.ok(user instanceof UserB)
assert.equal(user._model, undefined) // _model is not stored on the object
})
```
+
## Dynamic Values
@@ -142,7 +140,7 @@ Generating fixed values isn't much fun. We can also define asyncronous
functions as dynamic generators for properties.
```javascript
-kin.blueprint('User', {
+kin.blueprint('UserC', {
username: function(callback) {
callback(null, Faker.Internet.userName())
},
@@ -151,7 +149,7 @@ kin.blueprint('User', {
}
})
-kin.generate('User', function(err, user) {
+kin.generate('UserC', function(err, user) {
assert.ok(user.username) // some random username as defined by Faker, eg "Rupert_Mertz"
assert.ok(user.email) // some random email as defined by Faker, eg "Brook_Bednar@price.us"
})
@@ -163,8 +161,8 @@ Generating another user should always run the matching generation function,
generating different data each time
```javascript
-kin.generate('User', function(err, user) {
- kin.generate('User', function(err, anotherUser) {
+kin.generate('UserC', function(err, user) {
+ kin.generate('UserC', function(err, anotherUser) {
assert.notEqual(anotherUser.username, user.username)
assert.notEqual(anotherUser.email, user.email)
})
@@ -172,6 +170,7 @@ kin.generate('User', function(err, user) {
```
+
_Remember_ When using a generation function, you must always use a callback to
return the value, in the standard `err, value` format: `callback(err, value1[,
value2, value3... valueN])`
@@ -234,7 +233,7 @@ A common use case for generating multiple values is creating multi-level model
hierarchies.
```javascript
-kin.blueprint('User', {
+kin.blueprint('UserD', {
username: function(callback) {
callback(null, Faker.Internet.userName())
},
@@ -253,7 +252,7 @@ We can easily generate a `User` with some number of `Document`s by simply
passing a number as the value of the documents property.
```javascript
-kin.generate('User', {documents: 5}, function(err, user) {
+kin.generate('UserD', {documents: 5}, function(err, user) {
assert.equal(user.documents.length, 5)
})
@@ -309,7 +308,6 @@ mongoose.connect('kin_examples')
```
-
If you want to use mongoose with Kin, you need to pass your reference to
mongoose to Kin
@@ -325,10 +323,10 @@ kin.blueprint('Stream', {
_model: 'Stream',
title: function(callback) {
callback(null, Faker.Lorem.words(1).pop())
- }
+ }
})
-kin.blueprint('User', {
+kin.blueprint('UserE', {
_model: 'User', // Note use of a String here
username: function(callback) {
callback(null, Faker.Internet.userName())
@@ -345,7 +343,7 @@ kin.blueprint('User', {
}
})
-kin.generate('User', function(err, user) {
+kin.generate('UserE', function(err, user) {
_.each(user.streams, function(streamId) {
/* The streams we generated should be saved in the DB */
Stream.findById(streamId, function(err, found) {
@@ -353,8 +351,8 @@ kin.generate('User', function(err, user) {
})
})
})
-```
+```
## Referencing other properties
@@ -380,7 +378,7 @@ without the _ prefix.
```javascript
var ObjectId = mongoose.Types.ObjectId
-kin.blueprint('User', {
+kin.blueprint('UserF', {
_documents: function(callback) {
kin.generate('Document', {tags: 3}, callback)
},
@@ -389,7 +387,7 @@ kin.blueprint('User', {
}
})
-kin.generate('User', {_documents: 4}, function(err, user, meta) {
+kin.generate('UserF', {_documents: 4}, function(err, user, meta) {
assert.equal(user._documents, undefined)
assert.equal(meta.documents.length, 4)
})
@@ -398,64 +396,102 @@ kin.generate('User', {_documents: 4}, function(err, user, meta) {
## Applying post processing to a generator
+
+
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. _Remember_ you must call the callback
with the 'meta' property if you want to keep your meta data.
-```javascript
+```javascript
var ObjectId = mongoose.Types.ObjectId
-kin.blueprint('User', {
- _model: 'User',
- _streams: function(callback) {
- kin.generate('Stream', callback)
- },
- title: function(callback) {
- callback(null, Faker.Lorem.words(1).pop())
- }
+kin.blueprint('UserG', {
+ _model: 'User',
+ _streams: function(callback) {
+ kin.generate('Stream', callback)
+ },
+ title: function(callback) {
+ callback(null, Faker.Lorem.words(1).pop())
+ }
})
-kin.post('User', function(user, meta, callback) {
- user.save(function(saveErr, user) {
- callback(saveErr, user, meta)
- })
+
+kin.post('UserG', function(user, meta, callback) {
+ user.save(function(saveErr, user) {
+ callback(saveErr, user, meta)
+ })
})
-kin.generate('User', {_streams: 0}, function(err, user, meta) {
- User.findById(user._id, function(err, found) {
- assert.ok(found) // ensure user was saved
- })
+
+kin.generate('UserG', {_streams: 0}, function(err, user, meta) {
+ User.findById(user._id, function(err, found) {
+ assert.ok(found) // ensure user was saved
+ })
})
```
-Example: if you also want to save nested items, you could do this in the 'top
-level' generator's post function (e.g. save documents via documents stored in
-the meta parameter in User's post function)
+
+Example: `save` all generated Users and Streams
+
+```javascript
+kin.post('UserG', function(user, meta, callback) {
+ user.save(function(err, user) {
+ callback(err, user, meta)
+ })
+})
+
+kin.post('Stream', function(stream, meta, callback) {
+ stream.save(function(err, stream) {
+ callback(err, stream, meta)
+ })
+})
+
+
+kin.generate('UserG', {_streams: 3}, function(err, user, meta) {
+ User.findById(user._id, function(err, found) {
+ assert.ok(found) // ensure user was saved
+ })
+ Stream.find(function(err, found) {
+ assert.equal(found.length, 3) // ensure our 3 streams were saved
+ })
+})
+```
+
+
+
+## Generator functions
+
+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
+`generate` with no callback. You can use generator functions just like normal,
+or use their additional properties to make modifications.
```javascript
-kin.post('User', function(user, meta, callback) {
- user.save(function(saveErr, user) {
- var numSavedStreams = 0
- for(var i = 0; i < meta.streams.length; i++) {
- var stream = meta.streams[i]
- var streamSaveErrs
- stream.save(function(streamSaveErr) {
- streamSaveErrs = streamSaveErr
- numSavedStreams++
- if (numSavedStreams == meta.streams.length) {
- callback(saveErr || streamSaveErrs, user, meta)
- }
- })
- }
- })
-})
+/* Simple example */
+var generateUser = kin.generate('UserA')
-kin.generate('User', {_streams: 3}, function(err, user, meta) {
- User.findById(user._id, function(err, found) {
- assert.ok(found) // ensure user was saved
- })
- Stream.find(function(err, found) {
- assert.equal(found.length, 3) // ensure our 3 streams were saved
- })
+generateUser(function(err, user, meta) {
+ assert.deepEqual(user, {username: 'joe', email: 'joe@example.com'}) // as normal
})
+
```
+
+Pass override properties when creating the generator function or when
+generating objects.
+
+
+```javascript
+/*
+ * All Users generated with this function will by default have username: bill,
+ * overriding the value `joe` provided in the blueprint.
+ */
+var generateUser = kin.generate('UserA', {username: 'bill'})
+
+generateUser(function(err, user, meta) {
+ assert.deepEqual(user, {username: 'bill', email: 'joe@example.com'}) // as normal
+})
+
+generateUser({email: 'bill@example.com'}, function(err, user, meta) {
+ assert.deepEqual(user, {username: 'bill', email: 'bill@example.com'}) // as normal
+})
+```
View
103 docs/kin-examples.html
@@ -7,46 +7,46 @@
<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">&#39;node-uuid&#39;</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">&#182;</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">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</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">&#182;</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">&#182;</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">&#39;UserA&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">username</span><span class="o">:</span> <span class="s1">&#39;joe&#39;</span><span class="p">,</span>
<span class="nx">email</span><span class="o">:</span> <span class="s1">&#39;joe@example.com&#39;</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">&#182;</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">&#39;User&#39;</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>
+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">&#39;UserA&#39;</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">&#39;joe&#39;</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">&#39;joe@example.com&#39;</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">&#182;</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">&#39;User&#39;</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">&#39;User&#39;</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="p">})</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</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">&#39;UserA&#39;</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">&#39;UserA&#39;</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">&#182;</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">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">&#39;bill&#39;</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>
+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">&#39;UserA&#39;</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">&#39;bill&#39;</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">&#39;bill&#39;</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">&#39;joe@example.com&#39;</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">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</span><span class="nx">manager</span><span class="o">:</span> <span class="s1">&#39;alice&#39;</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>
+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">&#39;UserA&#39;</span><span class="p">,</span> <span class="p">{</span><span class="nx">manager</span><span class="o">:</span> <span class="s1">&#39;alice&#39;</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">&#39;joe&#39;</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">&#39;joe@example.com&#39;</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">&#39;alice&#39;</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">&#182;</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">&#182;</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">&#182;</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">User</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>
+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">&#182;</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">&#39;@example.com&#39;</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">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</span>
- <span class="nx">_model</span><span class="o">:</span> <span class="nx">User</span><span class="p">,</span>
+<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">&#39;UserB&#39;</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">&#182;</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">&#39;User&#39;</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>
+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">&#39;UserB&#39;</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">&amp;&amp;</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">&#39;@example.com&#39;</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">User</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="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">&#182;</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">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</span>
+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">&#39;UserC&#39;</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>
@@ -55,12 +55,12 @@
<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">&#39;User&#39;</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">&#39;UserC&#39;</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 &quot;Rupert_Mertz&quot;</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 &quot;Brook_Bednar@price.us&quot;</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">&#182;</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">&#39;User&#39;</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">&#39;User&#39;</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>
+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">&#39;UserC&#39;</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">&#39;UserC&#39;</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>
@@ -98,7 +98,7 @@
<span class="p">})</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</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">&#39;User&#39;</span><span class="p">,</span> <span class="p">{</span>
+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">&#39;UserD&#39;</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>
@@ -109,7 +109,7 @@
<span class="nx">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">&#39;Document&#39;</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">&#182;</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">&#39;User&#39;</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>
+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">&#39;UserD&#39;</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>
@@ -163,7 +163,7 @@
<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">&#39;User&#39;</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">&#39;UserE&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="s1">&#39;User&#39;</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>
@@ -180,7 +180,7 @@
<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">&#39;User&#39;</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">&#39;UserE&#39;</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>
@@ -198,7 +198,7 @@
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">&#39;User&#39;</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">&#39;UserF&#39;</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">&#39;Document&#39;</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>
@@ -207,7 +207,7 @@
<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">&#39;User&#39;</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">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">&#39;UserF&#39;</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">&#182;</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">&#182;</a> </div> <p>Sometimes you always want to apply a certain action to every model generated
@@ -215,7 +215,7 @@
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">&#39;User&#39;</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">&#39;UserG&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">_model</span><span class="o">:</span> <span class="s1">&#39;User&#39;</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">&#39;Stream&#39;</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
@@ -224,41 +224,60 @@
<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">&#39;User&#39;</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">kin</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;UserG&#39;</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">&#39;User&#39;</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">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">&#39;UserG&#39;</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">&#182;</a> </div> <p>Example: if you also want to save nested items, you could do
-this in the 'top level' generator's post function (e.g. save documents via
-documents stored in the meta parameter in User's post function)</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">&#39;User&#39;</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="kd">var</span> <span class="nx">numSavedStreams</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">meta</span><span class="p">.</span><span class="nx">streams</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">stream</span> <span class="o">=</span> <span class="nx">meta</span><span class="p">.</span><span class="nx">streams</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
- <span class="kd">var</span> <span class="nx">streamSaveErrs</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">streamSaveErr</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">streamSaveErrs</span> <span class="o">=</span> <span class="nx">streamSaveErr</span>
- <span class="nx">numSavedStreams</span><span class="o">++</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">numSavedStreams</span> <span class="o">==</span> <span class="nx">meta</span><span class="p">.</span><span class="nx">streams</span><span class="p">.</span><span class="nx">length</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="o">||</span> <span class="nx">streamSaveErrs</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="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">&#182;</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">&#39;UserG&#39;</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">&#39;Stream&#39;</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">&#39;User&#39;</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">kin</span><span class="p">.</span><span class="nx">generate</span><span class="p">(</span><span class="s1">&#39;UserG&#39;</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">&#182;</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">&#39;UserA&#39;</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">&#39;joe&#39;</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">&#39;joe@example.com&#39;</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">&#182;</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">&#39;UserA&#39;</span><span class="p">,</span> <span class="p">{</span><span class="nx">username</span><span class="o">:</span> <span class="s1">&#39;bill&#39;</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">&#39;bill&#39;</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">&#39;joe@example.com&#39;</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">&#39;bill@example.com&#39;</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">&#39;bill&#39;</span><span class="p">,</span> <span class="nx">email</span><span class="o">:</span> <span class="s1">&#39;bill@example.com&#39;</span><span class="p">})</span> <span class="c1">// as normal</span>
<span class="p">})</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
Please sign in to comment.
Something went wrong with that request. Please try again.