Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ node_modules
local-config.json

.DS_Store

# webstorm
.idea/
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
{
"name": "Josh Bush",
"url": "https://github.com/digitalbush"
},
{
"name": "Aureliano Bergese",
"url": "https://github.com/auridevil"
}
],
"dependencies": {
Expand Down
68 changes: 43 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,83 @@ var _ = require( "lodash" );
var Monologue = require( "monologue.js" ).prototype;
var sql = require( "mssql" );
var connections = require( "./connections" );
var SqlContext = require( "./sqlContext" )();
var TransactionContext = require( "./transactionContext" )( SqlContext );
var SqlContextG = require( "./sqlContext" )();
var TransactionContextG = require( "./transactionContext" )( SqlContextG );
var utils = require( "./utils" );

function promisify( context, queryOptions ) {
var name = queryOptions.name || queryOptions.procedure || "__result__";
context.step( name, queryOptions );
return when.promise( function( resolve, reject, notify ) {
context
.end( resolve )
.error( reject )
.on( "data", notify );
.end( resolve )
.error( reject )
.on( "data", notify );
} );
}

var seriate = {
getTransactionContext: function( connection ) {
var options = { metrics: this.metrics, namespace: this.metricsNamespace };
var options = {
metrics: this.metrics,
namespace: this.metricsNamespace
};
if ( connection && connection.isolationLevel ) {
options.isolationLevel = connection.isolationLevel;
delete connection.isolationLevel;
}
options.connection = connections.get( connection );
return new TransactionContext( options );
return new this.TransactionContext( options );
},
getPlainContext: function( connection ) {
var conn = connections.get( connection );
var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn };
return new SqlContext( options );
var options = {
metrics: this.metrics,
namespace: this.metricsNamespace,
connection: conn
};
return new this.SqlContext( options );
},
executeTransaction: function( connection, queryOptions ) {
if ( arguments.length === 1 ) {
queryOptions = connection;
connection = undefined;
}
var conn = connections.get( connection );
var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn };
return promisify( new TransactionContext( options ), queryOptions );
var options = {
metrics: this.metrics,
namespace: this.metricsNamespace,
connection: conn
};
return promisify( new this.TransactionContext( options ), queryOptions );
},
execute: function( connection, queryOptions ) {
if ( arguments.length === 1 ) {
queryOptions = connection;
connection = undefined;
}
var conn = connections.get( connection );
var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn };
return promisify( new SqlContext( options ), queryOptions )
.then( function( data ) {
if ( data.__result__ ) {
return data.__result__;
} else {
return data[ queryOptions.procedure || queryOptions.name ];
}
} );
var options = {
metrics: this.metrics,
namespace: this.metricsNamespace,
connection: conn
};
return promisify( new this.SqlContext( options ), queryOptions )
.then( function( data ) {
if ( data.__result__ ) {
return data.__result__;
} else {
return data[queryOptions.procedure || queryOptions.name];
}
} );
},
first: function() {
var args = Array.prototype.slice.call( arguments, 0 );
return this.execute.apply( this, args ).then( function( rows ) {
return rows[ 0 ];
return rows[0];
} );
},
_getFilePath: utils._getFilePath,
fromFile: utils.fromFile,
addConnection: function( config ) {
connections.add( config );
Expand All @@ -85,16 +101,18 @@ var seriate = {
useMetrics: function( metrics, namespace ) {
this.metrics = metrics;
this.metricsNamespace = namespace;
}
},
SqlContext: SqlContextG,
TransactionContext: TransactionContextG
};

_.each( sql.TYPES, function( val, key ) {
seriate[ key ] = sql.TYPES[ key ];
seriate[ key.toUpperCase() ] = sql.TYPES[ key ];
seriate[key] = sql.TYPES[key];
seriate[key.toUpperCase()] = sql.TYPES[key];
} );

_.each( sql.ISOLATION_LEVEL, function( val, key ) {
seriate[ key ] = sql.ISOLATION_LEVEL[ key ];
seriate[key] = sql.ISOLATION_LEVEL[key];
} );

seriate.MAX = sql.MAX;
Expand Down
18 changes: 9 additions & 9 deletions src/sqlContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,6 @@ function preparedSql( state, name, options ) {
}
}

function executeSql( state, name, options ) {
if ( options.query || options.procedure ) {
return nonPreparedSql( state, name, options );
} else {
return preparedSql( state, name, options );
}
}

function addState( fsm, name, stepAction ) {
if ( fsm.states[ name ] ) {
throw new Error( "A step by that name already exists: " + fsm.instance );
Expand All @@ -187,7 +179,7 @@ function addState( fsm, name, stepAction ) {
_onEnter: function() {
var promise;
var exec = function( options ) {
promise = executeSql( fsm, name, options );
promise = fsm.executeSql( fsm, name, options );
return promise;
};

Expand Down Expand Up @@ -340,6 +332,14 @@ module.exports = function() {

abort: function() {
this.handle( "error", "Operation aborted" );
},

executeSql: function( state, name, options ) {
if ( options.query || options.procedure ) {
return nonPreparedSql( state, name, options );
} else {
return preparedSql( state, name, options );
}
}

} );
Expand Down
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ module.exports = {
fileCache[ p ] = content;
}
return content;
}
},
_getFilePath: _getFilePath
};