<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dave Fulton</title>
    <description>The latest articles on DEV Community by Dave Fulton (@thedavefulton).</description>
    <link>https://dev.to/thedavefulton</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F133248%2Fe3226e12-c07f-43c4-8440-ca756017d4be.png</url>
      <title>DEV Community: Dave Fulton</title>
      <link>https://dev.to/thedavefulton</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thedavefulton"/>
    <language>en</language>
    <item>
      <title>Node.js Starter Project for Advent of Code</title>
      <dc:creator>Dave Fulton</dc:creator>
      <pubDate>Tue, 24 Nov 2020 01:09:05 +0000</pubDate>
      <link>https://dev.to/thedavefulton/node-js-starter-project-for-advent-of-code-1l6h</link>
      <guid>https://dev.to/thedavefulton/node-js-starter-project-for-advent-of-code-1l6h</guid>
      <description>&lt;h2&gt;
  
  
  Hello!
&lt;/h2&gt;

&lt;p&gt;It's getting to be that time of year when the annual &lt;a href="https://adventofcode.com"&gt;Advent of Code&lt;/a&gt; kicks off. I've taken a swing at it the last couple of years but always fell off after a few days. One of the biggest reasons was having to setup a new 'project' every day. Having to take the time to copy things over and tweak the config was often &lt;em&gt;just&lt;/em&gt; enough to keep me from keeping on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;So this year I've put together a &lt;a href="https://github.com/thedavefulton/create-node-cli"&gt;package&lt;/a&gt; to install a starter project with minimal effort and I've made it available to everyone through &lt;a href="https://www.npmjs.com/package/@thedavefulton/create-node-cli"&gt;NPM&lt;/a&gt;. Now I'm not going to claim that this is the &lt;em&gt;best&lt;/em&gt; in the world. In fact it's still a &lt;em&gt;bit&lt;/em&gt; rough.&lt;/p&gt;

&lt;p&gt;BUT.&lt;/p&gt;

&lt;p&gt;I'll be &lt;a href="https://en.wikipedia.org/wiki/Eating_your_own_dog_food"&gt;dogfooding&lt;/a&gt; it for the rest of the month while I work through some of the previous years puzzles. Anything I find that makes getting up and running with the least possible effort will be added to the package.&lt;/p&gt;

&lt;p&gt;I'm also hoping that some folks may find it useful enough to make some suggestions to improve it 🙏&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://github.com/thedavefulton/create-node-cli/blob/main/README.md"&gt;README.md&lt;/a&gt; should give a pretty clear idea of how to use the package. Of special note: I've made sure you can run it using the &lt;code&gt;npx&lt;/code&gt; command. For those who might not be familiar, &lt;code&gt;npx&lt;/code&gt; allows you to run an &lt;code&gt;npm&lt;/code&gt; command without actually installing it. It's pretty slick 🙌&lt;/p&gt;

&lt;p&gt;For example, you can just run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx @thedavefulton/create-node-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the package will work it's magic without any extra work on your part. It really is one of my favorite things about the Node ecosystem!&lt;/p&gt;

&lt;p&gt;Sorry to gush 😊&lt;/p&gt;

&lt;p&gt;I've also added a flag for all of the options so that you can setup an alias in your terminal's configuration. So rather than typing it all out every day or going through the wizard, you could just setup something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alias newcli='npx @thedavefulton/create-node-cli ts --name "Dave Fulton" --project '
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then everyday you just type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newcli my-new-project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you're off 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;p&gt;So you're still not convinced that this is the &lt;em&gt;one thing&lt;/em&gt; that could make 2020 not a &lt;em&gt;complete&lt;/em&gt; dumpster fire?&lt;/p&gt;

&lt;p&gt;Well, you're probably right 😢&lt;/p&gt;

&lt;p&gt;But there are a few other things that I think make this pretty useful. I've included a few packages that will make your life a little easier and hopefully help you focus on solving the various daily challenges.&lt;/p&gt;

&lt;p&gt;There's &lt;a href="https://lodash.com/docs/4.17.15"&gt;Lodash&lt;/a&gt;, a really great utility library. I know some of you may frown on it, and there is certainly a lot more that can be done in vanilla JavaScript now. But &lt;em&gt;I&lt;/em&gt; still find a bunch of their functions to be helpful ¯_(ツ)_/¯&lt;/p&gt;

