{"id":1041,"date":"2021-03-24T11:05:27","date_gmt":"2021-03-24T18:05:27","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=1041"},"modified":"2021-03-24T11:05:27","modified_gmt":"2021-03-24T18:05:27","slug":"dev-life-is-easy-with-node-mssql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/dev-life-is-easy-with-node-mssql\/","title":{"rendered":"Dev life is easy with node-mssql"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-1044\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245.jpg\" alt=\"Image pexels jonathan borba 3255245\" width=\"1600\" height=\"971\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245.jpg 1600w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245-300x182.jpg 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245-1024x621.jpg 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245-768x466.jpg 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2021\/03\/pexels-jonathan-borba-3255245-1536x932.jpg 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>As mentioned already in a <a href=\"https:\/\/dev.to\/azure\/promises-node-tedious-azure-sql-oh-my-ho1\">previous posts<\/a>, I&#8217;ve just started learning Node in the last months. At first I had a rough start as the totally asynchronous nature of Node, and the many ways it can be leveraged, wasn&#8217;t something I was used to. I battled a bit with that, learned a lot, and also figured out how to properly use <a href=\"https:\/\/tediousjs.github.io\/tedious\/\">Tedious<\/a> to take advantage of Azure SQL in my projects.<\/p>\n<p>But using Tedious is&#8230;<del>tedious<\/del> verbose. Also the way it manages all asynchronous calls is quite different than the modern async\/await pattern.<\/p>\n<p>So I looked for something different and more modern and I found the <a href=\"https:\/\/www.npmjs.com\/package\/mssql\">node-mssql<\/a> package that does exactly what I needed. Taking advantage of Azure SQL is really a breeze now. Exposing a stored procedure as a REST endpoint is now as easy as writing something like (the stored procedure returns data a JSON):<\/p>\n<pre class=\"prettyprint\">const sql = require('mssql')\r\n\r\nconst AZURE_CONN_STRING = process.env[\"AzureSQLConnectionString\"];\r\n\r\nmodule.exports = async function (context, req) {    \r\n    const pool = await sql.connect(AZURE_CONN_STRING);    \r\n\r\n    const busData = await pool.request()\r\n        .input(\"routeId\", sql.Int, parseInt(req.query.rid))\r\n        .input(\"geofenceId\", sql.Int, parseInt(req.query.gid))\r\n        .execute(\"web.GetMonitoredBusData\");        \r\n\r\n    context.res = {        \r\n        body: JSON.parse(busData.recordset[0][\"locationData\"])\r\n    };\r\n}\r\n<\/pre>\n<p>The above code is literally all you need, for example, if you are using Azure Functions to host your node code.<\/p>\n<p>I prefer to use the async\/await pattern when I can as it is so easy to use, but if for some reason you cannot use it, our you prefer the old Promise approach, or even the Callback one, you&#8217;ll be happy to know that they are supported too.<\/p>\n<p>Definitely recommended.<\/p>\n<h2>Warning Note<\/h2>\n<p>Make sure that you import the <code>mssql<\/code> package:<\/p>\n<pre class=\"prettyprint\">npm install mssql<\/pre>\n<p>even if the official name is <em>node-mssql<\/em>. Unfortunately if you try to import &#8220;node-mssql&#8221; package you&#8217;ll end up with something <a href=\"https:\/\/www.npmjs.com\/package\/node-mssql\"> different<\/a> and not really working \ud83d\ude41 Keep that in mind, I&#8217;ve lost a good hour just trying to figure out why my code was not working, and then I discovered I imported the wrong package :\/<\/p>\n<hr \/>\n<p>Photo by <a href=\"https:\/\/www.pexels.com\/@jonathanborba?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels\">Jonathan Borba<\/a> from <a href=\"https:\/\/www.pexels.com\/photo\/red-hammock-between-wall-3255245\/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels\">Pexels<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The node-mssql package simplify a lot the process of connecting and using Azure SQL with Node<\/p>\n","protected":false},"author":24720,"featured_media":1044,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,429],"tags":[496,495,460],"class_list":["post-1041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-node","tag-node-mssql","tag-package","tag-tedious"],"acf":[],"blog_post_summary":"<p>The node-mssql package simplify a lot the process of connecting and using Azure SQL with Node<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/24720"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=1041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/1041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/1044"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}