node-bcp
A node.js wrapper for the SQL Server bcp utility which allows for bulk insert and export operations.
Install
npm install bcp
You will need the bcp
utility installed on your system. If you're running on Windows and use SQL Server, you probably already have this. On Linux, you can use Microsoft's ODBC Driver for Linux which comes includes the bcp utility. On OSX, sorry, better luck next time.
This library does not support FreeTDS's reimplementation called freebcp
. Their implementation is very incomplete and differs enough from the Microsoft version
Bulk Import Example
Imagine we have this table:
MyDatabase.dbo.MyTable
Column | Type |
---|
Id | Int Identity |
MyDate | DateTime |
MyFloat | Float |
MyString | NVarChar(50) |
var Bcp = require('bcp');
var b = new Bcp({
user: 'login_name',
password: 'password',
database: 'MyDatabase',
fieldTerminator: '\t::\t',
rowTerminator: '\t::\n',
unicode: true,
checkConstraints: true
});
b.prepareBulkInsert('MyTable', ['myDate', 'myFloat', 'myString'], function (err, imp) {
if (err)
imp.writeRows([
{ myDate: new Date(), myFloat: 23.7, myString: 'hello' },
{ myDate: new Date('2014-09-01'), myFloat: 7.23, myString: 'world' }
]);
imp.execute(function (error)
{
if (error)
});
});
Bulk Export Example
var b = new Bcp({
user: 'login_name',
password: 'password',
database: 'MyDatabase',
checkConstraints: false,
unicode: false,
native: true
});
var customOptions = {
read: false,
keepFiles: true,
sql: 'select * from MyDatabase.dbo.MyTable where id = 1'
};
b.bulkExport(tablename, customOptions, function (err) {
});
For a full description of all of the options which can be passed to the Bcp constructor, see lib/Bcp.js and Microsoft's bcp documentation.
Other Notes
This library is incomplete, and not very well tested. It needs lots more documentation, features, better error reporting, and love. Bulk Export is partially implemented, but there are no examples yet. Use at your own risk. May cause drowsiness and irritability.