&lt;p&gt;I've also included a basic testing setup using &lt;a href="https://jestjs.io/docs/en/using-matchers"&gt;Jest&lt;/a&gt;. I really like having this around for AoC. They usually offer a bunch of examples for their problems and writing a couple of simple unit tests using their data can really clarify my thinking 🤔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://prettier.io/docs/en/index.html"&gt;Prettier&lt;/a&gt; is there because I like to not worry about how my code is formatted and styled and whatnot&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/chalk/chalk/blob/master/readme.md"&gt;Chalk&lt;/a&gt; is there because I like to worry about how my output is formatted and styled and whatnot.&lt;/p&gt;

&lt;p&gt;I'm also keeping an eye on adding utility functions when they seem appropriate. For instance, there's a function now that will read a file and give you back an array of lines. Nothing fancy, but I hate having to write that stuff over and over.&lt;/p&gt;

&lt;h2&gt;
  
  
  In Closing
&lt;/h2&gt;

&lt;p&gt;I'm really excited to get started on the Advent of Code this year. If you are too and you're planning to use JavaScript or TypeScript I hope you'll consider checking out my project. If you're newish (or lazyish 😈) I think you'll find it can really take out a lot of the boilerplate busywork and let you focus on the challenges you &lt;em&gt;actually&lt;/em&gt; care about 🏋️&lt;/p&gt;

&lt;p&gt;I'd also love you hear your &lt;a href="https://github.com/thedavefulton/create-node-cli/issues"&gt;thoughts&lt;/a&gt; and &lt;a href="https://github.com/thedavefulton/create-node-cli/pulls"&gt;suggestions&lt;/a&gt; for improving it! I'm sure there are a bunch of things I haven't thought of that could make your life easier. But more importantly: they could also make &lt;em&gt;my&lt;/em&gt; life easier 😃&lt;/p&gt;

&lt;p&gt;Be well 🖖&lt;/p&gt;

</description>
      <category>adventofcode</category>
      <category>node</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Creating Multiple Model Objects at Once</title>
      <dc:creator>Dave Fulton</dc:creator>
      <pubDate>Tue, 16 Apr 2019 23:00:57 +0000</pubDate>
      <link>https://dev.to/thedavefulton/creating-multiple-model-objects-at-once-3d97</link>
      <guid>https://dev.to/thedavefulton/creating-multiple-model-objects-at-once-3d97</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclaimer - I'm still fairly new to Laravel, so I'll readily admit that there may be other/better ways of doing this, or that this is already well known.  But a bit of Googling seemed to indicate otherwise&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So today I was presented with a bit of a challenge.  I have a parent object that I'll creatively call "App\Parent" which has a bunch of children I'll call, um, "App\Child"?&lt;/p&gt;

&lt;p&gt;Yeah, that works.&lt;/p&gt;

&lt;p&gt;So the children are basically just a bunch of filenames, and the POST request already has them put into an array, so I'd like to just stick that array in some kind of &lt;code&gt;Model::Create()&lt;/code&gt; method, but &lt;code&gt;Create()&lt;/code&gt; seems to prefer just one set of attributes, thank you very much.&lt;/p&gt;

&lt;p&gt;Some looking around found a solution in using the &lt;code&gt;Insert()&lt;/code&gt; method.  This would allow me to insert as many records as I wanted but apparently wouldn't set the timestamps.  And I don't want to do that manually.  Or rather, I don't want to waste the server's time doing it manually.&lt;/p&gt;

&lt;p&gt;And that's when I stumbled onto the solution.  If you have a HasMany relationship, you can use the &lt;code&gt;CreateMany()&lt;/code&gt; method on &lt;em&gt;that&lt;/em&gt;.  Let me give you an example.  Say I have a couple of basic Models that are related with a HasMany/BelongsTo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;children&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;hasMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'App\Child'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'App\Parent'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I can leverage that relationship to create all the children I want in one shot.  Let's say I get a request with &lt;code&gt;'parentName'&lt;/code&gt; and an array of &lt;code&gt;'childNames'&lt;/code&gt;.  I can handle it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParentController&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$parentName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'parentName'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$childNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'childNames'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Remember, $childNames is already an array&lt;/span&gt;

        &lt;span class="nv"&gt;$parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;Parent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'parent_name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$parentName&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="nv"&gt;$parent&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;createMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$childNames&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it.  And a little bonus feature is that you don't even have to pass in the &lt;code&gt;'parent_id'&lt;/code&gt; as an attribute; it's handled automatically.  As I said before, this may all be common knowledge, but if it's not, I want this to at least be easier to find for the next person!&lt;/p&gt;

&lt;p&gt;Happy Hacking!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>eloquent</category>
    </item>
  </channel>
</rss>
