database-pool.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2015-present, Vitaly Tomilov
  3. *
  4. * See the LICENSE file at the top-level directory of this distribution
  5. * for licensing information.
  6. *
  7. * Removal or modification of this copyright notice is prohibited.
  8. */
  9. const {ColorConsole} = require('./utils/color');
  10. const npm = {
  11. utils: require('./utils')
  12. };
  13. /**
  14. * @class DatabasePool
  15. * @static
  16. * @private
  17. */
  18. class DatabasePool {
  19. /**
  20. * Global instance of the database pool repository.
  21. *
  22. * @returns {{dbMap: {}, dbs: Array}}
  23. */
  24. static get instance() {
  25. const s = Symbol.for('pgPromiseDatabasePool');
  26. let scope = global[s];
  27. if (!scope) {
  28. scope = {
  29. dbMap: {}, // map of used database context keys (connection + dc)
  30. dbs: [] // all database objects
  31. };
  32. global[s] = scope;
  33. }
  34. return scope;
  35. }
  36. /**
  37. * @method DatabasePool.register
  38. * @static
  39. * @description
  40. * - Registers each database object, to make sure no duplicates connections are used,
  41. * and if they are, produce a warning;
  42. * - Registers each Pool object, to be able to release them all when requested.
  43. *
  44. * @param {Database} db - The new Database object being registered.
  45. */
  46. static register(db) {
  47. const cnKey = DatabasePool.createContextKey(db);
  48. npm.utils.addReadProp(db, '$cnKey', cnKey, true);
  49. const {dbMap, dbs} = DatabasePool.instance;
  50. if (cnKey in dbMap) {
  51. dbMap[cnKey]++;
  52. /* istanbul ignore if */
  53. if (!db.$config.options.noWarnings) {
  54. ColorConsole.warn(`WARNING: Creating a duplicate database object for the same connection.\n${npm.utils.getLocalStack(4, 3)}\n`);
  55. }
  56. } else {
  57. dbMap[cnKey] = 1;
  58. }
  59. dbs.push(db);
  60. }
  61. /**
  62. * @method DatabasePool.unregister
  63. * @static
  64. * @param db
  65. */
  66. static unregister(db) {
  67. const cnKey = db.$cnKey;
  68. const {dbMap} = DatabasePool.instance;
  69. if (!--dbMap[cnKey]) {
  70. delete dbMap[cnKey];
  71. }
  72. }
  73. /**
  74. * @method DatabasePool.shutDown
  75. * @static
  76. */
  77. static shutDown() {
  78. const {instance} = DatabasePool;
  79. instance.dbs.forEach(db => {
  80. db.$destroy();
  81. });
  82. instance.dbs.length = 0;
  83. instance.dbMap = {};
  84. }
  85. /**
  86. * @method DatabasePool.createContextKey
  87. * @static
  88. * @description
  89. * For connections that are objects it reorders the keys alphabetically,
  90. * and then serializes the result into a JSON string.
  91. *
  92. * @param {Database} db - Database instance.
  93. */
  94. static createContextKey(db) {
  95. let cn = db.$cn;
  96. if (typeof cn === 'object') {
  97. const obj = {}, keys = Object.keys(cn).sort();
  98. keys.forEach(name => {
  99. obj[name] = cn[name];
  100. });
  101. cn = obj;
  102. }
  103. return npm.utils.toJson(npm.utils.getSafeConnection(cn)) + npm.utils.toJson(db.$dc);
  104. }
  105. }
  106. module.exports = {DatabasePool};