Skip to content

Commit

Permalink
Merge pull request #35 from arobson/feature-connection-events
Browse files Browse the repository at this point in the history
Added connectivity events and prevent multiple file reads
  • Loading branch information
Brian Edgerton committed Dec 4, 2015
2 parents 801249e + 59b970a commit e547508
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.5.*

### 0.5.5
* Emit connectivity events
* Prevent reading sql files multiple times

### 0.5.4
Update mssql to 2.3.2

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ A connection configuration provides information on how to connect to the datbase
}
```

### Connectivity Events
Seriate emits connectivity events from the top level library:

* connected
* closed
* failed

Each event includes a `name` property that represents which connection the event is happening on. If the event is failed, an `error` property will have the error that caused the failure.

## API

Sql type constants are exposed in both Pascal Case and all capitals off of the library. See the listing at the end of this document.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "seriate",
"version": "0.5.4",
"version": "0.5.5",
"description": "A cross platform module for Microsoft SQL Server that wraps node-mssql",
"main": "src/index.js",
"author": "LeanKit",
Expand Down Expand Up @@ -44,7 +44,7 @@
"configya": "~0.2.1",
"lodash": "3.x",
"machina": "1.x",
"monologue.js": "~0.3.1",
"monologue.js": "~0.3.3",
"mssql": "~2.3.2",
"postal": "^1.0.6",
"when": "3.x",
Expand Down
40 changes: 38 additions & 2 deletions spec/integration/seriate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function deleteTestRows( sql ) {
}

describe( "Seriate Integration Tests", function() {
var sql;
var sql, connected;
before( function() {
this.timeout( 20000 );
sql = proxyquire( "../src/index", {} );
Expand Down Expand Up @@ -73,12 +73,23 @@ describe( "Seriate Integration Tests", function() {
.then( function() {} );
}

sql.once( "connected", function() {
connected = true;
} );

return dropDatabase()
.then( createDatabase )
.then( setupPrerequisites );
} );

after( function() {
it( "should connect successfully", function() {
connected.should.equal.true;
} );

after( function( done ) {
sql.once( "closed", function( connection ) {
done();
} );
sql.closeConnection( "default" );
sql.closeConnection( "master" );
} );
Expand Down Expand Up @@ -368,6 +379,7 @@ describe( "Seriate Integration Tests", function() {
procResults.returnValue.should.equal( 0 );
} );
} );

describe( "when retrieving multiple record sets", function() {
before( function() {
return insertTestRows( sql );
Expand Down Expand Up @@ -439,6 +451,7 @@ describe( "Seriate Integration Tests", function() {
multipleResults.returnValue.should.equal( 0 );
} );
} );

describe( "with stored procedures", function() {
var multipleRSProcResults;

Expand Down Expand Up @@ -466,4 +479,27 @@ describe( "Seriate Integration Tests", function() {
} );
} );
} );

describe( "when failing to connect", function() {
var failed;
before( function( done ) {
sql.once( "failed", function( connection ) {
failed = connection;
done();
} );

sql.addConnection( {
name: "lol",
host: "lol",
user: "lol",
password: "lol",
database: "lol"
} );
} );

it( "should emit failed with an error", function() {
failed.name.should.equal( "lol" );
failed.should.have.property( "error" );
} );
} );
} );
28 changes: 18 additions & 10 deletions src/connections.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require( "lodash" );
var when = require( "when" );
var sql = require( "mssql" );
var Monologue = require( "monologue.js" ).prototype;
var log = require( "./log" )( "seriate.connection" );

var state = {
Expand All @@ -10,6 +11,14 @@ var state = {
aliases: {}
};

var api = _.assign( {
state: state,
add: addConnection,
close: closeConnection,
get: getConnection,
reset: resetState
}, Monologue );

function addConnection( config ) {
var name = getName( config );
var original = getConfiguration( name );
Expand Down Expand Up @@ -52,28 +61,33 @@ function connect( name, config ) {
pool = state.pools[ name ] = new sql.Connection( config );

pool.on( "connect", function() {
api.emit( "connected", { name: name } );
log.info( "Connected to \"%s\"", name );
} );

pool.on( "close", function() {
api.emit( "closed", { name: name } );
log.info( "Closed connection to \"%s\"", name );
pool.removeAllListeners();
delete state.connections[ name ];
delete state.pools[ name ];
} );

pool.on( "error", function( err ) {
function onConnectionError( err ) {
api.emit( "failed", { name: name, error: err } );
log.error( "Failed to connection to \"%s\" with: %s", name, err );
delete state.connections[ name ];
delete state.pools[ name ];
pool.removeAllListeners();
} );
}

pool.on( "error", onConnectionError );

state.pools[ name ] = pool;
state.connections[ name ] = pool.connect()
.then( function() {
return pool;
} );
}, onConnectionError );
return state.connections[ name ];
}

Expand Down Expand Up @@ -147,10 +161,4 @@ function resetState() {
};
}

module.exports = {
state: state,
add: addConnection,
close: closeConnection,
get: getConnection,
reset: resetState
};
module.exports = api;
25 changes: 23 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var when = require( "when" );
var fs = require( "fs" );
var _ = require( "lodash" );
var Monologue = require( "monologue.js" ).prototype;
var path = require( "path" );
var callsite = require( "callsite" );
var sql = require( "mssql" );
var connections = require( "./connections" );
var SqlContext = require( "./sqlContext" )();
var TransactionContext = require( "./transactionContext" )( SqlContext );
var fileCache = {};

function promisify( context, queryOptions ) {
var name = queryOptions.name || queryOptions.procedure || "__result__";
Expand Down Expand Up @@ -84,7 +86,12 @@ var seriate = {
p = this._getFilePath( p );
var ext = path.extname( p );
p = ( ext === "." ) ? ( p + "sql" ) : ( ext.length === 0 ) ? p + ".sql" : p;
return fs.readFileSync( p, { encoding: "utf8" } );
var content = fileCache[ p ];
if ( _.isEmpty( content ) ) {
content = fs.readFileSync( p, { encoding: "utf8" } );
fileCache[ p ] = content;
}
return content;
},
addConnection: function( config ) {
connections.add( config );
Expand Down Expand Up @@ -114,4 +121,18 @@ _.each( sql.TYPES, function( val, key ) {
seriate[ key.toUpperCase() ] = sql.TYPES[ key ];
} );

module.exports = seriate;
var api = _.assign( seriate, Monologue );

connections.on( "connected", function( info ) {
api.emit( "connected", info );
} );

connections.on( "closed", function( info ) {
api.emit( "closed", info );
} );

connections.on( "failed", function( info ) {
api.emit( "failed", info );
} );

module.exports = api;

0 comments on commit e547508

Please sign in to comment.