<?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[Stories by Sergio Almécija on Medium]]></title>
        <description><![CDATA[Stories by Sergio Almécija on Medium]]></description>
        <link>https://medium.com/@sheniff?source=rss-588210165075------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*lq0cNiMVO2Ly2Wq6.jpeg</url>
            <title>Stories by Sergio Almécija on Medium</title>
            <link>https://medium.com/@sheniff?source=rss-588210165075------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:43:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sheniff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[requiredIf: conditional validator for Angular reactive forms]]></title>
            <link>https://medium.com/@sheniff/requiredif-conditional-validator-for-angular-reactive-forms-95c8d6e82524?source=rss-588210165075------2</link>
            <guid isPermaLink="false">https://medium.com/p/95c8d6e82524</guid>
            <category><![CDATA[angular-4]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Sergio Almécija]]></dc:creator>
            <pubDate>Fri, 21 Jun 2019 21:26:03 GMT</pubDate>
            <atom:updated>2019-06-21T21:26:03.575Z</atom:updated>
            <content:encoded><![CDATA[<p>Tl;dr, use this validator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b07bc76344752716c1fedae7431d93d/href">https://medium.com/media/4b07bc76344752716c1fedae7431d93d/href</a></iframe><p>Sometimes, we need to validate a form based on certain premises that might depend on other field selection on the same form.</p><p>For example, let’s say we have a form that requires your car’s license plate only if you mark the option of commuteByCar.</p><pre>getCommuteFormValidator() {<br>  return this.FormBuilder.group({<br>    name: [&#39;&#39;, [Validators.required]],<br>    commuteByCar: [false, [Validators.required]],<br><strong>    licensePlate: [&#39;&#39;, [???]],</strong><br>  });<br>}</pre><p>How to conditionally require licensePlate only if commuteByCar = true?</p><p>Here’s a very simple validator that solves the problem and gives you access to the form <strong>control’s parent</strong>, from which we get access to other fields, if needed (we can just ignore it for simpler conditions).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b07bc76344752716c1fedae7431d93d/href">https://medium.com/media/4b07bc76344752716c1fedae7431d93d/href</a></iframe><p>Then, it can be easily set up like:</p><pre>const requireIfCommuteByCar = <strong>requiredIf</strong>((control: FormGroup) =&gt; {<br>  <br>  if(!control) {<br>    return false; // &#39;parent&#39; might not be ready while form loads...<br>  }</pre><pre>  return control.get(&#39;commuteByCar&#39;).value == true;<br>});</pre><pre>...<br><br>licensePlate: [&#39;&#39;, [requireIfCommuteByCar]],</pre><p>Hope it helps anyone! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95c8d6e82524" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Next time you (aka, me) end up messing up with git rebase…]]></title>
            <link>https://medium.com/@sheniff/next-time-you-aka-me-end-up-messing-up-with-git-rebase-da4bb30d1447?source=rss-588210165075------2</link>
            <guid isPermaLink="false">https://medium.com/p/da4bb30d1447</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[rebase]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Sergio Almécija]]></dc:creator>
            <pubDate>Thu, 12 Jan 2017 01:06:38 GMT</pubDate>
            <atom:updated>2017-01-12T01:06:38.441Z</atom:updated>
            <content:encoded><![CDATA[<p>I was about to wrap up a bunch of work that took me a real while to have ready when I made a stupid mistake, when rebasing my multiple commits into a single, nicer one with</p><pre><strong>git rebase -i master</strong> # or whatever commit you&#39;re rebasing from</pre><p>I ended up removing one commit line from the editor during rebase. I decided to go on with the rebase as I (stupidly) guessed if the commit is not in the list, it’ll be just left in peace...</p><h4>But what’s actually what happens?</h4><p>Such commit will be totally wiped out of your now rebased code. As if you did a cherry-pick of the rest of commits, skipping that one…</p><h4>Panic.</h4><p>Ok, all my work (that was a pretty dense commit) from the last few hours to the trash in a glimpse!! Thankfully I found this article <a href="http://blog.screensteps.com/recovering-from-a-disastrous-git-rebase-mistake">http://blog.screensteps.com/recovering-from-a-disastrous-git-rebase-mistake</a> (thanks so much <a href="http://blog.screensteps.com/author/greg-devore">Greg DeVore</a> for it!).</p><h4>Quick solution: Git has your ass covered (most of the time)</h4><p>Inside your project, look for</p><pre>less .git/logs/refs/heads/&lt;your branch name&gt;</pre><p>There, you can see something like</p><pre>000... 502... Sergio 1484097809  branch: Created from HEAD<br>... # moar commits</pre><pre>91a... aa7... Sergio 1484166834  commit: fix de...<br><strong><em>aa7... 2e1... Sergio 1484169896  commit: use t...</em></strong> #(1)<br>... # moar commits</pre><pre><strong><em>6d6... 96d... Sergio 1484180670  rebase -i (finish):...</em></strong> #(2)</pre><p>Where (1) is the commit I mistakenly removed while rebasing and (2) is where I committed the sin…</p><h4>Phew…!</h4><p>Well, at least here we are all the info we need! Note that in every line (e.g, in (1)):</p><pre><strong>aa7...</strong> (&lt;- this is the parent hash) <strong>2e1...</strong> (&lt;- this is the commit hash)</pre><p>So now we can create a new branch and save ourselves from burning in hell (or sitting at the office, for a few extra hours…)</p><pre>git checkout -b myWorkRecovery <strong><em>6d6...</em></strong></pre><p>Where 6d6... is the hash of the commit that’s parent to the one where we messed it up.</p><p>Thanks GIT for scaring me to death and then saving me from stupidity! xDD</p><p>Hope it helps anyone else, including future me!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da4bb30d1447" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using shell to rename files in bulk]]></title>
            <link>https://medium.com/@sheniff/using-shell-to-rename-files-in-bulk-ee657f1a06?source=rss-588210165075------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee657f1a06</guid>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[bulk]]></category>
            <dc:creator><![CDATA[Sergio Almécija]]></dc:creator>
            <pubDate>Fri, 17 Jun 2016 01:30:45 GMT</pubDate>
            <atom:updated>2016-06-17T01:30:45.725Z</atom:updated>
            <content:encoded><![CDATA[<p>I found myself refactoring a UI app from plain JavaScript to TypeScript which, to begin with (and given that TypeScript is a superset of JavaScript), would just mean renaming all my js files and to use .ts extension instead of .js to be understood by TypeScript compiler.</p><p>Such a mess to do manually… much better to do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ef9767c30a4b863135956df93e8d5b3/href">https://medium.com/media/5ef9767c30a4b863135956df93e8d5b3/href</a></iframe><p>Tadaa!! save it to, say, “<strong>rename.sh</strong>” and run it with</p><blockquote><strong>$&gt; bash rename.sh</strong></blockquote><p>Hope it helps! It did for me!</p><p>As usual, thanks to <a href="http://stackoverflow.com/a/1225236">http://stackoverflow.com/a/1225236</a> for being always so damn helpful :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee657f1a06" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing ExpressJS API + Waterline ORM]]></title>
            <link>https://medium.com/@sheniff/testing-expressjs-api-waterline-orm-4c2996e020ec?source=rss-588210165075------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c2996e020ec</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[orm]]></category>
            <dc:creator><![CDATA[Sergio Almécija]]></dc:creator>
            <pubDate>Tue, 25 Aug 2015 20:29:12 GMT</pubDate>
            <atom:updated>2015-08-25T20:29:12.700Z</atom:updated>
            <content:encoded><![CDATA[<p>Following my previous post, I found myself integrating Waterline ORM with ExpressJS in a PENA stack (P stands for Postgres, should I standardize this…?).</p><p>In this process, I hit a kinda strong wall (ouch, I guess?): <strong>I need Waterline to bootstrap the models before running the tests</strong> if I want to test them, as Mongoose used to do before I moved to this new ORM.</p><p>For this, given that NodeJS follows a one-time instantiation strategy for the dependency injection, it should’ve been as easy as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad00b54b9a248d345108fd606c466e51/href">https://medium.com/media/ad00b54b9a248d345108fd606c466e51/href</a></iframe><p>This way, app will be initialized first time it’s called from one of the tests and it’ll be just used afterwards, allowing us to access to all of the models at will. There’s a complication here, though. Looking back at our app.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a292e3bbf186714175c535e63f09318b/href">https://medium.com/media/a292e3bbf186714175c535e63f09318b/href</a></iframe><p>Yep… Waterline initialization is asynchronous. Hence, tests will try to run without waiting for the ORM to be ready and we’ll get a nice error stating <strong><em>app.models does not exist</em></strong> in our tests.</p><h4>Waiting for Waterline to run tests</h4><p>The solution is pretty straight forward: Promises. Let’s make waterline notify when initialization is ready and tests wait for it to happen:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/547a6a4fd8ec470023cea770b6a6234e/href">https://medium.com/media/547a6a4fd8ec470023cea770b6a6234e/href</a></iframe><p>Now in our tests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06eab1ca11640f36652729417c9eaa96/href">https://medium.com/media/06eab1ca11640f36652729417c9eaa96/href</a></iframe><p>That snippet will run before tests just <strong>once</strong> and we’ll be ready to use app.models as much as we want along the whole test file :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c2996e020ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Waterline ORM with ExpressJS in MEAN project]]></title>
            <link>https://medium.com/@sheniff/using-waterline-orm-with-expressjs-in-mean-project-5e1bda988e24?source=rss-588210165075------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e1bda988e24</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[orm]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sergio Almécija]]></dc:creator>
            <pubDate>Tue, 25 Aug 2015 20:23:23 GMT</pubDate>
            <atom:updated>2015-08-25T20:23:23.599Z</atom:updated>
            <content:encoded><![CDATA[<p>I was recently working on a side project of mine where, in an attempt to save some time, I opted for a <a href="http://mean.io/">MEAN stack</a> using a wonderful code generator, named <a href="https://github.com/DaftMonk/generator-angular-fullstack">generator-angular-fullstack</a> (from now on, GAF), which crazily speeds up the process of prototyping the app.</p><p>Unfortunately, due to project’s specific needs, I found myself needing to leave MongoDB on behalf of a relational DB system, in my case I decided to use PostgreSQL (which converts MEAN stack in… PENA?). But to keep enough flexibility in case I wanted to move to a different DB at some point again, I decided to try a pretty established ORM for Express, <a href="https://github.com/balderdashy/waterline">Waterline</a>.</p><h4>Porting code from Mongoose to Waterline</h4><p>I’m happy the project wasn’t very advanced when I took this path. Still, around 10 models were already created using Mongoose and the whole of the application relied on this MongoDB’s ODM. Also, my main goal was to make <strong>as few changes as possible</strong> with regards to the automatically generated code, so that I could keep on using <a href="https://github.com/DaftMonk/generator-angular-fullstack">GAF</a> to build my app.</p><p>This was the structure of the server project so far (the exact one that is generated by GAF):</p><pre>MyProject<br>  — …<br>  — server<br>    — …<br>    — api<br>      # entity1<br>        + entity1.controller.js<br>        + entity1.model.js<br>        + entity1.spec.js<br>        + index.js <em>(entity’s API routes)</em><br>      # entity2<br>        + …</pre><p>Which turned out to be a pretty convenient structure that allows to keep modularity with every API endpoint in an independent folder so that it can be extended easily.</p><h4>But Waterline needs to know…</h4><p>However, this complicates things for Waterline, as this ORM requires you to explicitly load all the models during app init, but such info is in many different files (all the <strong><em>.model.js</em></strong> files) to keep things modular.</p><p>To solve this problem, I got inspired by <a href="http://ganeshdatta.com/2015/03/01/waterline-orm-in-express-the-correct-way/">Ganesh’s cool article</a> to integrate Waterline and Express. And created this loader as <strong><em>MyProject/server/api/index.js</em></strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/489c18cd0db3b9d9cb948bd83995d69d/href">https://medium.com/media/489c18cd0db3b9d9cb948bd83995d69d/href</a></iframe><p>This way, our app will automatically fetch all the files with the extension <strong><em>.model.js</em></strong> and will load the collection in them.</p><p>In my case (again because <em>I wanted to reduce the number of changes</em> as much as possible, I leveraged environments’ config files to store waterline’s settings.</p><h4>How models should look like now?</h4><p>Well, a few changes are needed to make models to be understood by Waterline vs old Mongoose, here’s an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e86080c530ebff70bab0d1c289baa0b/href">https://medium.com/media/0e86080c530ebff70bab0d1c289baa0b/href</a></iframe><p>The only royal pain is having to include <em>identity</em> and <em>connection</em> attributes to every single model (but I guess I can live with it :).</p><h4><strong>Starting up the DB</strong></h4><p>Finally the only step left is initializing the DB in app.js for express to have access to data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0302236bf0ef59259bb662df77614f33/href">https://medium.com/media/0302236bf0ef59259bb662df77614f33/href</a></iframe><p>I opted for creating a global variable <strong>Models</strong>, to be able to access every model from controllers easily. If global variables gives you the creeps, you still can just <strong><em>require(‘./app’);</em></strong> from anywhere in the app and use <strong>app.models</strong> to access the ORM methods.</p><p>Unlike Mongoose, Waterline initialization is asynchronous, which requires you to wait for it to be ready before running the server.</p><p>This same asynchrony gave me a lot of headaches at the time of testing the API, but that will be cover in a future post…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e1bda988e24" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>