mssql 7.2.0 → 8.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.txt +19 -2
- package/LICENSE.md +22 -22
- package/README.md +136 -208
- package/bin/mssql +0 -0
- package/lib/base/connection-pool.js +5 -40
- package/lib/base/index.js +7 -0
- package/lib/cli.js +113 -113
- package/lib/isolationlevel.js +9 -9
- package/lib/msnodesqlv8/connection-pool.js +4 -2
- package/lib/msnodesqlv8/index.js +7 -0
- package/lib/msnodesqlv8/request.js +12 -2
- package/lib/shared.js +7 -0
- package/lib/table.js +7 -0
- package/lib/tedious/connection-pool.js +10 -10
- package/lib/tedious/index.js +7 -0
- package/lib/tedious/request.js +14 -14
- package/package.json +6 -6
- package/test.js +40 -51
- package/lib/connectionstring.js +0 -289
package/CHANGELOG.txt
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
v8.0.0 (2022-01-30)
|
|
2
|
+
-------------------
|
|
3
|
+
[new] Add table.rows.clear() method to allow for chunking updates ([#1094](https://github.com/tediousjs/node-mssql/pull/1094))
|
|
4
|
+
[new] Add valueHandler map to store callbacks that are used to process row values ([#1356](https://github.com/tediousjs/node-mssql/pull/1356))
|
|
5
|
+
[change] msnodesqlv8 driver detects os platform and attempts to pick correct connections string for it ((#1318)[https://github.com/tediousjs/node-mssql/pull/1318])
|
|
6
|
+
[change] Updated to latest Tedious 14 ((#1312)[https://github.com/tediousjs/node-mssql/pull/1312])
|
|
7
|
+
[change] Errors for bad bulk load parameters have slightly different error messages ((#1318)[https://github.com/tediousjs/node-mssql/pull/1318])
|
|
8
|
+
[change] Options provided to the driver via the config.options object will not be overridden with other values if set explicitly ((#1340)[https://github.com/tediousjs/node-mssql/pull/1340])
|
|
9
|
+
[change] Duplicate column names will now be presented as an array even if the values are empty ((#1240)[https://github.com/tediousjs/node-mssql/pull/1240])
|
|
10
|
+
[change] Update tarn pool dependency ((#1344)[https://github.com/tediousjs/node-mssql/pull/1344])
|
|
11
|
+
[removed] Remove connection string parser helpers ((#1342)[https://github.com/tediousjs/node-mssql/pull/1342])
|
|
12
|
+
[removed] Remove deprecated pool properties ((#1359)[https://github.com/tediousjs/node-mssql/pull/1359])
|
|
13
|
+
|
|
14
|
+
v7.2.1 (2021-08-19)
|
|
15
|
+
-------------------
|
|
16
|
+
[fix] Fix issue with bulk insert of dates ((#1298)[https://github.com/tediousjs/node-mssql/pull/1298])
|
|
17
|
+
|
|
1
18
|
v7.2.0 (2021-07-29)
|
|
2
19
|
-------------------
|
|
3
20
|
[new] Update Geography field parsing to provide lat/lng props from Geography Point ((#1282)[https://github.com/tediousjs/node-mssql/pull/1282])
|
|
@@ -243,7 +260,7 @@ v4.0.4 (2017-04-25)
|
|
|
243
260
|
|
|
244
261
|
v4.0.3 (2017-04-25)
|
|
245
262
|
-------------------
|
|
246
|
-
[fix] Fixed broken CLI &
|
|
263
|
+
[fix] Fixed broken CLI & debugging
|
|
247
264
|
|
|
248
265
|
v4.0.2 (2017-04-19)
|
|
249
266
|
-------------------
|
|
@@ -264,7 +281,7 @@ v4.0.0 (2017-04-01)
|
|
|
264
281
|
[change] Removed verbose and debug mode
|
|
265
282
|
[change] Removed 'driver' from options
|
|
266
283
|
[change] Removed Transaction and Prepared Statement queues
|
|
267
|
-
[change] Removed 'multiple'
|
|
284
|
+
[change] Removed 'multiple' directive
|
|
268
285
|
[change] Connection renamed to ConnectionPool
|
|
269
286
|
[change] Updated to latest Tedious 2.0.0
|
|
270
287
|
|
package/LICENSE.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2013-2018 Patrik Simek and contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2018 Patrik Simek and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -140,7 +140,7 @@ async () => {
|
|
|
140
140
|
* [SQL injection](#sql-injection)
|
|
141
141
|
* [Known Issues](#known-issues)
|
|
142
142
|
* [Contributing](https://github.com/tediousjs/node-mssql/wiki/Contributing)
|
|
143
|
-
* [6.x to 7.x changes
|
|
143
|
+
* [6.x to 7.x changes](#6x-to-7x-changes)
|
|
144
144
|
* [5.x to 6.x changes](#5x-to-6x-changes)
|
|
145
145
|
* [4.x to 5.x changes](#4x-to-5x-changes)
|
|
146
146
|
* [3.x to 4.x changes](#3x-to-4x-changes)
|
|
@@ -370,264 +370,169 @@ function processRows() {
|
|
|
370
370
|
}
|
|
371
371
|
```
|
|
372
372
|
|
|
373
|
-
##
|
|
374
|
-
|
|
375
|
-
An important concept to understand when using this library is [Connection Pooling](https://en.wikipedia.org/wiki/Connection_pool)
|
|
376
|
-
as this library uses connection pooling extensively.
|
|
373
|
+
## Connection Pools
|
|
377
374
|
|
|
378
|
-
As one Node JS process is able to handle multiple requests at once, we can take advantage of this long running process
|
|
379
|
-
to create a pool of database connections for reuse; this saves overhead of connecting to the database for each request
|
|
375
|
+
An important concept to understand when using this library is [Connection Pooling](https://en.wikipedia.org/wiki/Connection_pool) as this library uses connection pooling extensively. As one Node JS process is able to handle multiple requests at once, we can take advantage of this long running process to create a pool of database connections for reuse; this saves overhead of connecting to the database for each request
|
|
380
376
|
(as would be the case in something like PHP, where one process handles one request).
|
|
381
377
|
|
|
382
|
-
With the advantages of pooling comes some added complexities, but these are mostly just conceptual and once you understand
|
|
383
|
-
how the pooling is working, it is simple to make use of it efficiently and effectively.
|
|
378
|
+
With the advantages of pooling comes some added complexities, but these are mostly just conceptual and once you understand how the pooling is working, it is simple to make use of it efficiently and effectively.
|
|
384
379
|
|
|
385
|
-
### The Global Connection
|
|
380
|
+
### The Global Connection Pool
|
|
386
381
|
|
|
387
|
-
To assist with pool management in your application there is the
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
it will resolve to the connected pool. For example:
|
|
382
|
+
To assist with pool management in your application there is the `sql.connect()` function that is used to connect to the global connection pool. You can make repeated calls to this function, and if the global pool is already connected, it will resolve to the connected pool. The following example obtains the global connection pool by running `sql.connect()`, and then runs the query against the pool.
|
|
383
|
+
|
|
384
|
+
NB: It's important to note that there can only be one global connection pool connected at a time. Providing a different connection config to the `connect()` function will not create a new connection if it is already connected.
|
|
391
385
|
|
|
392
386
|
```js
|
|
393
387
|
const sql = require('mssql')
|
|
388
|
+
const config = { ... }
|
|
394
389
|
|
|
395
390
|
// run a query against the global connection pool
|
|
396
391
|
function runQuery(query) {
|
|
397
392
|
// sql.connect() will return the existing global pool if it exists or create a new one if it doesn't
|
|
398
|
-
return sql.connect().then((pool) => {
|
|
393
|
+
return sql.connect(config).then((pool) => {
|
|
399
394
|
return pool.query(query)
|
|
400
395
|
})
|
|
401
396
|
}
|
|
402
|
-
|
|
403
397
|
```
|
|
404
398
|
|
|
405
|
-
|
|
406
|
-
We also do *not* close the pool after the query is executed and that is because other queries may need to be run against
|
|
407
|
-
this pool and closing it will add an overhead to running the query. We should only ever close the pool when our application
|
|
408
|
-
is finished. For example, if we are running some kind of CLI tool or a CRON job:
|
|
399
|
+
Awaiting or `.then`-ing the pool creation is a safe way to ensure that the pool is always ready, without knowing where it is needed first. In practice, once the pool is created then there will be no delay for the next `connect()` call.
|
|
409
400
|
|
|
410
|
-
|
|
411
|
-
const sql = require('mssql')
|
|
401
|
+
Also notice that we do *not* close the global pool by calling `sql.close()` after the query is executed, because other queries may need to be run against this pool and closing it will add additional overhead to running subsequent queries. You should only ever close the global pool if you're certain the application is finished. Or for example, if you are running some kind of CLI tool or a CRON job you can close the pool at the end of the script.
|
|
412
402
|
|
|
413
|
-
|
|
414
|
-
sql.connect().then(pool => {
|
|
415
|
-
return pool.query('SELECT 1')
|
|
416
|
-
}).then(result => {
|
|
417
|
-
// do something with result
|
|
418
|
-
}).then(() => {
|
|
419
|
-
return sql.close()
|
|
420
|
-
})
|
|
421
|
-
})()
|
|
422
|
-
```
|
|
423
|
-
|
|
424
|
-
Here the connection will be closed and the node process will exit once the queries and other application logic has completed.
|
|
425
|
-
You should aim to only close the pool once in your application, when it is exiting or you know your application will never make
|
|
426
|
-
another SQL query.
|
|
403
|
+
### Global Pool Single Instance
|
|
427
404
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
In some instances you will not want to use the connection pool, you may have multiple databases to connect to or you may have
|
|
431
|
-
one pool for read-only operations and another pool for read-write. In this instance you will need to implement your own pool
|
|
432
|
-
management.
|
|
405
|
+
The ability to call `connect()` and `close()` repeatedly on the global pool is intended to make pool management easier, however it is better to maintain your own reference to the pool, where `connect()` is called **once**, and the resulting global pool's connection promise is re-used throughout the entire application.
|
|
433
406
|
|
|
434
|
-
|
|
407
|
+
For example, in Express applications, the following approach uses a single global pool instance added to the `app.locals` so the application has access to it when needed. The server start is then chained inside the `connect()` promise.
|
|
435
408
|
|
|
436
409
|
```js
|
|
437
|
-
const
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
410
|
+
const express = require('express')
|
|
411
|
+
const sql = require('mssql')
|
|
412
|
+
const config = {/*...*/}
|
|
413
|
+
//instantiate a connection pool
|
|
414
|
+
const appPool = new sql.ConnectionPool(config)
|
|
415
|
+
//require route handlers and use the same connection pool everywhere
|
|
416
|
+
const route1 = require('./routes/route1')
|
|
417
|
+
const app = express()
|
|
418
|
+
app.get('/path', route1)
|
|
419
|
+
|
|
420
|
+
//connect the pool and start the web server when done
|
|
421
|
+
appPool.connect().then(function(pool) {
|
|
422
|
+
app.locals.db = pool;
|
|
423
|
+
const server = app.listen(3000, function () {
|
|
424
|
+
const host = server.address().address
|
|
425
|
+
const port = server.address().port
|
|
426
|
+
console.log('Example app listening at http://%s:%s', host, port)
|
|
446
427
|
})
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const pool = getPool(name)
|
|
451
|
-
if (pool) {
|
|
452
|
-
delete POOLS[name]
|
|
453
|
-
return pool.close()
|
|
454
|
-
}
|
|
455
|
-
return Promise.resolve()
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function getPool(name) {
|
|
459
|
-
if (Object.prototype.hasOwnProperty.apply(POOLS, name)) {
|
|
460
|
-
return POOLS[name]
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
module.exports = {
|
|
465
|
-
closePool,
|
|
466
|
-
createPool,
|
|
467
|
-
getPool
|
|
468
|
-
}
|
|
428
|
+
}).catch(function(err) {
|
|
429
|
+
console.error('Error creating connection pool', err)
|
|
430
|
+
});
|
|
469
431
|
```
|
|
470
432
|
|
|
471
|
-
|
|
472
|
-
should aim to only close a pool when you know it will never be needed by the application again; typically this will be when
|
|
473
|
-
your application is shutting down.
|
|
474
|
-
|
|
475
|
-
## Connection Pools
|
|
476
|
-
|
|
477
|
-
Using a single connection pool for your application/service is recommended.
|
|
478
|
-
Instantiating a pool with a callback, or immediately calling `.connect`, is asynchronous to ensure a connection can be
|
|
479
|
-
established before returning. From that point, you're able to acquire connections as normal:
|
|
480
|
-
|
|
481
|
-
```javascript
|
|
482
|
-
const sql = require('mssql')
|
|
433
|
+
Then the route uses the connection pool in the `app.locals` object:
|
|
483
434
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
try {
|
|
495
|
-
const request = pool1.request(); // or: new sql.Request(pool1)
|
|
496
|
-
const result = await request.query('select 1 as number')
|
|
497
|
-
console.dir(result)
|
|
498
|
-
return result;
|
|
499
|
-
} catch (err) {
|
|
500
|
-
console.error('SQL error', err);
|
|
435
|
+
```js
|
|
436
|
+
// ./routes/route1.js
|
|
437
|
+
const sql = require('mssql');
|
|
438
|
+
|
|
439
|
+
module.exports = function(req, res) {
|
|
440
|
+
req.locals.db.query('SELECT TOP 10 * FROM table_name', function(err, recordset) {
|
|
441
|
+
if (err) {
|
|
442
|
+
console.error(err)
|
|
443
|
+
res.status(500).send('SERVER ERROR')
|
|
444
|
+
return
|
|
501
445
|
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
// promise style:
|
|
505
|
-
const pool2 = new sql.ConnectionPool(config)
|
|
506
|
-
const pool2Connect = pool2.connect()
|
|
507
|
-
|
|
508
|
-
pool2.on('error', err => {
|
|
509
|
-
// ... error handler
|
|
510
|
-
})
|
|
511
|
-
|
|
512
|
-
function runStoredProcedure() {
|
|
513
|
-
return pool2Connect.then((pool) => {
|
|
514
|
-
pool.request() // or: new sql.Request(pool2)
|
|
515
|
-
.input('input_parameter', sql.Int, 10)
|
|
516
|
-
.output('output_parameter', sql.VarChar(50))
|
|
517
|
-
.execute('procedure_name', (err, result) => {
|
|
518
|
-
// ... error checks
|
|
519
|
-
console.dir(result)
|
|
520
|
-
})
|
|
521
|
-
}).catch(err => {
|
|
522
|
-
// ... error handler
|
|
523
|
-
})
|
|
446
|
+
res.status(200).json({ message: 'success' })
|
|
447
|
+
})
|
|
524
448
|
}
|
|
525
449
|
```
|
|
526
450
|
|
|
527
|
-
|
|
528
|
-
is needed first. In practice, once the pool is created then there will be no delay for the next operation.
|
|
451
|
+
### Advanced Pool Management
|
|
529
452
|
|
|
530
|
-
|
|
531
|
-
thrown, allowing for the safe use of `mssql.connect().then(...)` throughout your code as well as making multiple calls to
|
|
532
|
-
close when your application is shutting down.
|
|
453
|
+
For some use-cases you may want to implement your own connection pool management, rather than using the global connection pool. Reasons for doing this include:
|
|
533
454
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
Repeatedly calling `connect()` when running queries risks running into problems when `close()` is called on the pool.
|
|
455
|
+
* Supporting connections to multiple databases
|
|
456
|
+
* Creation of separate pools for read vs read/write operations
|
|
537
457
|
|
|
538
|
-
|
|
458
|
+
The following code is an example of a custom connection pool implementation.
|
|
539
459
|
|
|
540
|
-
```
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
console.dir(result)
|
|
545
|
-
}).catch(err => {
|
|
546
|
-
// ... error checks
|
|
547
|
-
})
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
All values are automatically sanitized against sql injection.
|
|
460
|
+
```js
|
|
461
|
+
// pool-manager.js
|
|
462
|
+
const mssql = require('mssql')
|
|
463
|
+
const pools = new Map();
|
|
551
464
|
|
|
552
|
-
|
|
465
|
+
module.exports = {
|
|
466
|
+
/**
|
|
467
|
+
* Get or create a pool. If a pool doesn't exist the config must be provided.
|
|
468
|
+
* If the pool does exist the config is ignored (even if it was different to the one provided
|
|
469
|
+
* when creating the pool)
|
|
470
|
+
*
|
|
471
|
+
* @param {string} name
|
|
472
|
+
* @param {{}} [config]
|
|
473
|
+
* @return {Promise.<mssql.ConnectionPool>}
|
|
474
|
+
*/
|
|
475
|
+
get: (name, config) => {
|
|
476
|
+
if (!pools.has(name)) {
|
|
477
|
+
if (!config) {
|
|
478
|
+
throw new Error('Pool does not exist');
|
|
479
|
+
}
|
|
480
|
+
const pool = new mssql.ConnectionPool(config);
|
|
481
|
+
// automatically remove the pool from the cache if `pool.close()` is called
|
|
482
|
+
const close = pool.close.bind(pool);
|
|
483
|
+
pool.close = (...args) => {
|
|
484
|
+
pools.delete(name);
|
|
485
|
+
return close(...args);
|
|
486
|
+
}
|
|
487
|
+
pools.set(name, pool.connect());
|
|
488
|
+
}
|
|
489
|
+
return pools.get(name);
|
|
490
|
+
},
|
|
491
|
+
/**
|
|
492
|
+
* Closes all the pools and removes them from the store
|
|
493
|
+
*
|
|
494
|
+
* @return {Promise<mssql.ConnectionPool[]>}
|
|
495
|
+
*/
|
|
496
|
+
closeAll: () => Promise.all(Array.from(pools.values()).map((connect) => {
|
|
497
|
+
return connect.then((pool) => pool.close());
|
|
498
|
+
})),
|
|
499
|
+
};
|
|
500
|
+
```
|
|
553
501
|
|
|
554
|
-
|
|
555
|
-
of a single pool this library exposes a set of functions to access a single global connection. eg:
|
|
502
|
+
This file can then be used in your application to create, fetch, and close pools.
|
|
556
503
|
|
|
557
504
|
```js
|
|
558
|
-
|
|
505
|
+
const { get } = require('./pool-manager')
|
|
559
506
|
|
|
560
|
-
|
|
561
|
-
const
|
|
507
|
+
async function example() {
|
|
508
|
+
const pool = await get('default')
|
|
509
|
+
return pool.request().query('SELECT 1')
|
|
510
|
+
}
|
|
511
|
+
```
|
|
562
512
|
|
|
563
|
-
|
|
513
|
+
Similar to the global connection pool, you should aim to only close a pool when you know it will never be needed by the application again. Typically this will only be when your application is shutting down.
|
|
564
514
|
|
|
565
|
-
|
|
566
|
-
return sql.query('SELECT 1')
|
|
567
|
-
}).then(result => {
|
|
568
|
-
console.dir(result)
|
|
569
|
-
})
|
|
515
|
+
### Result value manipulation
|
|
570
516
|
|
|
571
|
-
|
|
572
|
-
poolPromise.then(() => {
|
|
573
|
-
return sql.close()
|
|
574
|
-
})
|
|
575
|
-
```
|
|
517
|
+
In some instances it is desirable to manipulate the record data as it is returned from the database, this may be to cast it as a particular object (eg: `moment` object instead of `Date`) or similar.
|
|
576
518
|
|
|
577
|
-
|
|
578
|
-
pool), then you will need to manage your pools yourself. The best way to do this is to create a shared library file that
|
|
579
|
-
can hold references to the pools for you. For example:
|
|
519
|
+
In v8.0.0+ it is possible to register per-datatype handlers:
|
|
580
520
|
|
|
581
521
|
```js
|
|
582
522
|
const sql = require('mssql')
|
|
583
523
|
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
// manage a set of pools by name (config will be required to create the pool)
|
|
587
|
-
// a pool will be removed when it is closed
|
|
588
|
-
async function getPool(name, config) {
|
|
589
|
-
if (!Object.prototype.hasOwnProperty.call(pools, name)) {
|
|
590
|
-
const pool = new sql.ConnectionPool(config)
|
|
591
|
-
const close = pool.close.bind(pool)
|
|
592
|
-
pool.close = (...args) => {
|
|
593
|
-
delete pools[name]
|
|
594
|
-
return close(...args)
|
|
595
|
-
}
|
|
596
|
-
await pool.connect()
|
|
597
|
-
pools[name] = pool
|
|
598
|
-
}
|
|
599
|
-
return pools[name]
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
// close all pools
|
|
603
|
-
function closeAll() {
|
|
604
|
-
return Promise.all(Object.values(pools).map((pool) => {
|
|
605
|
-
return pool.close()
|
|
606
|
-
}))
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
module.exports = {
|
|
610
|
-
closeAll,
|
|
611
|
-
getPool
|
|
612
|
-
}
|
|
613
|
-
```
|
|
614
|
-
|
|
615
|
-
You can then use this library file in your code to get a connected pool when you need it:
|
|
524
|
+
// in this example all integer values will return 1 more than their actual value in the database
|
|
525
|
+
sql.valueHandler.set(sql.TYPES.Int, (value) => value + 1)
|
|
616
526
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
// run a query
|
|
621
|
-
async function runQuery(query, config) {
|
|
622
|
-
// pool will always be connected when the promise has resolved - may reject if the connection config is invalid
|
|
623
|
-
const pool = await getPool('default', config)
|
|
624
|
-
const result = await pool.request().query(query)
|
|
625
|
-
return result
|
|
626
|
-
}
|
|
527
|
+
sql.query('SELECT * FROM [example]').then((result) => {
|
|
528
|
+
// all `int` columns will return a manipulated value as per the callback above
|
|
529
|
+
})
|
|
627
530
|
```
|
|
628
531
|
|
|
629
532
|
## Configuration
|
|
630
533
|
|
|
534
|
+
The following is an example configuration object:
|
|
535
|
+
|
|
631
536
|
```javascript
|
|
632
537
|
const config = {
|
|
633
538
|
user: '...',
|
|
@@ -1616,6 +1521,20 @@ request.execute('MyCustomStoredProcedure', (err, result) => {
|
|
|
1616
1521
|
|
|
1617
1522
|
**TIP**: You can also create Table variable from any recordset with `recordset.toTable()`. You can optionally specify table type name in the first argument.
|
|
1618
1523
|
|
|
1524
|
+
You can clear the table rows for easier batching by using `table.rows.clear()`
|
|
1525
|
+
|
|
1526
|
+
```js
|
|
1527
|
+
const tvp = new sql.Table() // You can optionally specify table type name in the first argument.
|
|
1528
|
+
|
|
1529
|
+
// Columns must correspond with type we have created in database.
|
|
1530
|
+
tvp.columns.add('a', sql.VarChar(50))
|
|
1531
|
+
tvp.columns.add('b', sql.Int)
|
|
1532
|
+
|
|
1533
|
+
// Add rows
|
|
1534
|
+
tvp.rows.add('hello tvp', 777) // Values are in same order as columns.
|
|
1535
|
+
tvp.rows.clear()
|
|
1536
|
+
```
|
|
1537
|
+
|
|
1619
1538
|
## Response Schema
|
|
1620
1539
|
|
|
1621
1540
|
An object returned from a `sucessful` basic query would look like the following.
|
|
@@ -2094,11 +2013,20 @@ request.query('select @myval as myval', (err, result) => {
|
|
|
2094
2013
|
- If you're facing problems with connecting SQL Server 2000, try setting the default TDS version to 7.1 with `config.options.tdsVersion = '7_1'` ([issue](https://github.com/tediousjs/node-mssql/issues/36))
|
|
2095
2014
|
- If you're executing a statement longer than 4000 chars on SQL Server 2000, always use [batch](#batch-batch-callback) instead of [query](#query-command-callback) ([issue](https://github.com/tediousjs/node-mssql/issues/68))
|
|
2096
2015
|
|
|
2097
|
-
##
|
|
2016
|
+
## 7.x to 8.x changes
|
|
2017
|
+
|
|
2018
|
+
- Upgraded to tedious version 14
|
|
2019
|
+
- Removed internal library for connection string parsing. Connection strings can be resolved using the static method `parseConnectionString` on ConnectionPool
|
|
2020
|
+
|
|
2021
|
+
## 6.x to 7.x changes
|
|
2098
2022
|
|
|
2099
|
-
- Upgraded tedious version to
|
|
2023
|
+
- Upgraded tedious version to v11
|
|
2024
|
+
- Upgraded msnodesqlv8 version support to v2
|
|
2025
|
+
- Upgraded tarn.js version to v3
|
|
2100
2026
|
- Requests in stream mode that pipe into other streams no longer pass errors up the stream chain
|
|
2027
|
+
- Request.pipe now pipes a true node stream for better support of backpressure
|
|
2101
2028
|
- tedious config option `trustServerCertificate` defaults to `false` if not supplied
|
|
2029
|
+
- Dropped support for Node < 10
|
|
2102
2030
|
|
|
2103
2031
|
## 5.x to 6.x changes
|
|
2104
2032
|
|
package/bin/mssql
CHANGED
|
File without changes
|
|
@@ -7,7 +7,7 @@ const tarn = require('tarn')
|
|
|
7
7
|
const { IDS } = require('../utils')
|
|
8
8
|
const ConnectionError = require('../error/connection-error')
|
|
9
9
|
const shared = require('../shared')
|
|
10
|
-
const
|
|
10
|
+
const clone = require('rfdc/default')
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Class ConnectionPool.
|
|
@@ -44,7 +44,7 @@ class ConnectionPool extends EventEmitter {
|
|
|
44
44
|
|
|
45
45
|
if (typeof config === 'string') {
|
|
46
46
|
try {
|
|
47
|
-
this.config = this.parseConnectionString(config)
|
|
47
|
+
this.config = this.constructor.parseConnectionString(config)
|
|
48
48
|
} catch (ex) {
|
|
49
49
|
if (typeof callback === 'function') {
|
|
50
50
|
return setImmediate(callback, ex)
|
|
@@ -52,7 +52,7 @@ class ConnectionPool extends EventEmitter {
|
|
|
52
52
|
throw ex
|
|
53
53
|
}
|
|
54
54
|
} else {
|
|
55
|
-
this.config =
|
|
55
|
+
this.config = clone(config)
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// set defaults
|
|
@@ -85,11 +85,11 @@ class ConnectionPool extends EventEmitter {
|
|
|
85
85
|
return this._healthy
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
parseConnectionString (connectionString) {
|
|
88
|
+
static parseConnectionString (connectionString) {
|
|
89
89
|
return this._parseConnectionString(connectionString)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
_parseConnectionString (connectionString) {
|
|
92
|
+
static _parseConnectionString (connectionString) {
|
|
93
93
|
const parsed = parseSqlConnectionString(connectionString, true, true)
|
|
94
94
|
return Object.entries(parsed).reduce((config, [key, value]) => {
|
|
95
95
|
switch (key) {
|
|
@@ -388,41 +388,6 @@ class ConnectionPool extends EventEmitter {
|
|
|
388
388
|
propagateCreateError: true
|
|
389
389
|
}, this.config.pool)
|
|
390
390
|
)
|
|
391
|
-
const self = this
|
|
392
|
-
Object.defineProperties(this.pool, {
|
|
393
|
-
size: {
|
|
394
|
-
get: () => {
|
|
395
|
-
const message = 'the `size` property on pool is deprecated, access it directly on the `ConnectionPool`'
|
|
396
|
-
self.emit('debug', message)
|
|
397
|
-
process.emitWarning(message)
|
|
398
|
-
return self.size
|
|
399
|
-
}
|
|
400
|
-
},
|
|
401
|
-
available: {
|
|
402
|
-
get: () => {
|
|
403
|
-
const message = 'the `available` property on pool is deprecated, access it directly on the `ConnectionPool`'
|
|
404
|
-
self.emit('debug', message)
|
|
405
|
-
process.emitWarning(message)
|
|
406
|
-
return self.available
|
|
407
|
-
}
|
|
408
|
-
},
|
|
409
|
-
pending: {
|
|
410
|
-
get: () => {
|
|
411
|
-
const message = 'the `pending` property on pool is deprecate, access it directly on the `ConnectionPool`'
|
|
412
|
-
self.emit('debug', message)
|
|
413
|
-
process.emitWarning(message)
|
|
414
|
-
return self.pending
|
|
415
|
-
}
|
|
416
|
-
},
|
|
417
|
-
borrowed: {
|
|
418
|
-
get: () => {
|
|
419
|
-
const message = 'the `borrowed` property on pool is deprecated, access it directly on the `ConnectionPool`'
|
|
420
|
-
self.emit('debug', message)
|
|
421
|
-
process.emitWarning(message)
|
|
422
|
-
return self.borrowed
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
})
|
|
426
391
|
|
|
427
392
|
this._connecting = false
|
|
428
393
|
this._connected = true
|
package/lib/base/index.js
CHANGED
|
@@ -54,6 +54,13 @@ Object.defineProperty(module.exports, 'Promise', {
|
|
|
54
54
|
}
|
|
55
55
|
})
|
|
56
56
|
|
|
57
|
+
Object.defineProperty(module.exports, 'valueHandler', {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
value: shared.valueHandler,
|
|
60
|
+
writable: false,
|
|
61
|
+
configurable: false
|
|
62
|
+
})
|
|
63
|
+
|
|
57
64
|
for (const key in TYPES) {
|
|
58
65
|
const value = TYPES[key]
|
|
59
66
|
module.exports.exports[key] = value
|