|
1 | 1 | const Sequelize = require('sequelize'); |
2 | 2 |
|
3 | | -const databaseOptions = { |
4 | | - logging: false, |
5 | | - pool: { maxConnections: 10, minConnections: 1 }, |
6 | | -}; |
| 3 | +class ConnectionManager { |
| 4 | + constructor(connectionString) { |
| 5 | + this.connectionString = connectionString; |
| 6 | + this.databaseOptions = { |
| 7 | + logging: false, |
| 8 | + pool: { maxConnections: 10, minConnections: 1 }, |
| 9 | + }; |
| 10 | + this.connection = null; |
| 11 | + } |
| 12 | + |
| 13 | + getDialect() { |
| 14 | + return this.connection && this.connection.options && this.connection.options.dialect; |
| 15 | + } |
7 | 16 |
|
8 | | -const sequelizePostgres = new Sequelize( |
9 | | - 'postgres://forest:secret@localhost:5436/forest-express-sequelize-test', |
10 | | - databaseOptions, |
11 | | -); |
| 17 | + getPort() { |
| 18 | + return this.connection && this.connection.options && this.connection.options.port; |
| 19 | + } |
12 | 20 |
|
13 | | -const sequelizeMySQLMin = new Sequelize( |
14 | | - 'mysql://forest:secret@localhost:8998/forest-express-sequelize-test', |
15 | | - databaseOptions, |
16 | | -); |
| 21 | + createConnection() { |
| 22 | + if (!this.connection) { |
| 23 | + this.connection = new Sequelize(this.connectionString, this.databaseOptions); |
| 24 | + } |
| 25 | + return this.connection; |
| 26 | + } |
17 | 27 |
|
18 | | -const sequelizeMySQLMax = new Sequelize( |
19 | | - 'mysql://forest:secret@localhost:8999/forest-express-sequelize-test', |
20 | | - databaseOptions, |
21 | | -); |
| 28 | + closeConnection() { |
| 29 | + if (this.connection) { |
| 30 | + this.connection.close(); |
| 31 | + this.connection = null; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
22 | 35 |
|
23 | 36 | module.exports = { |
24 | | - sequelizePostgres, |
25 | | - sequelizeMySQLMin, |
26 | | - sequelizeMySQLMax, |
| 37 | + sequelizePostgres: new ConnectionManager('postgres://forest:secret@localhost:5436/forest-express-sequelize-test'), |
| 38 | + sequelizeMySQLMin: new ConnectionManager('mysql://forest:secret@localhost:8998/forest-express-sequelize-test'), |
| 39 | + sequelizeMySQLMax: new ConnectionManager('mysql://forest:secret@localhost:8999/forest-express-sequelize-test'), |
27 | 40 | }; |
0 commit comments