connManager.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 = null;//(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. if (applied.length)
  24. log(`${applied.length} migrations applied to "${poolConfig.poolName}"`);
  25. }
  26. this._pool[poolConfig.poolName] = connPool;
  27. }
  28. }
  29. get pool() {
  30. return this._pool;
  31. }
  32. }
  33. const connManager = new ConnManager();
  34. module.exports = connManager;