Skip to content

Commit

Permalink
Merge pull request #64 from digitalBush/feature-empty-pool
Browse files Browse the repository at this point in the history
feat: add ability to disable pool.
  • Loading branch information
rniemeyer committed Dec 12, 2023
2 parents df66cef + f7507d6 commit b8e6eec
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const sql = skwell.connect( {
} );

```

*NOTE:* Setting `pool:false` will disable pooling entirely.

At this point, you have a client (`sql`) that is ready to be used non-transactionally. A pool of connections is maintained in the background and one will be chosen for you to execute your queries. Queries will resolve with the values or be rejected with an error.

Expand Down
22 changes: 22 additions & 0 deletions spec/integration/emptyPool.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { config } = testHelpers;

const skwell = require( "src" );
describe( "No Pooling - Integration", () => {
let sql;
before( async () => {
const emptyPoolConfig = Object.assign( {}, config, { pool: false } );
sql = await skwell.connect( emptyPoolConfig );
} );

after( () => {
return sql.dispose();
} );

it( "should not reuse the same connection", async () => {
const query = "select connection_id from sys.dm_exec_connections where session_id = @@spid";

const cid1 = await sql.queryValue( query );
const cid2 = await sql.queryValue( query );
cid1.should.not.equal( cid2 );
} );
} );
8 changes: 5 additions & 3 deletions spec/integration/reset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ describe( "Connection Reset - Integration", () => {
} );

it( "should reuse the same connection between each call", async () => {
const spid = await sql.queryValue( "select @@spid" );
const spid2 = await sql.queryValue( "select @@spid" );
spid.should.equal( spid2 );
const query = "select connection_id from sys.dm_exec_connections where session_id = @@spid";

const cid1 = await sql.queryValue( query );
const cid2 = await sql.queryValue( query );
cid1.should.equal( cid2 );
} );

it( "should reset connection between calls", async () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/types.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe( "Types - Integration", () => {
( value BIGINT );
END` );

const ids = await sql.query( `SELECT value FROM @ids`, {
const ids = await sql.query( "SELECT value FROM @ids", {
ids: {
type: sql.tvp( "IdTable", {
value: sql.bigint
Expand Down
16 changes: 16 additions & 0 deletions src/EmptyPool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const EventEmitter = require( "events" );
class EmptyPool extends EventEmitter {

constructor( { create, destroy } ) {
super();

this.acquire = create;
this.release = destroy;
}

drain() { return Promise.resolve(); }
clear() { /* Nothing to do */ }

}

module.exports = EmptyPool;
4 changes: 4 additions & 0 deletions src/poolFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const genericPool = require( "generic-pool" );

const configBuilder = require( "./configBuilder" );
const connectionFactory = require( "./connectionFactory" );
const EmptyPool = require( "./EmptyPool" );

function getResourceFactory( client, config ) {
return {
Expand Down Expand Up @@ -30,5 +31,8 @@ function getResourceFactory( client, config ) {

module.exports = ( client, config ) => {
const resourceFactory = getResourceFactory( client, config );
if ( config.pool === false ) {
return new EmptyPool( resourceFactory );
}
return genericPool.createPool( resourceFactory, configBuilder.connectionPool( config ) );
};

0 comments on commit b8e6eec

Please sign in to comment.