Tedium
Tedious is a great JavaScript implementation of the TDS protocol for interacting with Microsoft SQL Server, but its API is not particularly application friendly. Tedium gives you a very clean interface which helps you write code both quicker and safer.
Tedium is built using generators, and therefore requires node >= 0.11.x
. If you need a Tedious wrapper using the traditional callback style, try node-mssql. It's also a great library and there is a co-ified version of it as well.
Tedium is a work in progress. Expect there to be some problems and missing features...
Usage
npm install tedium
First create a connection pool. The tediousOptions
are exactly the same as the options accepted by the Tedious Connection constructor.
var tedium = require('tedium');
var tediousOptions = {
userName: 'user',
password: 'pass',
server: 'localhost',
options: {
database: 'DatabaseName'
}
};
var poolOptions = {
min: 0,
max: 20,
timeout: 30000
};
var pool = yield tedium.createConnectionPool(tediousOptions, poolOptions);
The best way to acquire a connection is to use the .using()
method. Inspired by C#'s using-block syntax, a the connection will be automatically released back to the pool when the "scope" function completes. If an exception is thrown inside the scope, the connection is closed instead of returning it to the pool and the exception is re-thrown.
yield pool.using(function * (db) {
var params = { id: tedium.int(52) };
var result = yield db.request('select * from MyTable where id = @id', params);
console.log('row count: ' + result.rowCount);
console.log(result.rows);
});
If you only need the connection open for one request, you can use the pool#request
shortcut method.
var results = yield pool.request(sql, params);
Data Types
Tedium supports all of the data types that Tedious does. Each type is a top level method on the tedium
object. The last argument is the value of the parameter. Some types accept a length, scale, and/or precision arguments as well.
For example, nVarChar accepts a length argument first. Such as
tedium.nVarChar(50, 'this string can be up to 50 chars')
tedium.nVarChar('max', 'this could be long...')
Type Signatures:
tedium.bit (value)
tedium.tinyInt (value)
tedium.smallInt (value)
tedium.int (value)
tedium.bigInt (value)
tedium.numeric (precision, scale, value)
tedium.decimal (precision, scale, value)
tedium.smallMoney (value)
tedium.money (value)
tedium.float (value)
tedium.real (value)
tedium.smallDateTime (value)
tedium.dateTime (value)
tedium.dateTime2 (scale, value)
tedium.dateTimeOffset (scale, value)
tedium.time (scale, value)
tedium.char (length, value)
tedium.varChar (length, value)
tedium.text (value)
tedium.nChar (length, value)
tedium.nVarChar (length, value)
tedium.nText (value)
tedium.binary (length, value)
tedium.varBinary (length, value)
tedium.image (value)
tedium.null (value)
tedium.tvp (value)
tedium.udt (value)
tedium.uniqueIdentifier (value)
tedium.xml (value)
Bulk Load
Tedium supports SQL Server Bulk Insert.
yield pool.using(function * (db)
{
var bulk = db.bulkLoad('#tmpTable');
bulk.addColumn('intCol', Tedium.int(), false, 'iii');
bulk.addColumn('strCol', Tedium.nVarChar(50), true, 'sss');
bulk.addRow({ iii: 201, sss: "one zero one" });
bulk.addRow([ 202, "one zero two" ]);
bulk.addRow(203, "one zero three");
var rowCount = yield bulk.execute({ createTable: true });
console.log(rowCount);
});