connManager.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const SqliteConnectionPool = require('./SqliteConnectionPool');
  2. const log = require('../core/getLogger').getLog();
  3. const migrations = {
  4. 'app': require('./migrations/app'),
  5. 'readerStorage': require('./migrations/readerStorage'),
  6. };
  7. class ConnManager {
  8. constructor() {
  9. this._pool = {};
  10. }
  11. async init(config) {
  12. this.config = config;
  13. const force = (config.branch == 'development' ? 'last' : null);
  14. for (const poolConfig of this.config.db) {
  15. const dbFileName = this.config.dataDir + '/' + poolConfig.fileName;
  16. const connPool = new SqliteConnectionPool();
  17. await connPool.open(poolConfig.connCount, dbFileName);
  18. log(`Opened database "${poolConfig.poolName}"`);
  19. //миграции
  20. const migs = migrations[poolConfig.poolName];
  21. if (migs && migs.data.length) {
  22. const applied = await connPool.migrate(migs.data, migs.table, force);
  23. log(`Applied ${applied.length} migrations to "${poolConfig.poolName}"`);
  24. }
  25. this._pool[poolConfig.poolName] = connPool;
  26. }
  27. }
  28. get pool() {
  29. return this._pool;
  30. }
  31. }
  32. const connManager = new ConnManager();
  33. module.exports = connManager;