Promises, Node, Tedious, Azure SQL. Oh My!

Promises, Node, Tedious, Azure SQL. Oh My!

Encapsulating Tedious in a Promise to allow easy asyncronous support to the most used library to accees Azure SQL

If you are new to Node.js like I am, using Tedious to access Azure SQL can be challenging at the beginning. My understanding is that Tedious, while being fully asynchronous, doesn’t support nor Promises nor the more modern async/await pattern. Tedious, in fact, uses events to execute asynchronous code and so a bit of work is needed to make it compatible with Promises.

At the end of the day is just a few lines of code, but the process of discovering those two lines can be quite long and sometime frustrating. There is no clear statement anywhere that shows how to properly do that. I think that’s due to the fact that once you get the grasp of Promises it became absolutely obvious….but if you haven’t got to that point, you’re left in the dark.

Well, let’s shed some light then, and fix this hole in the shared knowledge book that is the internet!

Encapsulate usage of Tedious

To avoid writing the same code again and again, you probably want to encapsulate the business logic that executes a SQL command into a function. Something – in theory – like the following

executeSQL = function(query, params) {
  var result = null;

  const conn = new Connection(...);
  conn.on('connect', err => { ... invoke req ... });

  const req = new Request(query, err => { return result });
  req.addParameter(...params...);
  req.on('rows', columns => { ... build result ... });

  conn.connect();  
}

After the connect``() method has been successfully called, the connect event will happen. From there, the created Request can be executed. The request will generate a row event that allows you to get the result coming in from Azure SQL, and process and store it into a variable of your choice.

azure sql javascript node developers development promises tedious

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Serverless REST API with Azure Functions, Node, JSON and Azure SQL

In this post use Azure SQL JSON support to easily create serverless REST API with Node, taking advantage of all available post-relational features

TodoMVC Full Stack with Azure Static Web Apps, Node and Azure SQL

This article is part of #ServerlessSeptember. You’ll find other helpful articles, detailed tutorials, and videos in this all-things-Serverless content collection. New articles from community members and cloud advocates are published every week from Monday to Thursday through September.

Demo: Configure Azure SQL with Azure CLI | Azure SQL for beginners (Ep. 17)

In this video, see how to configure your Azure SQL connectivity leveraging the Azure CLI and PowerShell notebooks in Azure Data Studio. For the full Azure SQ...

Top Node.js Development Companies and Expert NodeJS Developers

A thoroughly researched list of top NodeJS development companies with ratings & reviews to help hire the best Node.JS developers who provide development services and solutions across the world. List of Leading Node.js development Service Providers...