npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

azuresql

v0.2.4

Published

A tiny Azure SQL database client utility

Readme

A tiny Azure SQL database Node.js client utility to help you focus on just writing your SQL query.

GitHub issues GitHub forks GitHub stars GitHub license

Installation

npm install azuresql

Usage

const { sqlQuery, closePool } = require('azureSql')
try {
  var result = await sqlQuery('select * from tableName where id = @id', { k: 'id', v: 1 })
  ...
} catch (e) {
  ...
}

Only call closePool() at the end of application where no further queries to the database are to be performed.

Configuration

The following environment variables need to be set up:

  • sqlUser

    Azure SQL database user

  • sqlPass

    Azure SQL database password

  • sqlServer

    Azure SQL database server name in a format like: .database.windows.net

  • sqlDb

    Azure SQL database name

  • sqlPoolMax

    Connection pool maximum connections. Default 1

  • sqlPoolMin

    Connection pool minimum connections. Default 0

  • sqlPoolIdleTimeout

    Connection pool idle timeout in miliseconds. Default 30000 (30 seconds)

Also make sure your Azure SQL's firewall allows the IPs that are trying to connect.

Examples

Multiple queries in one go

var r = await sqlQuery(`SELECT userId FROM ${tableUser} WHERE id = @id; SELECT beginTime FROM ${tableSession} WHERE session = @session`,
  { k: 'id', v: data.userId }, { k: 'session', v: data.sessionNo })
r.recordsets[0] ... // first query results
r.recordsets[1] ... // second query results

Insert and SQL Data Types

const { sql, sqlQuery } = require('azuresql')
var r = await sqlQuery(`insert into ${tableName} (id, notes, amount) vaules(@id, @comment, @money)`,
  { k: 'id', v: 3 }, { k: 'comment', v: 'blah...', type: sql.VarChar(sql.MAX) }, { k: 'money', v: 9876543210.1234, type: sql.Money })

Check more SQL data types.

An upsert example

A slightly complex example using T-SQL's upsert/merge syntax. The code is succinct as the data object has the same property names as the table comlumns.

const columns = ['orderNo', 'details', 'quantity'] // table columns
const sql =
  `merge ${tableName} target
  using (values (@orderNo)) as src (orderNo)
  on target.orderNo = src.orderNo
  when matched then
    update set ${columns.slice(1).map(r => 'target.' + r + '=@' + r).join()}
  when not matched by target then
    insert (${columns.join()})
    values (@${columns.join(',@')})`

// When the data object has the same property names as the table columns, the prepared statement will be easy:
var params = columns.map(r => ({ k: r, v: data[r] }))
var result = await sqlQuery(sql, ...params)

Enable debug info

Set the DEBUG envrionment variable to azuresql.

On Windows CMD:

set DEBUG=azuresql & node app.js

PowerShell (VS Code default)

$env:DEBUG='azuresql'; node app.js

Functions

sqlQuery(sql, ...params) ⇒ object

Query Azure SQL database

Kind: global function Returns: object - an object like: { recordsets: [[{...}]], recordset:[{...}], output: {}, rowsAffected: [] }

| Param | Type | Description | | --- | --- | --- | | sql | string | T-SQL | | ...params | object | rest parameters | | param.k | string | key for prepared statements | | param.v | string | value for prepared statements | | [param.type] | object | SQL data types, optional in normal cases |

closePool()

NOTE: Should only be called at the end of application!

Kind: global function

getRequest() ⇒ object

NOTE: Only when you need to extend and play with other features in mssql package

Kind: global function Returns: object - Request object