connManager.js 1.4 KB

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