<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Feathers-plus - Medium]]></title>
        <description><![CDATA[Bridging FeathersJS and real world apps - Medium]]></description>
        <link>https://medium.com/feathers-plus?source=rss----9eaf9206aade---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Feathers-plus - Medium</title>
            <link>https://medium.com/feathers-plus?source=rss----9eaf9206aade---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 18:57:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/feathers-plus" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create your own super scaffolding — automatically generate the boilerplate]]></title>
            <link>https://medium.com/feathers-plus/how-to-quickly-build-or-expand-your-feathers-plus-cli-app-without-answering-prompts-c45292ecf29c?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/c45292ecf29c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Thu, 01 Nov 2018 22:20:26 GMT</pubDate>
            <atom:updated>2018-11-14T12:57:08.621Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nd/4.0/</cc:license>
            <content:encoded><![CDATA[<h3>Create your own super scaffolding — automatically generate the boilerplate</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YOmQTvkqwavpjCXMHjwwOg.png" /><figcaption>Live the life using the cli+ pipeline</figcaption></figure><p>You can quickly generate a new starter app or add dozens of new services by avoiding the Feathers-plus/cli (cli+) prompts. With this technique you can build your own super-scaffolding tool.</p><h3>Adding 5, 10, 20 services</h3><p>People have asked me if there was a quick way to add 5, 10 and 20+ services to their app. Here’s what I tell them: <strong>yes!</strong></p><p>They had usually generated a starter app containing a <em>users</em> service, plus a couple of others. They had exercised the cli+ features and they now wanted to generate the remaining services they needed.</p><p>They wanted to know if there was a way to avoid running generate service 5, 10 and in one case 20+ times. One motivated person even suggested implementing a new command syntax like generate service blogs posts comments stars --template users.</p><p>However cli+ has always been able to quickly create services, as well as doing so much more. It all revolves around using the <em>feathers-gen-specs.json</em> file.</p><h3>feathers-gen-specs.json (a.k.a. specs.json)</h3><p>This is what file <em>myApp/feathers-gen-specs.json</em> contains once you’ve run generate app and then generate service for the <em>users</em> service.</p><pre>{<br>  <strong>&quot;options&quot;</strong>: {<br>    <strong>&quot;ver&quot;</strong>: <strong>&quot;1.0.0&quot;</strong>,<br>    <strong>&quot;inspectConflicts&quot;</strong>: <strong>false</strong>,<br>    <strong>&quot;semicolons&quot;</strong>: <strong>true</strong>,<br>    <strong>&quot;freeze&quot;</strong>: [],<br>    <strong>&quot;ts&quot;</strong>: <strong>false<br>  </strong>},<br>  <strong>&quot;app&quot;</strong>: {<br>    <strong>&quot;environmentsAllowingSeedData&quot;</strong>: <strong>&quot;&quot;</strong>,<br>    <strong>&quot;seedData&quot;</strong>: <strong>false</strong>,<br>    <strong>&quot;name&quot;</strong>: <strong>&quot;feathers-app&quot;</strong>,<br>    <strong>&quot;description&quot;</strong>: <strong>&quot;Project feathers-app&quot;</strong>,<br>    <strong>&quot;src&quot;</strong>: <strong>&quot;src&quot;</strong>,<br>    <strong>&quot;packager&quot;</strong>: <strong>&quot;npm@&gt;= 3.0.0&quot;</strong>,<br>    <strong>&quot;providers&quot;</strong>: [<br>      <strong>&quot;rest&quot;</strong>,<br>      <strong>&quot;socketio&quot;<br>    </strong>]<br>  },<br>  <strong>&quot;services&quot;</strong>: {<br>    <strong>&quot;users&quot;</strong>: {<br>      <strong>&quot;name&quot;</strong>: <strong>&quot;users&quot;</strong>,<br>      <strong>&quot;nameSingular&quot;</strong>: <strong>&quot;user&quot;</strong>,<br>      <strong>&quot;subFolder&quot;</strong>: <strong>&quot;&quot;</strong>,<br>      <strong>&quot;fileName&quot;</strong>: <strong>&quot;users&quot;</strong>,<br>      <strong>&quot;adapter&quot;</strong>: <strong>&quot;mongoose&quot;</strong>,<br>      <strong>&quot;path&quot;</strong>: <strong>&quot;/users&quot;</strong>,<br>      <strong>&quot;isAuthEntity&quot;</strong>: <strong>false</strong>,<br>      <strong>&quot;graphql&quot;</strong>: <strong>false<br>    </strong>}<br>  },<br>  <strong>&quot;hooks&quot;</strong>: {},<br>  <strong>&quot;connections&quot;</strong>: {<br>    <strong>&quot;mongoose&quot;</strong>: {<br>      <strong>&quot;database&quot;</strong>: <strong>&quot;mongodb&quot;</strong>,<br>      <strong>&quot;adapter&quot;</strong>: <strong>&quot;mongoose&quot;</strong>,<br>      <strong>&quot;connectionString&quot;</strong>: <strong>&quot;mongodb://localhost:27017/feathers_app&quot;<br>    </strong>}<br>  }<br>}</pre><p><strong>services.users</strong> contains the configuration for the <em>users</em> service. Most of the properties are just your responses to the prompts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*7zP-tlYunChFt2-LdM5ONw.png" /><figcaption>Prompts configuring the “users” service</figcaption></figure><p>The 2 additional ones are</p><ul><li><strong>filename</strong>: Used to form npm-compatible folder and file names for the service. By default a service name like <em>userRoles</em> will have a <em>filename</em> of <em>user-roles.</em></li><li><strong>isAuthEntity</strong>: Is this the service to be used by Feathers’ authentication to read user records? generate authentication sets the value to true for the right service.</li></ul><h3>Let’s add services</h3><p><strong>Step 1.</strong> You can add as many services as you want by duplicating the <em>users</em> object as many times as needed and customizing the copies.</p><p>Its very easy to customize the duplicate objects using your <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a>. Do a find-and-replace on the word “user” (singular!), replacing it with the new service names. Here’s my IDE half-way through renaming “user” to “post”. I would replace “post” by “comment” as I got to the second copy of <em>users</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*0NGLIFfT-5umgD_tnf5ffw.png" /><figcaption>Half-way through renaming one service from users to posts</figcaption></figure><p><strong>Step 2.</strong> Run generate all to generate the modules for all your new services.</p><h3>The internals of cli+</h3><p>You may be wondering if this is a hacky way to generate a service. Not at all. <a href="https://github.com/feathers-plus/generator-feathers-plus/tree/master/test-expands/cumulative-1-nedb.test-copy">It’s what the majority of the cli+ tests do.</a> They take a test <em>specs.json</em> file along with some snippets of custom code, they run generate app and then they do a string compare of each resulting module to what is expected.</p><p>The design of cli+ is based on the generator prompts being written to <em>specs.json,</em> while the code generation itself is based only on the contents of that file, and the custom code extracted from the app’s modules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*I5k3C6yDAvts_lLZcPkTHw.png" /><figcaption>cli+ prompts write to feathers-gen-specs.json, and the generator reads from there</figcaption></figure><p>That’s why changing <em>specs.json</em> works. It’s what the code writer looks at.</p><h3>What else can we do?</h3><p>The cli+ design allows you to modify <em>specs.json</em> either manually or via your own code. You can then run generate all to generate the appropriate code.</p><p>You could, for example, generate a custom starter app containing services and authentication the way you want them configured. You can seed new projects just by copying the starter app’s <em>specs.json</em> file.</p><p>You could take that starter file and merge it with other partial configurations. It’s just JSON after all.</p><p>You could attach your <em>specs.json</em> to a GitHub issue as it would give the cli+ maintainers a good idea of what your app looks like.</p><h3>In conclusion</h3><p>This article is part of a series of articles on <a href="https://medium.com/@eddyystop/write-your-own-app-generator-by-leveraging-feathers-plus-cli-37ad8dad9847">writing your own app generator by leveraging feathers-plus/cli</a>. We’ve learned here how cli+ is oriented around <em>feathers-gen-specs.json</em> and how we can (carefully!) manipulate it. This will be fundamental as we go forward.</p><p>However we have yet to discuss custom code. We might know how to generate a new service, but we don’t yet know how to populate it with a schema.</p><p>So we’re not done yet. Subscribe to Feathers-plus publications to be informed of the coming articles.</p><p>As always, feel free to join <a href="https://feathersjs.slack.com/">Feathers Slack</a> to join the discussion or just lurk around.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c45292ecf29c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/how-to-quickly-build-or-expand-your-feathers-plus-cli-app-without-answering-prompts-c45292ecf29c">Create your own super scaffolding — automatically generate the boilerplate</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Topic: Write your own app generator by leveraging feathers-plus/cli]]></title>
            <link>https://medium.com/feathers-plus/write-your-own-app-generator-by-leveraging-feathers-plus-cli-37ad8dad9847?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/37ad8dad9847</guid>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Sun, 14 Oct 2018 02:51:08 GMT</pubDate>
            <atom:updated>2018-12-10T22:29:02.696Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nd/4.0/</cc:license>
            <content:encoded><![CDATA[<h3>Topic: Automatically generate your own specialized apps with cli+ templates</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PVXfIYbE1dQcXwnuZRvdyQ.jpeg" /></figure><p>Feathers-plus/cli (cli+) generates an application as you respond to its prompts. You can however leverage cli+ to build your own super scaffolding tool.</p><p>This series of articles highlights cli+ features you would utilize.</p><ul><li><a href="https://medium.com/feathers-plus/automatically-keep-your-app-up-to-the-minute-with-feathers-plus-cli-57021f24e548">Automatically keep your app up-to-the-minute with feathers-plus/cli</a>.</li><li><a href="https://medium.com/feathers-plus/how-to-quickly-build-or-expand-your-feathers-plus-cli-app-without-answering-prompts-c45292ecf29c">Create your own super scaffolding — automatically generate the boilerplat</a>e.</li><li><a href="https://medium.com/@eddyystop/create-your-own-super-scaffolding-automatically-add-custom-code-89418e888dac?sk=02ccd2663c5416bba9d4708c3ea22ab9">Create your own super scaffolding — automatically add custom code.</a></li><li>Create your own super scaffolding — customize your scaffolding.</li></ul><p>Subscribe to Feathers Flightpath and Feathers-plus publications to be notified when these articles are published.</p><p>As always, feel free to join <a href="https://feathersjs.slack.com/">Feathers Slack</a> to join the discussion or just lurk around.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37ad8dad9847" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/write-your-own-app-generator-by-leveraging-feathers-plus-cli-37ad8dad9847">Topic: Write your own app generator by leveraging feathers-plus/cli</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatically keep your app up-to-the-minute with feathers-plus/cli]]></title>
            <link>https://medium.com/feathers-plus/automatically-keep-your-app-up-to-the-minute-with-feathers-plus-cli-57021f24e548?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/57021f24e548</guid>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Mon, 08 Oct 2018 01:31:00 GMT</pubDate>
            <atom:updated>2018-10-08T20:43:15.227Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>You generated you app with feathers-plus/cli (cli+). But enhancements have now been added to what cli+ generates. How do you update your already generated app? Worse, maybe some bugs were fixed. How do you keep your app healthy?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IRiVkFigp4lC0CxVOcT5vQ.jpeg" /><figcaption>Steve Jobs always kept his tech up to date</figcaption></figure><h3>That issue on GitHub</h3><p>This story involves TypeScript. Don’t worry if you are not too familar with it; you’ll still get the point.</p><p>cli+ generates a TypeScript interface module for each service when apps are generated in TypeScript. These tell TypeScript what fields each service record contains. The generated file originally looked like</p><pre><strong>export interface </strong>Users {<br>  <strong>name</strong>: string;<br>  <strong>roleId</strong>: any;<em><br>  // ...<br></em>}</pre><p>where <em>roleId</em> is a foreign key to the r<em>oles</em> service.</p><p>Then one day <em>murbanowicz</em> posted an issue on GitHub commenting that</p><blockquote>“I need to modify the interface file to reflect data injected in hooks etc., but at the moment I do not find a place in the code which would allow to do it ... Any advice?”</blockquote><p><strong>Hmmm … good point.</strong> Jan Lohage — cli+ maintainer, TypeScript expert, and a.k.a. j2l4e on Slack—changed cli+ to instead generate</p><pre><strong>interface</strong> UserBase {<br>  <em>/</em>/ !&lt;DEFAULT&gt; code: interface<br>  <strong>name</strong>: string;<br>  <strong>roleId</strong>: any;<br>  // !end<br>}</pre><pre>// tslint:disable-next-line:no-empty-interface<br><strong>export interface</strong> User <strong>extends</strong> UserBase {<br>  // !code: more // !end<br>}</pre><p><em>murbanowicz</em> could now add his additional fields at <em>more</em></p><pre><strong>interface</strong> UserBase {<br>  // !&lt;DEFAULT&gt; code: interface<br>  <strong>name</strong>: string;<br>  <strong>roleId</strong>: any;<br>  // !end<br>}</pre><pre>// tslint:disable-next-line:no-empty-interface<br><strong>export interface</strong> User <strong>extends</strong> UserBase {<br>  // !code: more<br>  <strong>injectedField</strong>: string;<br>  // !end<br>}</pre><p>Jan later realized he could further improve this code when TypeScript 3.0 introduced the unknown type. The generated file now looks like</p><pre><strong>export interface </strong>UserBase {<br>  // !&lt;DEFAULT&gt; code: interface<em><br>  </em><strong><em>name</em></strong><em>: string</em>;<br>  <strong>roleId</strong>: unknown;<br>  // !end<em><br></em>}<br><br>// tslint:disable-next-line:no-empty-interface<em><br></em><strong>export interface </strong>User <strong>extends </strong>UserBase {<br>  // !&lt;DEFAULT&gt; code: more<em><br>  </em><strong>roleId</strong>: any; <em>// change if needed<br>  </em>// !end<em><br></em>}</pre><p>This is a notable improvement over the original code, and it’s possible that more improvements will be made in the future.</p><h3>But here’s the problem …</h3><p>Any new services get this latest, greatest interface module. That’s fine, but what about <em>murbanowicz’s </em>app that started all this? Does he have to change each of his service interfaces by hand?</p><p>Jan has multiple apps and many services. What does he do?</p><p>What about services other people have generated? They likely don’t even know these improvements have been made.</p><p>What about services which were generated after the first improvement but before the last one?</p><p>Do we have to live with <strong>out-of-date app modules</strong> reflecting the entire history of cli+?</p><h3>generate all</h3><p>All these problems go away with <em>regeneration</em>. cli+ can regenerate your app’s modules using the most recent templates, without losing any of the custom code you added to those modules.</p><p>You can upgrade your app to contain the latest, greatest code produced by cli+ with</p><pre>npm i -g @feathers-plus/cli<br>cd ./path/to/my/app<br>feathers-plus generate all</pre><p><em>murbanowicz’s</em> interface module was automatically updated when he did that.</p><p>You can also regenerate parts of your app. For example you can regenerate all the modules associated with service <em>users</em> by running generate service and selecting <em>users</em>.</p><p>However most people just run generate all because it’s error proof, and it only takes 1 or 2 seconds more to regenerate the whole app.</p><h3>In conclusion</h3><p>You can be sure your app’s modules reflect the most current cli+ by running generate all. Any known bugs in the boilerplate are automatically fixed. Any new features are automatically added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*evS1_srGUDY53k16FVFVlQ.jpeg" /><figcaption>‘generate all’ keeps your app healthy</figcaption></figure><p>This article is part of a series of articles on <a href="https://medium.com/@eddyystop/write-your-own-app-generator-by-leveraging-feathers-plus-cli-37ad8dad9847">writing your own app generator by leveraging feathers-plus/cli</a>. And we’re not done yet … so subscribe to Feathers-plus publications to remain informed.</p><p>As always, feel free to join <a href="https://feathersjs.slack.com/">Feathers Slack</a> to join the discussion or just lurk around.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57021f24e548" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/automatically-keep-your-app-up-to-the-minute-with-feathers-plus-cli-57021f24e548">Automatically keep your app up-to-the-minute with feathers-plus/cli</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatic testing with feathers-plus/cli]]></title>
            <link>https://medium.com/feathers-plus/automatic-tests-with-feathers-plus-cli-4844721a29cf?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/4844721a29cf</guid>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Sun, 23 Sep 2018 13:19:17 GMT</pubDate>
            <atom:updated>2018-10-15T19:49:11.627Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*Aj4uKEWlXBkXhJxxx-XIGA.gif" /><figcaption>Unit testers be like: “Looks like it’s working” (*)</figcaption></figure><p>Feathers-plus/cli generates unit, integration and client/server tests for hooks, services and authentication. These help give you increased confidence in your test suite.</p><p>This article is part 3 in a set of publications dealing with testing in Feathers.</p><ul><li><a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">Automatic fake data with feathers-plus/cli</a></li><li><a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">Automatically seeding data with feathers-plus/cli</a></li><li>Automatic tests with feathers-plus/cli (this article)</li><li><a href="https://medium.com/@eddyystop/use-decision-tables-to-write-better-tests-faster-835b18906cf8?sk=d464c55af3b7bb4be795794afaa86247">Use decision tables to write better tests faster</a>.</li><li>Automatic auditing of tests with feathers-plus/cli (to come)</li></ul><h3>A minimum viable app to test</h3><p>We need an app that we can generate multiple tests for. Medium <a href="https://medium.com/@j2L4e/feathers-plus-cli-5c7ba0015e8e">articles</a> have already been <a href="https://medium.com/@mattchewone/graphql-with-feathersjs-4cc67e785bd">published</a> describing how to generate services with feathers-plus/cli (cli+), so we will skim through that process here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*BkDu4nvC_X8KDkuytMTgOw.png" /><figcaption>Generating our test app</figcaption></figure><p>Also generate service roles like we didteams.</p><p>Now let’s generate a hook. We’ll name it <strong>skipRemainingHooks</strong> because, for illustrative reasons, it’ll do exactly what <strong>feathers-hooks-common</strong> ‘s <a href="https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#skipremaininghooks">hook of the same name</a> does.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*8QIRhcHLLFX04HpK1jYe3w.png" /><figcaption>feathers-plus generate hook</figcaption></figure><h3>Testing hooks</h3><p>cli+ can generate both unit and integration tests for hooks. These tests use the popular <a href="https://codeburst.io/how-to-test-javascript-with-mocha-the-basics-80132324752e">Mocha test framework</a>.</p><p>A template for a unit test is automatically created whenever you generate a hook. By running generate test, you can regenerate this unit test or generate the integration test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*wEWq37f0U_vgnc-rKMp0Zw.png" /><figcaption>feathers-plus generate test</figcaption></figure><p>Once you have the template, just customize it with your tests.</p><h3>Unit testing hooks</h3><p>Unit tests for hooks are great because, in addition to simple conditions, they can test</p><ul><li>edge case conditions,</li><li>conditions that are more complicated and difficult to trigger,</li><li>conditions which require time to trigger, or that are date based.</li></ul><p>Almost all of the 759 tests in <a href="https://feathers-plus.github.io/v1/feathers-hooks-common/"><strong>feathers-hooks-common</strong></a> are unit tests. Many other Feathers repos also use unit tests for their hooks.</p><p>You can look at this <a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/skip-remaining-hook.unit-side.html">code comparison between the generated template for a unit test and the completed test for </a><a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/skip-remaining-hook.unit-side.html"><strong>skipRemainingHooks</strong></a><a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/skip-remaining-hook.unit-side.html">.</a></p><p>Let’s look at some of the code in the completed test. You should already know that Feathers passes a <a href="https://docs.feathersjs.com/api/hooks.html#hook-context"><strong>context</strong> object</a> to hooks. Here we build some of those <strong>context</strong> objects so we can use them later in tests.</p><pre>contextBefore = {<br>  type: <strong>&#39;before&#39;</strong>,<br>  params: { provider: <strong>&#39;socketio&#39; </strong>},<br>  data: {<br>    first: <strong>&#39;John&#39;</strong>, last: <strong>&#39;Doe&#39;<br>  </strong>}<br>};<br><br>contextAfter = {<br>  type: <strong>&#39;after&#39;</strong>,<br>  params: { provider: <strong>&#39;socketio&#39; </strong>},<br>  result: {<br>    first: <strong>&#39;Jane&#39;</strong>, last: <strong>&#39;Doe&#39;<br>  </strong>}<br>};</pre><p><strong>contextBefore</strong> is a short version of what a <em>before</em> hook would receive for, say, a create call. <strong>contextAfter</strong> is what an <em>after</em> hook would receive.</p><p>We can use these <strong>context</strong> objects to call the hooks directly, testing many conditions quickly.</p><pre><strong>const </strong>SKIP = <strong>require</strong>(<strong>&#39;@feathersjs/feathers&#39;</strong>).<strong>SKIP</strong>;<br><br>describe(<strong>&#39;Predicate is not a function&#39;</strong>, () =&gt; {<br>  <strong>it</strong>(<strong>&#39;False returns context&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks(<strong>false</strong>)(contextBefore);<br>    assert.equal(result, contextBefore);<br>  });<br><br>  <strong>it</strong>(<strong>&#39;True returns SKIP token&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks(<strong>true</strong>)(contextBefore);<br>    assert.equal(result, SKIP);<br>  });<br>});<br><br>describe(<strong>&#39;Predicate is a function&#39;</strong>, () =&gt; {<br>  <strong>it</strong>(<strong>&#39;False returns context&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks(() =&gt; <strong>false</strong>)(contextBefore);<br>    assert.equal(result, contextBefore);<br>  });<br><br>  <strong>it</strong>(<strong>&#39;True returns SKIP token&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks(() =&gt; <strong>true</strong>)(contextBefore);<br>    assert.equal(result, SKIP);<br>  });<br>});<br><br>describe(<strong>&#39;Default predicate is &quot;context =&gt; !!context.result&quot;&#39;</strong>, () =&gt; {<br>  <strong>it</strong>(<strong>&#39;No context.result&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks()(contextBefore);<br>    assert.equal(result, contextBefore);<br>  });<br><br>  <strong>it</strong>(<strong>&#39;Has context.result&#39;</strong>, () =&gt; {<br>    <strong>const </strong>result = skipRemainingHooks()(contextAfter);<br>    assert.equal(result, SKIP);<br>  });<br>});</pre><h3>Time for a story</h3><p>I chose <strong>skipRemainingHooks</strong> as our example so I could tell you the following story. I wrote that hook for <strong>feathers-hooks-common</strong> when <strong>SKIP</strong> arrived as a PR. The PR said it skipped the remaining hooks, the Feathers docs said it skipped the remaining hooks, so … it must skip the remaining hooks. Right?</p><p><strong>SKIP</strong> was key to the design of the new <strong>softDelete2</strong> hook. However weird problems appeared during testing.</p><p>It turns out <strong>SKIP</strong> skips the remaining hooks in the <em>before, after, </em>or <em>error</em> section it’s used in. If you use it in the <em>before</em> section, none of the remaining <em>before</em> hooks would be run, but the <em>after</em> hooks will still be run. Basically the phrase “skips the remaining hooks” was ambiguous.</p><p>This was news to all the Feathers maintainers, and the docs are now updated.</p><p>This story illustrates the need for integration testing when your hook interacts with Feathers’ infrastructure in a non-trivial way. The same concept applies to testing services and authentication: you should use Feathers infrastructure in your tests rather than try to fake or stub it.</p><p>My suggestion is to largely limit your unit tests to code which does not relate to Feathers, and to testing hooks (because unit tests are so convenient there).</p><p>You can have increasing confidence in your tests as you move from unit tests, to integration tests, to client/server tests. The tests will get <strong>harder to write</strong> and <strong>slower to run</strong> as you move up this hierarchy. However cli+ generates test templates for all of these, significantly reducing the work involved.</p><p>In summary, as one notable article says, <a href="https://blog.kentcdodds.com/write-tests-not-too-many-mostly-integration-5e8c7fff591c"><strong>Write tests. Not too many. Mostly integration.</strong></a></p><h3>Integration testing of hooks</h3><p>Let’s generate a template for the integration test. This will check <strong>SKIP</strong> works as described above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*aEm9TIH0Qqo0hMx7phNmvA.png" /><figcaption>feathers-plus generate test, option hook integration</figcaption></figure><p>You can look at this <a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/skip-remaining-hook.integ-side.html">code comparison between the generated template for an integration test and the completed test for <strong>skipRemainingHooks</strong>.</a></p><p>Let’s look at some of the code in the completed test. Our test does not need fake data, so we removed the code reading the fake data generated by generate fakes.</p><pre><strong>const </strong>{ join } = <strong>require</strong>(<strong>&#39;path&#39;</strong>);<br><strong>const </strong>{ readJsonFileSync } = <strong>require</strong>(<strong>&#39;@feathers-plus/test-utils&#39;</strong>);<br><strong>// ...</strong><br><br><em>// Get generated fake data<br>// eslint-disable-next-line no-unused-vars<br></em><strong>const </strong>fakeData = readJsonFileSync(join(__dirname, <strong>&#39;../../seeds/fake-data.json&#39;</strong>)) || {};</pre><p>We added a hook which logs calls made to it, along with 2 tests.</p><pre><strong>let </strong>hooksRun;<br><br><strong>function </strong><em>setHookRun</em>( name) {<br>  <strong>return </strong>() =&gt; { hooksRun.push(name); };<br>}<br><br><strong>beforeEach</strong>(() =&gt; {<br>  app = feathers();<br>  hooksRun = [];<br><br>  app.use(<strong>&#39;/test-service&#39;</strong>, {<br>    <strong>async </strong>create(data) { <strong>return </strong>data; }<br>  });<br><br>  app.service(<strong>&#39;/test-service&#39;</strong>).hooks({<br>    before: {<br>      create: [<br>        setHookRun(<strong>&#39;before1&#39;</strong>),<br>        skipRemainingHooks(<strong>true</strong>),<br>        setHookRun(<strong>&#39;before2&#39;</strong>)<br>      ]<br>    },<br>    after: {<br>      create: [<br>        setHookRun(<strong>&#39;after1&#39;</strong>),<br>        skipRemainingHooks(<strong>true</strong>),<br>        setHookRun(<strong>&#39;after2&#39;</strong>)<br>      ]<br>    }<br>  });<br>  // ...<br>});</pre><pre>it(<strong>&#39;SKIP before skips following before hooks&#39;</strong>, <strong>async </strong>() =&gt; {<br>  <strong>const </strong>result = <strong>await </strong>service.create({ foo: <strong>&#39;bar&#39; </strong>}, params);<br>  assert(!hooksRun.includes(<strong>&#39;before2&#39;</strong>), <strong>&#39;following hooks are run&#39;</strong>);<br>});<br><br>it(<strong>&#39;SKIP before does not skip after hooks&#39;</strong>, <strong>async </strong>() =&gt; {<br>  <strong>await </strong>service.create({ foo: <strong>&#39;bar&#39; </strong>});<br>  assert.deepEqual(hooksRun, [<strong>&#39;before1&#39;</strong>, <strong>&#39;after1&#39;</strong>]);<br>});</pre><p>Voilà! We have confirmed how <strong>SKIP</strong> works.</p><blockquote>“use Feathers infrastructure in your tests rather than try to fake or stub it”</blockquote><h3>Testing services on the server</h3><p>Let’s test that the <strong>users</strong> service encrypts the password and removes it for external requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*_PfoLb5EEc8DFSeK4Pyngw.png" /><figcaption>feathers-plus generate test, option service using server</figcaption></figure><p>You can see how the <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/test/services/users/users.service.server.test.js">completed test</a> <a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/users.service.server-side.html">differs from the test template</a>.</p><p>Since this test mutates the database, we left in the code which checks if NODE_ENV allows database mutation. (Read the <a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">seeding data</a> article for more information on protecting production data from unintended changes.)</p><pre><em>// Determine if environment allows test to mutate existing DB data.<br></em><strong>const </strong>env = (config.tests || {}).environmentsAllowingSeedData || [];<br><strong>if </strong>(!env.includes(process.env.NODE_ENV)) {<br>  <em>// eslint-disable-next-line no-console<br>  </em>console.log(<strong>&#39;SKIPPED - Test users/users.service.server.test.js&#39;</strong>);<br><br>  <strong>return</strong>;<br>}</pre><h3>Testing services using client/server</h3><p>We can run exactly the same tests from a client, rather than on the server. This tests the entire Feathers transport infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*i8oKA3RPFCBK31ItfVLGQg.png" /><figcaption>feathers-plus generate test, option service using client/server</figcaption></figure><p>Once again you can see how the <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/test/services/users/users.service.client.test.js">completed test</a> <a href="http://htmlpreview.github.io/?https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/medium/users.service.client-side.html">differs from the test template</a>, and you can see how the client is configured and authenticated.</p><pre><strong>async function </strong>makeClient(host, port, email1, password1) {<br>  <strong>const </strong>client = feathersClient();<br>  <strong>const </strong>socket = io(<strong>`http://</strong>${host}<strong>:</strong>${port}<strong>`</strong>, {<br>    transports: [<strong>&#39;websocket&#39;</strong>], forceNew: <strong>true</strong>, reconnection: <strong>false</strong>, extraHeaders: {}<br>  });<br>  client.configure(feathersClient.socketio(socket));<br>  client.configure(feathersClient.authentication({<br>    storage: localStorage<br>  }));<br><br>  <strong>try </strong>{<br>    <strong>await </strong>client.authenticate({<br>      strategy: <strong>&#39;local&#39;</strong>,<br>      email: email1,<br>      password: password1,<br>    });<br>  } <strong>catch </strong>(err) {<br>    <strong>throw new </strong>Error(<strong>`Unable to authenticate: </strong>${err.message}<strong>`</strong>);<br>  }<br><br>  <strong>return </strong>client;<br>}</pre><h3>A little practical matter</h3><p>It takes a bit of time to spin up a client/server test, mainly because <a href="https://github.com/dcodeIO/bcrypt.js">bcryptjs</a> requires some time to encrypt the login password. This is by design as that delay helps impede hacking. Nevertheless, this delay can add up when running multiple client/server tests.</p><p>You can speed up your development test sequence by passing the --noclient argument to mocha.</p><pre><strong>NODE_ENV=test npm run mocha -- --noclient</strong></pre><p>This will cause the client/server-based tests to be skipped since they include</p><pre><em>// Determine if environment allows test to mutate existing DB data.</em><strong><br>const </strong>env = (config.tests || {}).environmentsAllowingSeedData || [];<br><strong>if </strong>(!env.includes(process.env.NODE_ENV) ||<br>  process.argv.includes(<strong>&#39;--noclient&#39;</strong>)) {<br>  <em>// eslint-disable-next-line no-console<br>  </em>console.log(<strong>&#39;SKIPPED - Test users/users.service.client.test.js&#39;</strong>);<br><br>  <strong>return</strong>;<br>}</pre><p>You can run the client/server tests whenever you want, and your <a href="https://en.wikipedia.org/wiki/Continuous_integration">CI</a> will run them by default.</p><h3>Testing basic authentication</h3><p>The basic authentication test ensures a client can authenticate using either an email/password combo, or a jwt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*O8iYZzu3-C6_MonzJnYKdw.png" /><figcaption>feathers-plus generate test, option basic authentication</figcaption></figure><p>The <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/test/authentication.base.test.js">generated code</a> calls a utility routine. There is little to customize. cli+ persists the specs for your app in <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/feathers-gen-specs.json"><strong>feathers-gen-specs.json</strong></a>, and <a href="https://github.com/feathers-plus/test-utils/blob/master/lib/cli/authentication.base.js"><strong>authenticationBase</strong></a> uses that to figure out what to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*sfRsZy5XTI3w5eDE9R9HvQ.png" /><figcaption>partial log from authentication.base.test.js</figcaption></figure><blockquote>“I’ve haven’t yet written an app where I didn’t have to change the default generated authentication.” — Marshall Thompson, core member, FeathersJS</blockquote><h3>Testing authentication for every service</h3><p>cli+ can generate a test to confirm the app’s authentication is configured the way you think it is. So Marshall no longer has to write such tests manually. He tells me he’s very excited by this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*UawZw1gPtBYn7onWye80ZA.png" /><figcaption>feathers-plus generate test, option auth all services</figcaption></figure><p>The <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/test/authentication.services.test.js">generated code</a> calls a utility routine. That reads <strong>feathers-gen-specs.json</strong> and figures out what to do.</p><p>It calls every method of every service. It calls them both from an authenticated and an unauthenticated client. It calls them using all the transports you configured in generate app — REST, socket.io, and Primus.</p><p>It does a lot of calls … and it checks that each call succeeds or fails as expected based on how each service was generated.</p><p>But Marshall customizes his authentication. How do we deal with that? Well, some configuration was added to <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/examples/js/12-test-examples/feathers-app/config/default.json"><strong>config/default.json</strong></a> when we generated this test.</p><pre><strong>&quot;tests&quot;</strong>: {<br>  <strong>&quot;environmentsAllowingSeedData&quot;</strong>: [<br>    <strong>&quot;test&quot;<br>  </strong>],<br>  <strong>&quot;local&quot;</strong>: {<br>    <strong>&quot;password&quot;</strong>: <strong>&quot;password&quot;<br>  </strong>},<br>  <strong>&quot;client&quot;</strong>: {<br>    <strong>&quot;port&quot;</strong>: 3030,<br>    <strong>&quot;ioOptions&quot;</strong>: {<br>      <strong>&quot;transports&quot;</strong>: [<br>        <strong>&quot;websocket&quot;<br>      </strong>],<br>      <strong>&quot;forceNew&quot;</strong>: <strong>true</strong>,<br>      <strong>&quot;reconnection&quot;</strong>: <strong>false</strong>,<br>      <strong>&quot;extraHeaders&quot;</strong>: {}<br>    },<br>    <strong>&quot;primusOptions&quot;</strong>: {<br>      <strong>&quot;transformer&quot;</strong>: <strong>&quot;ws&quot;<br>    </strong>},<br>    <strong>&quot;restOptions&quot;</strong>: {<br>      <strong>&quot;url&quot;</strong>: <strong>&quot;http://localhost:3030&quot;<br>    </strong>},<br>    <strong>&quot;overriddenAuth&quot;</strong>: {}<br>  }<br>},</pre><ul><li><strong>environmentsAllowingSeedData</strong> contains the NODE_ENV values during which the database may be mutated by tests. The <a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">Seed article</a> contains more information.</li><li><strong>password</strong> is the name of the password field in the user-entity service.</li><li><strong>ioOptions, primusOptions, restOptions</strong> are the options to pass to Feathers handlers for socket.io, Primus and REST.</li><li><strong>overriddenAuth</strong> identifies differences between the current authentication and the generated defaults.</li></ul><p>Let’s say Marshall added authentication to <strong>users</strong>’ create method (no authentication is generated for that by default), he removed authentication from <strong>teams</strong>’ find method, and he disabled <strong>teams</strong>’ patch method using the <a href="https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#disallow">disallow</a> common hook.</p><p>He would indicate the changes he made as follows</p><pre>overriddenAuth: {<br>  teams: {<br>    find: &#39;noauth&#39;,   // authentication has been removed<br>    patch: &#39;disallow&#39; // client cannot call remove<br>  },<br>  users: {<br>    create: &#39;auth&#39;    // authentication has been added<br>  }<br>}</pre><p>The authentication test would take those changes into account when it’s run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*j6pBzARAZPS08jsROwlJpQ.png" /><figcaption>partial log from authentication.services.test.js</figcaption></figure><h3>The tests/ folder</h3><p>You might be wondering where these generated tests are placed. They are in the <strong>tests</strong> folder organized in the same manner as your app. For example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*ENXRNaZfPpxY6PUUL8DQ2Q.png" /><figcaption>Structure of tests/ folder.</figcaption></figure><h3>In conclusion</h3><p>This has been a long read. So, first of all, thanks for getting through it.</p><p>It’s also been a long road getting to here. We first <a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">generated fake data</a> so it could be used in these tests. We then introduced NODE_ENV environments to <a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">protect production data from inadvertent changes</a> by the tests. Now, finally, we are able to generate the tests themselves.</p><p>And we’re not done yet … so subscribe to Feathers-plus publications to remain informed.</p><p>As always, feel free to join <a href="https://feathersjs.slack.com/">Feathers Slack</a> to join the discussion or just lurk around.</p><p>(*) Image thanks to <a href="https://twitter.com/kentcdodds/status/628658648001048577">Kent C. Dodds</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4844721a29cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/automatic-tests-with-feathers-plus-cli-4844721a29cf">Automatic testing with feathers-plus/cli</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feathers-Plus CLI]]></title>
            <link>https://medium.com/feathers-plus/feathers-plus-cli-5c7ba0015e8e?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/5c7ba0015e8e</guid>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[j2L4e]]></dc:creator>
            <pubDate>Thu, 13 Sep 2018 14:22:59 GMT</pubDate>
            <atom:updated>2018-09-13T14:22:59.516Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Generate a FeathersJS application in TypeScript</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bXPYLGpbs9fSghOc" /><figcaption>by <a href="https://unsplash.com/@flysi3000?utm_source=medium&amp;utm_medium=referral">Simon Abrams</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Six months ago type definitions for the <a href="https://medium.com/u/fcea6654285f">FeathersJS</a> core packages were first published on <a href="https://github.com/DefinitelyTyped/DefinitelyTyped">DefinitelyTyped</a>. Yet, TypeScript adoption never really took off among Feathers users. Why’s that? There had been various open issues about Feathers Auk and Buzzard releases lacking typings, so there seemed to be at least some demand.</p><p>Those familiar with Feathers most likely have come across the original <a href="https://github.com/feathersjs/cli">Feathers CLI</a>. Setting up a working HTTP- or websocket-based API is a breeze, hooking it up with a variety of database adapters of your liking in no time. Neat. Until you try to migrate your freshly created application scaffold to TypeScript and all the lovely breeziness fades away. There’s so much refactoring that needs to be done that in the end you’re better off with writing the boilerplate by hand.</p><p>In total, using Feathers with TypeScript negated a lot of the benefits the CLI would have gotten you when you’d stuck to JavaScript.</p><h3>Feathers-Plus CLI to the rescue</h3><p>If you’ve always wanted to use Feathers with TypeScript’s benefits without sacrificing near-automatic code generation, your time has finally come.</p><p>Another generator, <a href="https://github.com/feathers-plus/cli">Feathers-Plus CLI</a>, has recently seen the light of day, one of its main features being the ability to choose TypeScript generation mode and even (in parts) regenerate an already generated JavaScript app to a TypeScript equivalent. Feathers-Plus CLI is designed to be similar to the original CLI in terms of both usage and generated code, so you’ll feel right at home if you’ve used the original before. If you haven’t, no problem. It’s quite simple, so let’s get straight to it.</p><h4>Generating a Feathers TypeScript app</h4><p>The easiest and recommended way to get things up and running is to install the Feathers-Plus CLI globally via npm (or yarn).</p><pre>npm install -g @feathers-plus/cli</pre><p>Once that’s done, create an empty folder in a place of your choice and cd to it. Instead of using generate app right away, we’ll have to tell the generator to use TypeScript first with the CLI’s generate options command.</p><pre>mkdir myProject<br>cd myProject<br>feathers-plus generate options</pre><p>The prompts are pretty self-explanatory, the one that’s important for us is Generate TypeScript Code? which we happily answer with yes. Leave the other ones alone, i.e. just use their default values by pressing the enter key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lGPltL8SZCmF0ypyaTxGvA.png" /></figure><p>You’ll now find a file named feathers-gen-specs.json in the current directory. Anything you generate or configure using the generator is saved in this file. Have a look if you like, but there’s not much to see yet. Let’s go ahead and generate the app.</p><pre>feathers-plus generate app</pre><p>Again, self-explanatory prompts. Again, just use the default values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*74cIpE37d27c3DqzQEWaOA.png" /></figure><p>You’ll end up with a basic Feathers application with working REST and websockets as you know it, but in a fully set up TypeScript development environment. ts-node and ts-mocha are included, saving you further configuration for debugging and testing.</p><p>Finally generate a service called messages with the default adapter NeDB</p><pre>feathers-plus generate service</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tXz0nQ3h8RPfYUKjH4WUUw.png" /></figure><p><em>(If the </em><em>GraphQL line caught your eye, make sure to head </em><a href="https://medium.com/@mattchewone/4cc67e785bd"><em>over here</em></a><em> later to read up on how the Feathers-Plus CLI makes GraphQL and Feathers play together.)</em></p><p>Have a look around the project, if you’ve been using the original Feathers-CLI it will look familar. You’ll find a detailed explanation of the generated files and folder structure in <a href="https://generator.feathers-plus.com/get-started/">the docs</a>.</p><p>You can easily run the application using npm start or ts-node src/ and our TypeScript codebase will be transpiled on the fly. This is great for development, but it’s rather slow in comparison to running JavaScript. For a production build, run npm run compile and the compiled application</p><h4>Interface generation</h4><p>Of course TypeScript is all about interfaces. We could create one by hand, but we’re not going to. The Feathers-Plus generator leverages service specific JSON-schemas to generate various other needed schemas, one of which is the corresponding TypeScript interface. <br>Open src/services/messages/messages.schema.ts and find line 24.</p><pre>properties: {<br>  <em>// !code: schema_properties // !end<br>}</em></pre><p>You may notice many more of similar // !code: ... comments in this file and the entire project. These are needed by the generator to identify and extract the code we enter. Far from pretty, but it gets the job done. Let’s add three properties to the messages schema: Its id, its owner’s id and the text of the message. Make sure you add it between // !code: schema_properties and // !end.</p><pre>properties: {<br>  <em>// !code: schema_properties<br>  </em>_id: { type: &#39;string&#39; },<br>  ownerId: { type: &#39;string&#39; },<br>  text: { type: &#39;string&#39; }<br>  <em>// !end<br></em>}</pre><p>After that, run feathers-plus generate all to regenerate everything. Hit enter a couple of times to keep the settings unchanged. You don’t necessarily have to regenerate the entire application, you could run feathers-plus generate service just as well, but generate all is a nice shortcut.<br>Open messages.interface.ts now and you’ll find a TypeScript interface consistent with the JSON-schema you created. You’ll also find that the other files in the messages folder containing MongoDB, Mongoose and Sequelize schemas as well as validation functions are now fully populated.</p><h4>Application interface</h4><p>Take a peek at src/app.interface.ts. It contains a type named App made up of all the generated services’ interfaces. If you’ve ever wondered, why Feathers’ Application interface is generic, here’s why.</p><p>The generic Application interface can make TypeScript aware of available services’ names as well as their interfaces. Because of this app.service(&#39;messages&#39;) returns a typed service which in the end leaves you with a properly typed response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*3NsbRR4Uxr6aBtj-OsXeeQ.png" /></figure><p>Those who do fullstack development may get the additional benefit of importing the generated interfaces in both their server and their client codebases. Sharing common functionalities between both is a thing already and having common interfaces helps ensuring client and server adhere to the API contract even before any of the code is run. Fully configuring a fullstack project is out of scope of this article, but make sure to take a look at one of the many fullstack boilerplates like <a href="https://github.com/DavideViolante/Angular-Full-Stack">Angular-Full-Stack</a> and <a href="https://github.com/gilamran/fullstack-typescript">fullstack-typescript (React)</a>.</p><p>Feel free to give FeathersJS and TypeScript a try and make sure to file any issues you have with the <a href="https://github.com/feathers-plus/cli/issues">CLI</a>, <a href="https://github.com/feathersjs/feathers/issues">Feathers</a> or with <a href="https://github.com/feathersjs-ecosystem/feathers-typescript/issues">Feathers’ typings packages</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c7ba0015e8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/feathers-plus-cli-5c7ba0015e8e">Feathers-Plus CLI</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FeathersJS: Get work done faster with `feathers-hooks-commons` typings.]]></title>
            <link>https://medium.com/feathers-plus/feathersjs-get-work-done-faster-with-feathers-hooks-commons-typings-cad04551d0b2?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/cad04551d0b2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathersjs]]></category>
            <dc:creator><![CDATA[j2L4e]]></dc:creator>
            <pubDate>Thu, 13 Sep 2018 14:00:09 GMT</pubDate>
            <atom:updated>2018-09-17T13:27:05.991Z</atom:updated>
            <content:encoded><![CDATA[<p>Don’t crawl the docs, write the code!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*3Sq9NGAGbhybnMYNv3NGXA.png" /><figcaption>Typings based intellisense in WebStorm</figcaption></figure><p>When you’re developing a FeathersJS application there’s no way around <a href="https://github.com/feathers-plus/feathers-hooks-common">feathers-hooks-common</a>. It’s a comprehensive collection of commonly used hooks (surprise!) and various utilities to aid you in creating your own hook functions. A lot of the provided hooks and functions are highly configureable, so, unfortunately, although <a href="https://feathers-plus.github.io/v1/feathers-hooks-common/guide.html">the documentation</a> is thorough you have to know your way around the package to work with it efficiently.</p><p>To help out both TypeScript and JavaScript users alike, <a href="https://github.com/feathers-plus/feathers-hooks-common/blob/master/types/index.d.ts">a typings file</a> for feathers-hooks-common was created and published with version 4.17 of the package.</p><p>Depending on your IDE you’ll now get some quick hints from the docs and links to the function’s full documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*YW9kM1ZOZc4iTphzK5qwGg.png" /><figcaption>Function documentation (WebStorm)</figcaption></figure><p>There’s also information on function parameters as you type …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*1xmBkoKD0M6cf4UgUCYY_w.png" /><figcaption>Parameter documentation (WebStorm)</figcaption></figure><p>… and, of course, full type safety for TypeScript users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*VgNIeug2wFeFnhTEWdeHIw.png" /><figcaption>Type mismatch error (WebStorm)</figcaption></figure><h4>A little note on fastJoin and batch loaders</h4><p>Batch loaders aren’t easy to wrap your head around, so they got <a href="https://github.com/feathers-plus/batch-loader/blob/master/types/index.d.ts">a typings file</a>, too. Yet to use batch loaders? They aid you in getting the number of database calls down to a minimum. Literally! So make sure to read up on them <a href="https://feathers-plus.github.io/v1/batch-loader/guide.html">in the docs</a>.</p><p>As always, feel free to join the <a href="https://feathersjs.slack.com/">Feathers Slack</a> to join the discussion or just lurk around. If you run into any problems with feathers-hooks-common you’re more than welcome to create a pull request or open an issue <a href="https://github.com/feathers-plus/feathers-hooks-common/issues">on github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cad04551d0b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/feathersjs-get-work-done-faster-with-feathers-hooks-commons-typings-cad04551d0b2">FeathersJS: Get work done faster with `feathers-hooks-commons` typings.</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatically seeding data with feathers-plus/cli]]></title>
            <link>https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/336302adfe3</guid>
            <category><![CDATA[feathersjs]]></category>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Mon, 10 Sep 2018 02:44:07 GMT</pubDate>
            <atom:updated>2018-10-15T19:47:39.571Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*mw726FpwB0L7YIEoWXhxaQ.png" /></figure><p>Feathers-plus/cli (cli+) generates JavaScript or TypeScript code for your application. However there isn’t very much you can initially do with that code because the services have no data. We need to seed those services with data.</p><p>This article is part 2 in a set of publications dealing with testing in Feathers.</p><ul><li><a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">Automatic fake data with feathers-plus/cli</a></li><li>Automatically seeding data with feathers-plus/cli (this article)</li><li><a href="https://medium.com/feathers-plus/automatic-tests-with-feathers-plus-cli-4844721a29cf">Automatic tests with feathers-plus/cli</a>.</li><li><a href="https://medium.com/@eddyystop/use-decision-tables-to-write-better-tests-faster-835b18906cf8?sk=d464c55af3b7bb4be795794afaa86247">Use decision tables to write better tests faster</a>.</li><li>Automatic auditing of tests with feathers-plus/cli (to come)</li></ul><p>A <a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">previous article</a> describes how cli+ can generate fake data based on your existing JSON-schema models. Excitingly, this includes generating values for foreign key fields which refer to other fake records.</p><p>In this artcle we’ll look at how we can seed our services’ tables, on application startup, with this fake data.</p><p>At that point we’ll have generated code, we’ll have generated data, and we’ll be ready to rock.</p><h3>Generating an app</h3><p>Medium <a href="https://medium.com/@j2L4e/feathers-plus-cli-5c7ba0015e8e">articles</a> have already been <a href="https://medium.com/@mattchewone/graphql-with-feathersjs-4cc67e785bd">published</a> describing how to generate apps with cli+, so we will skim over much of that here, while focusing on what’s important for seeding data.</p><p>First we generate the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*YziLsygCwsM4QiEqGs5qCQ.png" /></figure><p><strong>Data mutating tests and seeding may run when NODE_ENV is one of (optional)</strong></p><p>This is one of the prompts above. A Node program runs in an environment set by the <a href="https://stackoverflow.com/questions/16978256/what-is-node-env-in-express">NODE_ENV</a> environment variable. You may decide on using different environments such as the default one (no NODE_ENV specified), development, test, production, staging, and uat (user acceptance testing).</p><p>We want to be very careful not to accidentally overwrite our production or other records while seeding our fake data. So we enter here the comma-delimited environment names under which the services’ data may be seeded.</p><p>We’ve entered <strong>test</strong> here. The seeding will only run in this environment, skipping processing in other environments.</p><p>On a related topic, cli+ also generates tests. Those tests which may mutate service data also check NODE_ENV. They also skip processing if run in the “wrong” environment.</p><p><strong>Seed service records on startup when command line includes — seed?</strong></p><p>The second prompt. The code to do the seeding is generated only if yes is selected. Futhermore this seeding code will run only if the Node command line contains a <strong>— seed</strong> argument. An example of such a command would be</p><pre>NODE_ENV=test node src/ --seed</pre><h3>The gory details</h3><p><strong>Configuration:</strong> cli+ generates a configuration module for every seeding NODE_ENV we specified. In our case my-app/config/test.json is created containing</p><pre>{<br>  <strong>&quot;mongodb&quot;</strong>: <strong>&quot;mongodb://no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;mysql&quot;</strong>: <strong>&quot;mysql://root:@no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;nedb&quot;</strong>: <strong>&quot;nedb://no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;postgres&quot;</strong>: <strong>&quot;postgres://postgres:@no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;rethinkdb&quot;</strong>: <strong>&quot;rethinkdb://no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;sqlite&quot;</strong>: <strong>&quot;sqlite://no-connection-string-config-test&quot;</strong>,<br>  <strong>&quot;mssql&quot;</strong>: <strong>&quot;mssql://root:password@no-connection-string-config-test&quot;<br></strong>}</pre><p>When we start our app with <strong>NODE_ENV=test</strong>, because of the way Feathers configuration <a href="https://github.com/lorenwest/node-config">works</a>, these properties will override the connection strings cli+ generates in my-app/config/default.json. This override is the mechanism that’s used to saveguard data we don’t want overridden.</p><p>So you’ll have to manually copy the connection strings existing in default.json to test.json, changing them to point to the databases we’ll seed.</p><p><strong>Scripts:</strong> cli+ adds some convenience scripts for us in package.json.</p><pre>&quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;npm run eslint &amp;&amp; npm run mocha&quot;,<br>    &quot;test:all&quot;: &quot;npm run eslint &amp;&amp; NODE_ENV=test npm run mocha&quot;,<br>    &quot;start&quot;: &quot;node src/&quot;,<br>    &quot;start:seed&quot;: &quot;NODE_ENV=test node src/ --seed&quot;<br>  },</pre><p>Notice <strong>test:all </strong>and <strong>start:seed</strong> both have our seeding NODE_ENV embedded in their scripts. cli+ will change these scripts if we rerun <strong>generate app</strong> and change the NODE_ENV prompt.</p><p><strong>start:seed</strong> starts our app with data seeding. <strong>test:all</strong> runs all the tests, including those which mutate service data.</p><h3>Let’s do it</h3><p>All that detail is nice, but let’s create more of our app and seed our data! We will generate 3 services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*2NL_leWKbplJnW498WF6dg.png" /></figure><p>Let’s create <strong>teams</strong>, and then create <strong>users</strong> and <strong>roles</strong> the same way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*iWCYARpHfa46gfqoaQbvhg.png" /></figure><p>Now we add the JSON-schema for their models, including some decorations on how to fake data for the fields. We also indicate what tables the foreign keys refer to. (The article on <a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">fakes</a> explains all this.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*JVC1JLr93U5ERP5ip_ABIw.png" /><figcaption>DB-agnostic models for our services, using JSON-schema.</figcaption></figure><p>We can now generate our fake data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*awDIF6jK18dbR2gpluGcDQ.png" /></figure><p>and look at it in my-app/seeds/fake-data.json.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*r1mNKt-9Ke9mNdhM8Xz7ug.png" /><figcaption>Fake data generated for NeDB.</figcaption></figure><p>The key fields are named _id and have 6 digit strings. This is because the services were generated for the <a href="https://www.mongodb.com/blog/post/nedb-a-lightweight-javascript-database-using">NeDB</a> adapter.</p><p>cli+ will default to your previous prompts if you rerun <em>feathers-plus generate service</em>. It will make any needed changes to all the modules if you change any of those prompts, such as changing the adapter to Mongoose or Sequelize. The fake data would then generate the correct key fields for the new adapter.</p><p>With our fake data created, we can start our app and seed our services.</p><pre>npm run start:seed</pre><p>And see a log as the seeding occurs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*w9eXSJ6_50gXMpCjLZJjXQ.png" /></figure><h3>In conclusion</h3><p>Most of what we did here we’d be doing anyway while generating our app. Seeding data comes down to</p><ul><li>Answering 2 prompts,</li><li>Customizing config/test.json,</li><li>Running <strong>feathers-plus generate fakes</strong>,</li><li>And starting the application with <strong>npm run start:seed</strong>.</li></ul><p>In return we get</p><ul><li>Good services’ data with which we can immediately start exercising our application,</li><li>Good data we can use in our tests,</li><li>Good infrastructure to guard our production data from accidents.</li></ul><p>You can learn more about @feathers-plus/cli by reading its <a href="https://generator.feathers-plus.com/">docs</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=336302adfe3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">Automatically seeding data with feathers-plus/cli</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatic fake data with feathers-plus/cli]]></title>
            <link>https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8?source=rss----9eaf9206aade---4</link>
            <guid isPermaLink="false">https://medium.com/p/e668b0e16a8</guid>
            <category><![CDATA[feathers-guides]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[feathers-plus]]></category>
            <category><![CDATA[feathersjs]]></category>
            <dc:creator><![CDATA[eddyy stop]]></dc:creator>
            <pubDate>Mon, 13 Aug 2018 20:26:51 GMT</pubDate>
            <atom:updated>2018-10-15T19:46:22.214Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*Gea1OTKglkoqlx_idz2QBQ.png" /><figcaption>feathers-plus/cli can generate fake data, including foreign keys, from your JSON-schema models.</figcaption></figure><p>Feathers-plus/cli (cli+) generates JavaScript or TypeScript code for your application. However there isn’t very much you can do with that code because the services have no data.</p><p>cli+ skirts this issue by providing comprehensive features for generating fake data based on your existing JSON-schema models. This includes generating values for foreign key fields which refer to other generated records.</p><p>This article is part 1 in a set of publications dealing with testing in Feathers.</p><ul><li><a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">Automatic fake data with feathers-plus/cli</a> (this article)</li><li><a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">Automatically seeding data with feathers-plus/cl</a>i</li><li><a href="https://medium.com/feathers-plus/automatic-tests-with-feathers-plus-cli-4844721a29cf">Automatic tests with feathers-plus/cli</a></li><li><a href="https://medium.com/@eddyystop/use-decision-tables-to-write-better-tests-faster-835b18906cf8?sk=d464c55af3b7bb4be795794afaa86247">Use decision tables to write better tests faster.</a></li><li>Automatic auditing of tests with feathers-plus/cli (to come)</li></ul><h3><strong>Generating an app</strong></h3><p>Medium <a href="https://medium.com/@j2L4e/feathers-plus-cli-5c7ba0015e8e">articles</a> have already been <a href="https://medium.com/@mattchewone/graphql-with-feathersjs-4cc67e785bd">published</a> describing how to generate services with cli+, so we will skim through that process here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*SzjGe1_ASBEz_hgl4YmJoQ.png" /></figure><p>Also generate services users and roles in the same way.</p><h3><strong>Faking fields</strong></h3><p>Now let’s fill in the JSON-schema model templates cli+ created for our 3 services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*LIiUSrZAmVdHfiSIzQ3i1Q.png" /><figcaption>Models for the services, in JSON-schema.</figcaption></figure><p>The fakeRecords, faker and chance props are used by the cli+ data faker. They describe the type of fake values it should generate.</p><p>It’s a good idea to describe your fields in as much detail as possible using, for example, the minLength and maxLength JSON-schema props shown above. Not only will cli+ then generate better record validation hooks for you, but <em>the fake data will also respect those props</em>.</p><p>In the above schemas, fakeRecords specifies the number of fake records to create for the service. The default, which you can change, is 5.</p><p>Most of the faker props are self-explanatory. There are about 150 different types of <a href="https://github.com/Marak/Faker.js#api-methods">faker</a> props available. They can be <a href="https://github.com/Marak/Faker.js#localization">localized</a> to about 35 locales so, for example, your fake data can be in Spanish or German.</p><p>The chance prop is also self-explanatory. There are over 100 <a href="https://chancejs.com/basics/bool.html">chance</a> props available.</p><p>You can extend the faker and chance props with your own custom fake value generators. The fk in faker: { fk: &#39;roles:random&#39; } is just such a custom generator added by cli+. It extracts foreign keys from other services.</p><p>We can now generate our fake data with</p><pre>feathers-plus generate fakes</pre><p>and look at the fake data now stored in seeds/fake-data.js.</p><pre>{<br>  &quot;users&quot;: [<br>    {<br>      &quot;email&quot;: &quot;Dortha95@hotmail.com&quot;,<br>      &quot;firstName&quot;: &quot;Lafayette&quot;,<br>      &quot;lastName&quot;: &quot;Doyle&quot;,<br>      &quot;roleId&quot;: &quot;5b71c81d36cb5e0dd2d05db8&quot;,<br>      &quot;password&quot;: &quot;9bd9d8d69a674e0f467cc8796ed151a05dfc2ddf....&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05db2&quot;<br>    },<br>    {<br>      &quot;email&quot;: &quot;Peggie.Farrell@yahoo.com&quot;,<br>      &quot;firstName&quot;: &quot;Jedediah&quot;,<br>      &quot;lastName&quot;: &quot;Baumbach&quot;,<br>      &quot;roleId&quot;: &quot;5b71c81d36cb5e0dd2d05dba&quot;,<br>      &quot;password&quot;: &quot;cb739205929396fea7596eb10faaa2352c595590....&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05db3&quot;<br>    },<br>    {<br>      &quot;email&quot;: &quot;Sarah.Murazik@yahoo.com&quot;,<br>      &quot;firstName&quot;: &quot;Beth&quot;,<br>      &quot;lastName&quot;: &quot;Predovic&quot;,<br>      &quot;roleId&quot;: &quot;5b71c81d36cb5e0dd2d05dba&quot;,<br>      &quot;password&quot;: &quot;aa52c3f5ad019da1ffe78f097b0074f15471b5e6....&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05db4&quot;<br>    },<br>    ...<br>  ],<br>  &quot;roles&quot;: [<br>    {<br>      &quot;name&quot;: &quot;Legacy Marketing Analyst&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05db8&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;Future Brand Representative&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05db9&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;Customer Communications Manager&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05dba&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;Legacy Communications Architect&quot;,<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05dbb&quot;<br>    }<br>  ],<br>  &quot;teams&quot;: [<br>    {<br>      &quot;name&quot;: &quot;sunt in consequat exercitation proident&quot;,<br>      &quot;memberIds&quot;: [<br>        &quot;5b71c81d36cb5e0dd2d05db2&quot;,<br>        &quot;5b71c81d36cb5e0dd2d05db7&quot;,<br>        &quot;5b71c81d36cb5e0dd2d05db6&quot;<br>      ],<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05dbc&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;quis exercitation ullamco tempor&quot;,<br>      &quot;memberIds&quot;: [<br>        &quot;5b71c81d36cb5e0dd2d05db2&quot;,<br>        &quot;5b71c81d36cb5e0dd2d05dba&quot;<br>      ],<br>      &quot;_id&quot;: &quot;5b71c81d36cb5e0dd2d05dbd&quot;<br>    }<br>  ]<br>}</pre><p>Our services were created using Mongoose adapters, so the keys you see are ObjectId() bson values. We would see positive increasing integers if the Sequelize, Knex or feathers-memory adapters were used. The proper key format is used for each service if your set of services use multiple adapters.</p><p>One more thing, cli+ will regenerate your app if you change a service’s adapter. Afterwards run generate fakes or generate all and the fake data will be regenerated properly.</p><h3><strong>Foreign keys</strong></h3><p>You might be happy to know that foreign key support is also built in. The</p><pre>roleId: { type: &#39;ID&#39;, faker: { fk: &#39;roles:random&#39; } }</pre><p>we saw above populates roleId with the key from a randomly selected record from the roles service. The following syntax is supported:</p><ul><li>roles:random:fieldName — A random record is selected from the roles service. The value of its fieldName field is used. fieldName can be in dot notation, e.g. address.city.</li><li>‘roles:random’ — The value of the id or _id field is used.</li><li>‘roles’ — The same as ‘roles:random’.</li></ul><p><strong>Arrays of distinct foreign keys</strong></p><p>The above syntax alone cannot handle several needs we may have. First, we need an array of distinct foreign keys for teams.memberIds.</p><pre>memberIds: {<br>  type: <strong>&#39;array&#39;</strong>,<br>  maxItems: 10,<br>  items: [{ type: <strong>&#39;ID&#39;</strong>, faker: { fk: <strong>&#39;users:next&#39; </strong>} }]<br>}</pre><p>The memberIds field might contain duplicate foreign keys if we were to use faker: { fk: &#39;users&#39; }.</p><p>The list of keys for the users service is shuffled at the start of generation for each teams’ record. The { fk: &#39;users:next&#39; } will use the next one of these shuffled keys. Keys will start to be reused only once all existing keys have been used.</p><p>This ensures the foreign keys in memberIds are distinct.</p><p><strong>Extracting multiple fields from a related record</strong></p><p>We may need to copy the values of several fields from the same related record. For example we may need a result like</p><pre>memberIds: [{ id, firstName }, { id, firstName }, ... ]</pre><p>We could generate this using</p><pre>memberIds: {<br>      type: &#39;array&#39;,<br>      maxItems: 40,<br>      items: [{<br>        type: &#39;object&#39;,<br>        properties: {<br>          id: { type: &#39;ID&#39;, faker: { fk: &#39;users:next&#39; } },<br>          firstName: {<br>            type: &#39;string&#39;,<br>            faker: { fk: &#39;users:curr:firstName&#39; } },<br>        },<br>      }]<br>    }</pre><p>&#39;users:next&#39; selects the record containing the next shuffled key. Then &#39;users:curr:firstName&#39; refers to this current shuffled record, that is, the same one which provided the key.</p><p>You can extract <strong>any number of fields</strong> from a related record using this technique</p><h3><strong>Expressions</strong></h3><p>Sometimes an expression has to be calculated in order to populate a field.</p><pre>dayOfWeek: {<br>  type: &#39;integer&#39;,<br>  faker: { exp: &#39;new Date(&quot;December 25, 2018&quot;).getDay()&#39;}<br>}</pre><p>This exp is another custom prop defined by cli+. It calculates the expression and populates the field with the resulting value.</p><pre>date: { faker: &#39;date.soon&#39; },<br>dayOfWeek: {<br>  type: &#39;integer&#39;,<br>  faker: { exp: &#39;new Date(rec.date).getDay()&#39; }<br>}</pre><p>You can reference other fields in the record within the expression. rec is an object passed to the expression containing the record being populated. You can use it in any valid JavaScript expression.</p><p>The <a href="https://generator.feathers-plus.com/get-started/#expressions">docs</a> contain more information about exp. They explain, for example, how to pass your own custom objects and functions to the expression.</p><h3><strong>In summary</strong></h3><p>cli+ has comprehensive data faking capabilities which are easy to use. They tie in with the record validation hooks which cli+ automatically generates.</p><p>You can find more information about generate fakes in the <a href="https://generator.feathers-plus.com/get-started/#generate-fakes">docs</a>. This includes, for example, information on how to customize the <a href="https://github.com/feathers-plus/generator-feathers-plus/blob/master/generators/writing/templates/_configs/default.js">configuration</a> for the data faker.</p><h3>Next …</h3><p>I’ve written <a href="https://medium.com/feathers-plus/automatically-seeding-data-with-feathers-plus-cli-336302adfe3">a follow-up article</a> on how cli+ can automatically seed your services with fake data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e668b0e16a8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/feathers-plus/fake-data-with-feathers-plus-cli-e668b0e16a8">Automatic fake data with feathers-plus/cli</a> was originally published in <a href="https://medium.com/feathers-plus">Feathers-plus</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>