Question

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)

Was it helpful?

Solution

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

OTHER TIPS

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);
    });
});

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

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;");

(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).

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

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)

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

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

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

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.

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top