<?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 Debayan Singha on Medium]]></title>
        <description><![CDATA[Stories by Debayan Singha on Medium]]></description>
        <link>https://medium.com/@deb3007?source=rss-9fae335f475d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*D7OespaDlAk1WvmWDOlNWA.jpeg</url>
            <title>Stories by Debayan Singha on Medium</title>
            <link>https://medium.com/@deb3007?source=rss-9fae335f475d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:32:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deb3007/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[Trigger function in PostgreSQL]]></title>
            <link>https://medium.com/@deb3007/trigger-function-in-postgresql-22e118bb082d?source=rss-9fae335f475d------2</link>
            <guid isPermaLink="false">https://medium.com/p/22e118bb082d</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[updates]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Debayan Singha]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 19:23:09 GMT</pubDate>
            <atom:updated>2019-08-03T19:23:09.244Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fJt6vnnUrWGaY3DkKUnuZQ.jpeg" /></figure><p>Using triggers for database inter-table updates can be really helpful. As almost all tech choices, this is a highly debated topic. Using triggers might hinder the DB operations for certain cases. But also the flip side of the coin is it eases things to a great extent for some cases.</p><p>As I said, it is a highly debated and opinionated topic, my opinion is using triggers for situations which might hardly occur, but occurs will break your code, may be solved with triggers.</p><p>Let me share the way I have done it in my job.</p><p>Defining a trigger is a two-step job:</p><ol><li>Define a Postgres function, which will hold the logic of whether or not update is required, and if required what and how to update.</li><li>Defining a trigger which will call your declared function on a change.</li></ol><p><strong>Creating a trigger function</strong></p><p>If u need to know about defining a Postgres function, you can check <a href="http://www.postgresqltutorial.com/postgresql-create-function">this link</a> out.</p><p>A trigger function is just another Postgres function, just that it doesn’t accept any parameters and returns in type <strong>trigger.</strong></p><p>First, lets set up two simple tables, to try out a trigger. One can use the following commands to set the table up.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d06c8a086fd4d81dcd108a9efa3c30c/href">https://medium.com/media/4d06c8a086fd4d81dcd108a9efa3c30c/href</a></iframe><p>As we see, the arrangement is such that we have a <em>customers</em> table and corresponding <em>entries</em> table. The <em>customers</em> table holds customer data, and entries keep records of entries which are linked to one of the customers in the <em>customers</em> table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*2468Z3njlXXpChVvoC-idg.png" /><figcaption>customers table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*j_iLlwAKy3696DgUCqcAPw.png" /><figcaption>entries table</figcaption></figure><p>For the sake of the example, I have included the customer name field in <em>entries</em><strong> </strong>table which is very uncharacteristic of an industrial application except for unconventional necessities. Anyways, in an off chance that it happens, the name has to be updated in the entries table if a customer data is updated or altered. So, we will add a trigger to solve that (artificial) problem of ours.</p><p>As is stated above, about adding a trigger being a two-step job. We will take the first step to do it viz. making a trigger function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a31bbf76d0bda61e8ca44d6efbf527da/href">https://medium.com/media/a31bbf76d0bda61e8ca44d6efbf527da/href</a></iframe><p>This function just checks whether there is any change in the name of a record in the <em>customer</em> table and if there had been it runs an update command in the <em>entries</em> table.</p><p>Now, the consequent step to attach this function as a trigger to the <em>customers</em> table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e608ba9fbd0a1bcb1d91c2ec8f5ff00/href">https://medium.com/media/9e608ba9fbd0a1bcb1d91c2ec8f5ff00/href</a></iframe><p>Now, the expectation is that if I change the name of any of the customers in the <em>customers </em>table, it should reflect in all other entries of that particular customer.</p><p>Let’s alter one of the customer names and check with the <em>entries</em> table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5eb22d44835877a9caff0c23f2283a7/href">https://medium.com/media/b5eb22d44835877a9caff0c23f2283a7/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*O0YNW-kVxYHX4SfFZb4gNA.png" /><figcaption>cutomers table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*txYv9wOEWkG81XOGdZ7jhw.png" /><figcaption>entries</figcaption></figure><p>This example is one of high simplicity and the table structures are simple.</p><p>Also, trigger function can also be having a part of business logic apart from straightforward DB updates. Although this a highly debated implementation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22e118bb082d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Perks of using Lodash]]></title>
            <link>https://medium.com/@deb3007/perks-of-using-lodash-1914456c21d3?source=rss-9fae335f475d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1914456c21d3</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[lodash]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Debayan Singha]]></dc:creator>
            <pubDate>Tue, 16 Jul 2019 20:04:46 GMT</pubDate>
            <atom:updated>2019-07-16T20:08:19.300Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KdRJJctCmv2wJEYA1YeLtg.jpeg" /></figure><p>We often make a function for the pieces of code we are using throughout a script or an entire codebase. Not doing that will be a violation of the <a href="https://medium.com/infancyit/kiss-keep-it-simple-smart-javascript-habits-b019ce14b5ec">KISS</a> principle, in other words, it will be downright stupid.</p><p><strong>Let it make your functions for you</strong></p><p>Let us look at a particular case where we would need to build a function, where we are using a string literal in multiple places, e.g. defining some configuration object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef4ad7cb6427bf20311883e6a8f8a993/href">https://medium.com/media/ef4ad7cb6427bf20311883e6a8f8a993/href</a></iframe><p>Here, the same strings are used again and again with minor changes. So in such a case where we are using too much of this particular string pattern throughout the codebase, we might find highly utilisable to define a function which could return the strings by taking inputs of the customisable parts.</p><p>This is where Lodash comes in. Instead of creating our own specific function to create a particular string, which might contain its own share of errors and limitations, we can be using _.template. Here’s how:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fa3bc2170b61982bd2533d77ca61b3f/href">https://medium.com/media/1fa3bc2170b61982bd2533d77ca61b3f/href</a></iframe><p>_.template returns the function that you need. So, it is creating the function you need for you.</p><p><strong>Let it handle the intricacies</strong></p><p>Now let us look at another aspect of using Lodash, which has equal potential to make our lives easier.</p><p>Consider, you have an array of arrays. And you need to work with each element of the array in whichever depth it is in. This is the way we could go about using pure js.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5a4f5cbb6c8c434651e8018cef31414/href">https://medium.com/media/f5a4f5cbb6c8c434651e8018cef31414/href</a></iframe><p>But, we just missed elements, which are in a level deeper. So let us rectify ourselves and flatten it further.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ca61ec7738a6f2eadfac39d24d7fe7c/href">https://medium.com/media/1ca61ec7738a6f2eadfac39d24d7fe7c/href</a></iframe><p>There we go, problem solved. But, is it? What if we have three levels deeper, or even more. Well, then we need to write a function which can handle whichever level of depth and return a one-dimensional array. Needless to say, our function will have its own share of limitations, bugs, edge cases, and all of their friends to torture us.</p><p>Lets attempt to solve our problem using _.flattenDeep.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9138663bb8a9f91f0ab009568419a249/href">https://medium.com/media/9138663bb8a9f91f0ab009568419a249/href</a></iframe><p>Boom. Done.</p><p><strong>Priority sorting</strong></p><p>If we have an array of objects, and we need to sort it in a priority manner it is a hassle doing in pure JS. You can check <a href="https://www.sitepoint.com/sophisticated-sorting-in-javascript/">here</a> to have a sense of it.</p><p>Anyways, here’s how we do it using lodash.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58649ab50bd0b83946f66d92116bd14f/href">https://medium.com/media/58649ab50bd0b83946f66d92116bd14f/href</a></iframe><p>Here, I have done cherry-picking while picking up the examples, but the possibilities and scope are immense. It will always help to use lodash in the venture of having cleaner code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1914456c21d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>