73

Is there any way I can get my Node.js app to communicate with Microsoft SQL? I haven't seen any MS SQL drivers out there in the wild?

I'm putting a very simple app together and need to be able to communicate with an existing MS SQL database (otherwise I would have gone with mongoDB or Redis)

3

15 Answers 15

60

The original question is old and now using node-mssql as answered by @Patrik Šimek that wraps Tedious as answered by @Tracker1 is the best way to go.

The Windows/Azure node-sqlserver driver as mentioned in the accepted answer requires you to install a crazy list of prerequisites: Visual C++ 2010, SQL Server Native Client 11.0, python 2.7.x and probably also Windows 7 SDK for 64-bit on your server. You don't want to install all these GB's of software on your Windows Server if you ask me.

You really want to use Tedious. But also use node-mssql to wrap it and make coding a lot easier.

Update August 2014

  • Both modules are still actively maintained. Issues are responded on quite quickly and efficiently.
  • Both modules support SQL Server 2000 - 2014
  • Streaming supported since node-mssql 1.0.1

Update February 2015 - 2.x (stable, npm)

  • Updated to latest Tedious 1.10
  • Promises
  • Pipe request to object stream
  • Detailed SQL errors
  • Transaction abort handling
  • Integrated type checks
  • CLI
  • Minor fixes

This is plain Tedious:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

var config = {
  server: '192.168.1.212',
  userName: 'test',
  password: 'test'
};

var connection = new Connection(config);

connection.on('connect', function(err) {
    executeStatement();
  }
);

function executeStatement() {
  request = new Request("select 42, 'hello world'", function(err, rowCount) {
    if (err) {
      console.log(err);
    } else {
      console.log(rowCount + ' rows');
    }

    connection.close();
  });

  request.on('row', function(columns) {
    columns.forEach(function(column) {
      if (column.value === null) {
        console.log('NULL');
      } else {
        console.log(column.value);
      }
    });
  });

  request.on('done', function(rowCount, more) {
    console.log(rowCount + ' rows returned');
  });

  // In SQL Server 2000 you may need: connection.execSqlBatch(request);
  connection.execSql(request);
}

Here comes node-mssql which has Tedious as a dependency. Use this!

var sql     = require('mssql');

var config = {
  server: '192.168.1.212',
  user:     'test',
  password: 'test'
};

sql.connect(config, function(err) {
    var request = new sql.Request();
    request.query("select 42, 'hello world'", function(err, recordset) {
        console.log(recordset);
    });
});
1
  • 1
    Thanks for your helpful answer. Looks like the authors strove for accurate nomenclature by naming their package "Tedious". At the time of this post, MSNodeSQL on github hasn't been touched in about 8 months, whereas this solution was last updated 20 days ago. This appears to be the only regularly updated Node.js MS SQL driver at this time.
    – Conor
    Mar 31, 2014 at 16:24
24

A couple of new node.js SQL server clients have just released recently. I wrote one called node-tds and there is another called tedious

3
  • Thanks for the response. Your client doesnt rely on .net in any way, is that correct? Is this in any way related to oren mazor's tds project? Jan 6, 2012 at 11:55
  • My client is 100% native JS (actually CoffeeScript) as is Tedious. It is in no way related to Oren Mazor's project and were I better at coming up with library names, would have thought of something better and less ambiguous.
    – Chad Retz
    Jan 9, 2012 at 18:35
  • @Chad Retz - This looks like a this may be solution for a project I am working on. Currently there is a .Net entry point through JavaScript that makes a database call and returns JavaScript. This will eliminate that slower, middle step. However, what about connection pooling, and will you be writing support for SQL Server 2008 TVP's?
    – ElHaix
    Mar 2, 2012 at 2:14
22

We just released preview drivers for Node.JS for SQL Server connectivity. You can find them here: http://blogs.msdn.com/b/sqlphp/archive/2012/06/08/introducing-the-microsoft-driver-for-node-js-for-sql-server.aspx

3
  • 4
    This is Windows only for the moment. I suppose that most people using SQL Server or Azure are windows folks anyways, but there are quite a few that just have their existing system deep into the SQL Server world. Below mentions the user of cross platform ODBC for those that may want to keep a Linux option in their future. And MS...tisk tisk...supporting node cross platform and then this junk. Sep 3, 2012 at 4:03
  • 7
    Just noting that the MS drivers are rather immature at this point, and that the Tedious driver seems to work better (cross platform).
    – Tracker1
    Oct 25, 2012 at 23:50
  • 4
    I would not advise anyone to use these drivers in their current state Dec 16, 2013 at 9:57
10

You could maybe use node-tds.js:

An exciting implementation of the TDS protocol for node.js to allow communication with sql server...

USAGE:

var mssql = require('./mssql');
var sqlserver = new mssql.mssql();
sqlserver.connect({'Server':__IP__,'Port':'1433','Database':'','User Id':'','Password':''});
var result = sqlserver.execute("SELECT * FROM wherever;");
3
  • Awesome. I didnt see that before. Thank you so much. Mar 1, 2011 at 16:34
  • 14
    The project description now reads *EXPERIMENTAL and currently ABANDONED* Oct 5, 2011 at 17:40
  • This alternative github project might be of use until MS add official support. github.com/vivina/node-mssql
    – booyaa
    Dec 2, 2011 at 16:19
9

(duplicating my answer from another question).

I would recommend node-mssql, which is a nice wrapper for other connectors, the default being my previous choice (Tedious) bringing a bit nicer of an interface. This is a JavaScript implimentation, with no compilation requirements, meaning you can work in windows and non-windows environments alike.

Another option, if you don't mind bringing in .Net or Mono with a binary bridge would be to use edge.js. Which can be very nice if you want to leverage .Net libraries in node.js

node-tds is abandoned, node-odbc doesn't work with windows, and the MS node-sqlserver driver doesn't seem to work on non-windows (and has some goofy requirements).

9

There is another module you can use - node-mssql. It uses other TDS modules as drivers and offer easy to use unified interface. It also add extra features and bug fixes.

Extra features:

  • Unified interface for multiple MSSQL drivers
  • Connection pooling with Transactions and Prepared statements
  • Parametrized Stored Procedures for all drivers
  • Serialization of Geography and Geometry CLR types
  • Smart JS data type to SQL data type mapper
  • Support both Promises and standard callbacks
0
5

TSQLFTW - T-SQL For The WIN(dows) - by Fosco Marotto https://github.com/gfosco/tsqlftw

It is a C# and ADO .NET managed code solution, with a C++ wrapper that Node.js can import and work with.


If you know .NET you could try WCF Data Services (ADO.NET Data Services); write an WCF app for data access and use odata (REST on steroids) to interact with the database


If you are into SOA and use SQL Server 2005 you could check out the Native XML Web Services for Microsoft SQL Server 2005

http://msdn.microsoft.com/en-us/library/ms345123(v=sql.90).aspx

You could access SQL Server as a web service (HTTP, SOAP)

4

Microsoft (The Windows Azure Team) just released a node driver for SQL SERVER.

It has no package for npm yert, as far as I know, but it is open sourced. And the accepting community contribution too.

https://github.com/WindowsAzure/node-sqlserver

Introduction blog post here:

http://blogs.msdn.com/b/sqlphp/archive/2012/06/08/introducing-the-microsoft-driver-for-node-js-for-sql-server.aspx

3

I'd suggest taking a look at Prisma. We just (October 2020) announced preview support for SQL Server.

Prisma is an ORM that puts the emphasis on type-safety and developer experience. Unlike traditional ORMs that typically map tables to classes, Prisma maps queries to types (in TypeScript) and returns plain objects from queries.

To get started with Prisma and SQL Server check out this example and start from scratch guide in the docs.

2

If you are running on .NET look at entityspaces.js at, we are creating an entire universal ORM for Node.js that will not require a WCF JSON service ... https://github.com/EntitySpaces/entityspaces.js

If you are using MSFT backend technology you could use it now, however, we are creating a universal Node.js ORM and will have more information on that soon

2

There is an update from Microsoft. Here is a series of blog posts (part 1 and part 2).

1

Node.js SQL Server drivers seem very immature - there's a mish-mash of different projects with varying dependencies, performance, and levels of completeness, none of which inspire confidence.

I'd propose using edge-sql. This leverages .NET's mature database driver ecosystem, and depends only on .NET (a no-brainer if you are running node on Windows - if not there is Mono, but I have not tried that).

Here is a node example (server.js) using edge-sql (note you need to put your connection string into an environment variable as per edge-sql docs):

var edge = require('edge');

// edge-sql has built in support for T-SQL / MSSQL Server
var getData = edge.func('sql', function () {/*
    select top 10 * from sometable
*/
});

getData(null, function (error, result) {
    if (error) throw error;
    console.log(result);
});

You can also leverage Edge.js with .NET to access other databases, such as Oracle. I have given an example of that approach here.

0

The status as of May 2016 is as follows.

The official Microsoft SQL Driver for Node, called node-sqlserver, has not been updated for a number of years.

There is a new fork on this called node-sqlserver-v8 that works with Node Versions 0.12.x. and >= 4.1.x. This fork also has pre-compiled binaries for x64 and x86 targets.

The package is available on NPM as msnodesqlv8.

I recommend this package because it is lightweight (has no dependencies) and it is the only one that works with all recent version of SQL Server, including SQL LocalDB.

0

Now (2016) you can use Sequelize ORM that supports:

  • MySQL / MariaDB,
  • PostgreSQL
  • SQLite
  • Microsoft SQL Server

It is widely used according to its Github's stars.

-3

that link details only a sql 2000 solution, not sql 2005 nor sql 2008, and also that code only allow sending sql text, and does not allow the execution of stored procedures.

The real solution would be to install node JS on a linux server, or on a virtual linux server on a windows machine, and then go to microsoft web site and download the JDBC java drivers and install those microsoft ms sql java jdbc drivers on either the linux server or linux virtual server.

3
  • Im not particularly comfortable with linux so i try to do as little sys admin as possible. Doing a JDBC setup sounds messy, and if there is a node driver that is available as a module that will make life so much easier. You are right that not having stored procedures support is not great. Sep 1, 2011 at 11:00
  • 1
    But these drivers are for Java only. How would you use JDBC in NodeJS? May 31, 2012 at 11:45
  • Haven't tested it but there is node-jdbc.
    – Beau
    Jul 10, 2013 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.