-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ test/typescript/*.js | |
test/typescript/*.map | ||
typings/ | ||
generated.js | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
const npm = { | ||
con: require('manakin').local, | ||
utils: require('./utils') | ||
}; | ||
|
||
/** | ||
* @class DatabasePool | ||
* @private | ||
*/ | ||
class DatabasePool { | ||
|
||
constructor() { | ||
this.dbMap = {}; // map of used databases | ||
this.dbs = []; // all database objects | ||
} | ||
|
||
/** | ||
* @method DatabasePool.register | ||
* @private | ||
* @description | ||
* - Registers each database object, to make sure no duplicates connections are used, | ||
* and if they are, produce a warning; | ||
* - Registers each Pool object, to be able to release them all when requested. | ||
* | ||
* @param {Database} db - The new Database object being registered. | ||
*/ | ||
register(db) { | ||
const cnKey = DatabasePool.normalizeConnection(db.$cn); | ||
if (cnKey in this.dbMap) { | ||
if (!db.$config.options.noWarnings) { | ||
npm.con.warn('WARNING: Creating a duplicate database object for the same connection.\n%s\n', | ||
npm.utils.getLocalStack(5)); | ||
} | ||
} else { | ||
this.dbMap[cnKey] = true; | ||
} | ||
this.dbs.push(db); | ||
} | ||
|
||
/** | ||
* @method DatabasePool.shutDown | ||
* @private | ||
*/ | ||
shutDown() { | ||
this.dbs.forEach(db => { | ||
db.$destroy(); | ||
}); | ||
this.dbs.length = 0; | ||
this.dbMap = {}; | ||
} | ||
|
||
/** | ||
* @method DatabasePool.normalizeConnection | ||
* @static | ||
* @private | ||
* @description | ||
* For connections that are objects it reorders the keys alphabetically, | ||
* and then serializes the result into a JSON string. | ||
* | ||
* @param {string|object} cn - connection string or object | ||
*/ | ||
static normalizeConnection(cn) { | ||
if (typeof cn === 'object') { | ||
const obj = {}, keys = Object.keys(cn).sort(); | ||
keys.forEach(name => { | ||
obj[name] = cn[name]; | ||
}); | ||
cn = obj; | ||
} | ||
return JSON.stringify(cn); | ||
} | ||
} | ||
|
||
module.exports = new DatabasePool(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters