Wraps Tedious SQL commands with
Q
ores6
promises. Uses fluent syntax
Whats new?
- Transaction support (beta)
- Alternate return data formats (see Row Transformers)
- Should support any promise library
Install
$ npm install --save tedious-promises
Configure
Sample: config.json See the Tedious documentation for configuration details.
Initialization without connection pooling
var tp = ;var dbConfig = ;var TYPES = TYPES;tp; // global scope
Initialization with connection pooling
var tp = ;var dbConfig = ;var TYPES = TYPES;var ConnectionPool = ;var poolConfig = {}; // see tedious-connection-pool documentationvar pool = poolConfig dbConfig;tp; // global scope
Configure automatic column renaming
The callback can be any function that accepts a single string parameter, and returns a string.
var _ = ;tp; // global scope
Use
Basic usage
tp ; results === col1: 'row 1 col 1' col2: 'row 1 col 2' col1: 'row 2 col 1' col2: 'row 2 col 2'
Overriding column behavior
tp // rename column // create 'nameParts' object with 'first' property ;
Column types conversion
asBoolean can convert from:
- null === null
- Any integer: 0 === false
- strings: ** 'TRUE','T', 'Y', 'YES', '1' ** 'FALSE', 'F', 'N', 'NO', '0'
asDate can convert from:
- null === null
- integer: new Date(value)
- string: Date.parse(value)
tp ;
In a function returning the promise, with a parameter
{ return tp ;}
Returning the generated key from an identity column
(Technically, you don't need to use 'as id', but it makes the code easier to read.)
tp ;
Handeling each row yourself
tp ;
Return row count instead of data
Only usefull for INSERT, UPDATE, and DELETE statements
tp ;
Row transformers
- 'rowToObject' (default) converts each row into an object where the column names become the keys
col1: 'row 1 col 1' col2: 'row 1 col 2' col1: 'row 2 col 1' col2: 'row 2 col 2'
- 'rowToArray' converts each row into an array of values
'row 1 col 1' 'row 1 col 2' 'row 2 col 1' 'row 2 col 2'
- You can also pass in a function to do your own row transformation.
{ result = ; // Or {}, or anything you'd like for var i = 0; i < rowlength; i++ var col = rowi; var name = colmetadatacolName; // The getColumnMap function returns the built in column mappings. // The GetColumnValue function returns the columns value after being // processed by transformers like asBoolean() and asDate(). // See TediousPromiseColumn.js for other column functions. var map = this; var value = map // do something with the column name and value return result;}
Promises
The execute
function returns a promise (as do the transaction functions).
By default this is a Q promise.
es6 promises are also supported out of the box, but you should be able to
use any promise library by writing a small polyfill.
es6 Promises
// Set when you initialize TP to make it globaltp;tp; // You can also set the promise library on specific sql commandsreturn tp ;
Transactions
Transaction support in Tedious has been around for a long time, but it's new to Tedious Promises. So consider it beta for now. It is possible that there will be breaking changes in the future. (Initial implemetation by @akanieski)
var trans; // create the transaction from the a tp instancetp
Mocking for unit tests
Set the global mock function instead of setConnectionConfig or setConnectionPool to intercept all calls to tp.execute()
tp; parameters === id: name: 'id' type: TYPESInt value: 123 options: null
Setup end to end testing
- Create a SQL database either in Azure or locally
- Sample user setup and permissions are in
test/database/init.sql
- Create
test/database/config.json
based upon the sample below
- Replace all
REQUIRED
fields with your database credentials - This file is git ignored so that it isn't accidently checked in
- Run
grunt resetTestDatabase
to create the tables and populate test data - Run
grunt
to run the tests, orgrunt watch
to run the test on every file change
To do
- documentation
- sql generation and/or integration of a LINQ package
- local unit tests (current tests require a real database)
- more tests for the mock connection
- [![NPM version][npm-image]][npm-url]
- [![Build Status][travis-image]][travis-url]
License
MIT © Charles Hill