<?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 Raphael Feliciano on Medium]]></title>
        <description><![CDATA[Stories by Raphael Feliciano on Medium]]></description>
        <link>https://medium.com/@rdf014?source=rss-50b45955ad80------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hxwzF_NXrrn1LOj2.</url>
            <title>Stories by Raphael Feliciano on Medium</title>
            <link>https://medium.com/@rdf014?source=rss-50b45955ad80------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:12:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rdf014/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[How To Use Promises With Sequelize]]></title>
            <link>https://medium.com/@rdf014/how-to-use-promises-with-sequelize-dd82c26bb2a?source=rss-50b45955ad80------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd82c26bb2a</guid>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Raphael Feliciano]]></dc:creator>
            <pubDate>Wed, 03 May 2017 01:53:10 GMT</pubDate>
            <atom:updated>2017-05-03T01:53:10.529Z</atom:updated>
            <content:encoded><![CDATA[<p>Anyone who is familiar with backend programming knows too well about callback hell. Code can easily become messy and unreadable. This post aims to provide a simple “how to” in using promises in your backend code. Specifically in this article, I am using the bluebird library for promises, using Node/Express server and Sequelize as the ORM for making queries easier.</p><p>Why use promises? Promises helps code become more readable but it also makes error handling much easier. Let’s start with a simple example of the “.catch()” method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd0e8f72726d57b69b131de98f051ee8/href">https://medium.com/media/cd0e8f72726d57b69b131de98f051ee8/href</a></iframe><p>Brandon Eich, JavaScript creator, built the language to “throw” errors when code is executed. As engineers we want to model this behavior by having our own programs throw errors specific to the issues one might encounter while using our apps. I will explain more on this later.</p><p>So, how to use promises? When using promises, there are a couple things to remember. Whatever value your promise function returns, the value will then be injected as an argument into the next “.then()” function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b103a250e6f5b02642facca3eb0e32fc/href">https://medium.com/media/b103a250e6f5b02642facca3eb0e32fc/href</a></iframe><p>Also, you should avoid promise antipatterns; you can learn more about them <a href="http://www.datchley.name/promise-patterns-anti-patterns/">here</a>. However, the main anti pattern I want to emphasize to avoid is to write nested “.then()” functions; nested “.then()” functions start to look more like callbacks, which is what we want to avoid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/026d8de12705a3307fb10a7367fc13d1/href">https://medium.com/media/026d8de12705a3307fb10a7367fc13d1/href</a></iframe><p>Last but not least, when writing a “.catch()” function, whatever is thrown, whether it is an error JavaScript throws or a custom expression that you write, it will skip any “.then()” functions in between and go straight into the “.catch()” function.</p><p>For some basic information on ORM, Sequelize. All the functions are promises, so you can write then using all the rules of promises. The functions in Sequelize will not return a simple object; it will instead return an instance. The instance in itself will have several methods you can invoke on it; the two main methods I will focus on is “.get()” and “.update()”. “.get()” will simply return all the values of the row as an object. “.update()” will update the table row that the query returns with whatever column you want to update.</p><p>Ok, now that the intro is done, let’s start some examples. First, lets create a simple schema design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/0*dvPoExGZCyfjFscX." /></figure><p>The Parents and the Child table are basically the same except the parent has a type property that basically shows if they are the “Father”, “Mother”, “Step-Mother”, or “Step-Father”. Then you have a simple join table where there is a many-to-many relationship.</p><p>Now, going forward, I going to assume that you know how to setup your database using Sequelize and know about how a basic model works.</p><p>Let’s assume that in our database we have these data sets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e31384c8de8c621f70f685a900a4f51/href">https://medium.com/media/0e31384c8de8c621f70f685a900a4f51/href</a></iframe><p>Now, if you want to do a query for a child named ‘Joe’, you would write:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5179dc889c4c3f7b3ebfc673e52710c/href">https://medium.com/media/e5179dc889c4c3f7b3ebfc673e52710c/href</a></iframe><p>Fairly simple, the argument on line 2, is the result from the query on line 1. In order to get the data from the instance, you must invoke “.get()” function on the instance, see line 3. Since I am returning the statement on line 3, the result will then be passed into the argument on line 5. ChildData will then be equal to what is in the commented out code.</p><p>Now, if you want to find all the parent’s information for the child ‘Joe’, you would write:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6dc5e6250bad2f5630cc9017c979c7c/href">https://medium.com/media/a6dc5e6250bad2f5630cc9017c979c7c/href</a></iframe><p>If you have looked around the Sequelize documentation, you might have found that there is a property you can can use with the queries called “include”. Basically you would use it when you are querying join tables, and and want to include the data from a foreign key. I opted not to use that because it did not give me consistent results. So my work around was the above code. It is definitely long but, I think, very readable. You basically chain “.find()” functions and use the data from that query, to query other tables.</p><ol><li>Find child’s id</li><li>Find instance of child’s id in parent_child table</li><li>Find parent data where id is equal to parent_id from parent_child instance</li></ol><p>Let’s say now you want to update John in the Parents table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7b2d1fa88abf1b7d0df2fe1f2a4c650/href">https://medium.com/media/f7b2d1fa88abf1b7d0df2fe1f2a4c650/href</a></iframe><p>You must remember to invoke “.update()” function on the the instance and not the raw data, look at line 3. Also important to know that when you return the update function, the new updated instance will be used as the argument for the next “.then()” function, see line 5. Invoking the “.get()” function on the new updated instance will then yield you the new updated raw data, see commented out code.</p><p>Now how to use a custom “.catch()”. Let say you want to do a query to the Parents table, and want to check the type of a parent, and you want to do something if the type is “Mother”, but something else for all others.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccc7b4184010412a705a7af4c6e9395b/href">https://medium.com/media/ccc7b4184010412a705a7af4c6e9395b/href</a></iframe><p>If you look at line 7, what it is saying is that the the query did not find anything, it will return null. If motherData is equal null, the throw function will skip the “.then()” block and go straight into the “.catch()” function. You can think of the function transforming when, the query returns null.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/beca48331de283a0fe0a836a63f9f3b3/href">https://medium.com/media/beca48331de283a0fe0a836a63f9f3b3/href</a></iframe><p>One important thing to remember about the “.catch()” function is that it will catch ALL throw expressions. If there are any errors in your code, the catch function will receive the error and could potentially throw another error. So be careful when using “.catch()” function in this manner.</p><p>So with that said, that was a brief overview on how to use Sequelize with promises. If you look at the bluebird promises docs, there are a lot more powerful functions that I haven’t mention that I highly recommend learning; specifically Promise.map and Promise.all. Thank you for reading. If you have any comments or criticisms, please feel free to leave a comment below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd82c26bb2a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>