How to use the ConnectionPool function from mssql

Find comprehensive JavaScript mssql.ConnectionPool code examples handpicked from public code repositorys.

mssql.ConnectionPool creates a pool of connections to a Microsoft SQL Server database.

56
57
58
59
60
61
62
63
64
    '      [key] NVARCHAR(100) PRIMARY KEY,' +
    '      [value] NTEXT NOT NULL' +
    '    );' +
    '  END';

new mssql.ConnectionPool(this.settings).connect().then((pool) => {
  this.db = pool;

  const request = new mssql.Request(this.db);
fork icon89
star icon257
watch icon19

+ 7 other calls in file

164
165
166
167
168
169
170
171
172
173
} else {
    if (password){
        config.password = password;
    }

    var connection = new mssql.ConnectionPool(config);
    connection.connstr = connstr;
    connection.on('error', function(err){
        err_callback(id, err);
    });
fork icon61
star icon812
watch icon39

+ 22 other calls in file

How does mssql.ConnectionPool work?

mssql.ConnectionPool is a class in the mssql module that creates a pool of reusable connections to a Microsoft SQL Server database, enabling multiple concurrent database connections to be made and managed more efficiently. When a request for a connection is made, it is allocated a connection from the pool, and once the request is complete, the connection is returned to the pool to be reused.

62
63
64
65
66
67
68
69
70
71
dbConfig.requestTimeout = 600000;
dbConfig.port = parseInt(dbConfig.port);
dbConfig.options = {};
dbConfig.options.encrypt = dbConfig.encrypt || false;
this.dbConfig = dbConfig;
this.pool = new mssql.ConnectionPool(dbConfig);
var that = this;
this.pool.connect(err => {
    if (err) {
        console.error('Database config: ', dbConfig);
fork icon41
star icon260
watch icon10

+ 29 other calls in file

2
3
4
5
6
7
8
9
10
11
const { BaseAdapter } = require('./base.js');
const MssqlQueryBuilder = require('../core/MssqlQueryBuilder.js');

class MssqlAdapter extends BaseAdapter {
    getPool () {
        const pool = new mssql.ConnectionPool({
            server: this.params.host,
            port: Number(this.params.port),
            database: this.params.database,
            user: this.params.user,
fork icon42
star icon100
watch icon16

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const sql = require("mssql");

async function example() {
  try {
    const pool = await sql.connect(
      "mssql://username:password@localhost/database"
    );
    const result = await pool
      .request()
      .input("input_parameter", sql.Int, 1)
      .query("SELECT * FROM myTable WHERE id = @input_parameter");
    console.log(result.recordset);
  } catch (err) {
    console.log(err);
  }
}

In this example, a ConnectionPool is created using sql.connect() and is used to make a query to a database. The results of the query are logged to the console.

22
23
24
25
26
27
28
29
30
31
        enableArithAbort: true,
        encrypt: false // antes estava a true. Robson reportó que en docker no funcionava bien.
    }
}

const client = new SQLservice.ConnectionPool(config);

return new Promise((resolve, reject) => {
    client.connect((err, conn) => {
        if (err) {
fork icon10
star icon70
watch icon6

+ 5 other calls in file

80
81
82
83
84
85
86
87
88
89
MsSQL.prototype.connect = function(callback) {
  const self = this;
  if (self.client) {
    return process.nextTick(callback);
  }
  const connection = new mssql.ConnectionPool(this.connConfig, function(err) {
    if (err) {
      debug('Connection error: ', err);
      return callback(err);
    }
fork icon84
star icon51
watch icon41

+ 15 other calls in file

92
93
94
95
96
97
98
99
100
101


const runMigrations = async (version) => {
  try {
    await createDatabaseIfNotExists()
    const client = new mssql.ConnectionPool({
      server: config.Sql.Server,
      database: config.Sql.Database,
      user: config.Sql.Migrator.Username,
      password: config.Sql.Migrator.Password,
fork icon14
star icon12
watch icon11

58
59
60
61
62
63
64
65
66

let incomplete;
const rows = [];

return new Promise((resolve, reject) => {
  const pool = new mssql.ConnectionPool(config, (err) => {
    if (err) {
      return reject(err);
    }
fork icon759
star icon0
watch icon160

+ 5 other calls in file

45
46
47
48
49
50
51
52
53
54
                        failReturn(err);
                }
        );
} else {
// DEFAULT - Conexao com pool
        new sql.ConnectionPool(__serverConfig.db.msSqlServer.configDb).connect()
        .then(
                pool => {
                        return new sql.Transaction(pool);
                }
fork icon0
star icon7
watch icon1

+ 5 other calls in file

351
352
353
354
355
356
357
358
359
360
361
    res.json(results.recordset);
  });
});


app.get('/select-custom-pool', (req, res) => {
  const customPool = new sql.ConnectionPool(connectConfig, err1 => {
    if (err1) {
      log('Failed to create a connection pool.', err1);
      return res.status(500).json(err1);
    }
fork icon34
star icon66
watch icon0

1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
		pools_cache[self.$conn] = self.options;
} else
	self.options = pools_cache[self.$conn];

//self.db = new database.connect(self.options, function(err) {
self.db = new database.ConnectionPool(self.options, function(err) {
	if (err) {
		if (!self.errors)
			self.errors = self.isErrorBuilder ? new global.ErrorBuilder() : [];
		self.errors.push(err);
fork icon22
star icon48
watch icon0

20
21
22
23
24
25
26
27
28
29
    logger.warn(`cannot create connectionPool for role:${roleName} readonly:${readonly}, as it already exists`)
    return existingPool
  } else {
    const config = connectionBuilder.build(roleName, readonly)
    const internalPoolName = buildInternalPoolName(roleName, readonly)
    POOLS[internalPoolName] = new ConnectionPool(config)
    return POOLS[internalPoolName]
  }
},
/**
fork icon14
star icon12
watch icon11

+ 67 other calls in file

34
35
36
37
38
39
40
41
42
43
	});
	break;

case "mssql":
default:
	client = new mssql.ConnectionPool(database.connection);
	postgrator = new Postgrator({
		migrationPattern: path.joinSafe(
			__dirname,
			"../migrations/mssql/*"
fork icon2
star icon11
watch icon0

27
28
29
30
31
32
33
34
35
36
37
        encrypt: true, // for azure
        trustServerCertificate: false // change to true for local dev / self-signed certs
    }
}


const appPool = new sql.ConnectionPool(config)
try {
    appPool.connect()
}
catch (error) {
fork icon1
star icon2
watch icon0

11
12
13
14
15
16
17
18
19
20
21
22
    }
};




// Connect to the database
const poolPromise = new sql.ConnectionPool(config)
    .connect()
    .then(pool => {
        console.log('Connected to the database');
        return pool;
fork icon0
star icon1
watch icon0

55
56
57
58
59
60
61
62
63
64
    console.log('stream in progress')
    return false
  }
}

const pool = new sql.ConnectionPool(db02).connect().then(pool => {
  return pool.query`select orderId from linxapi.dbo.order_import_header where ownerid = 'cokem'`
}).then(results => {
  linxapiOrderIds = results.recordset.map(id => {
    return id.orderId
fork icon0
star icon1
watch icon1

36
37
38
39
40
41
42
43
44
45
        db: 'klasa',
        user: 'database-user',
        password: 'database-password',
        options: { encrypt: false }
}, this.client.options.providers.mssql);
this.pool = new mssql.ConnectionPool({
        user: connection.user,
        password: connection.password,
        server: connection.host,
        database: connection.database,
fork icon56
star icon0
watch icon2

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
    options: {
        encrypt: false,
        enableArithAbort: false
    },
};
const pool = new mssql.ConnectionPool(config);

router.get('/search', async (req, res) => {
    try {
        await pool.connect();
fork icon7
star icon10
watch icon2

63
64
65
66
67
68
69
70
71
72
// SQL database connection
if (config.useMsSqlServer == true) {
    var sqLiteDb = null
    
    // Single MS-SQL connection pool accross every route
    var msSqlPoolPromise = new sql.ConnectionPool(config.sqlLogin)
    .connect()
    .then(pool => {
        console.log('* Connected to MS-SQL!')
        return pool
fork icon9
star icon16
watch icon2

+ 11 other calls in file

34
35
36
37
38
39
40
41
42
43
  }
  const serial = currentPupilIndex.toString().padStart(3, '0')
  return upnService.calculateCheckLetter(baseUpn + serial) + baseUpn + serial
}

const pool = new sql.ConnectionPool(config.Sql)
pool.connect()
  .then(async () => {
    // schools
    console.log('connected')
fork icon14
star icon12
watch icon11

+ 3 other calls in file