Skip to content

Commit

Permalink
Merge pull request #600 from kalinkrustev/patch-1
Browse files Browse the repository at this point in the history
semver-MAJOR add beforeConnect handler
  • Loading branch information
willmorgan authored Nov 20, 2018
2 parents 0bb14fd + 6b5e677 commit ee2777b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ Default driver, actively maintained and production ready. Platform independent,

**Extra options:**

- **beforeConnect(conn)** - Function, which is invoked before opening the connection. The parameter `conn` is the configured tedious `Connection`. It can be used for attaching event handlers like in this example:
```js
require('mssql').connect(...config, beforeConnect: conn => {
conn.once('connect', err => { err ? console.error(err) : console.log('mssql connected')})
conn.once('end', err => { err ? console.error(err) : console.log('mssql disconnected')})
}})
```
- **options.instanceName** - The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1434 on the database server must be reachable.
- **options.useUTC** - A boolean determining whether or not use UTC time for values without time zone offset (default: `true`).
- **options.encrypt** - A boolean determining whether or not the connection will be encrypted (default: `false`).
Expand All @@ -420,6 +427,7 @@ More information about Tedious specific options: http://tediousjs.github.io/tedi

**Extra options:**

- **beforeConnect(conn)** - Function, which is invoked before opening the connection. The parameter `conn` is the connection configuration, that can be modified to pass extra parameters to the driver's `open()` method.
- **connectionString** - Connection string (default: see below).
- **options.instanceName** - The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1444 on the database server must be reachable.
- **options.trustedConnection** - Use Windows Authentication (default: `false`).
Expand Down
4 changes: 4 additions & 0 deletions lib/msnodesqlv8.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ class ConnectionPool extends base.ConnectionPool {
debug('pool(%d): connection #%d created', IDS.get(this), connedtionId)
debug('connection(%d): establishing', connedtionId)

if (typeof this.config.beforeConnect === 'function') {
this.config.beforeConnect(cfg)
}

msnodesql.open(cfg, (err, tds) => {
if (err) {
err = new base.ConnectionError(err)
Expand Down
3 changes: 3 additions & 0 deletions lib/tedious.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ class ConnectionPool extends base.ConnectionPool {
if (this.config.debug) {
tedious.on('debug', this.emit.bind(this, 'debug', tedious))
}
if (typeof this.config.beforeConnect === 'function') {
this.config.beforeConnect(tedious)
}
})
}

Expand Down

0 comments on commit ee2777b

Please sign in to comment.