|
@@ -5,10 +5,7 @@ const ayncExit = new (require('../core/AsyncExit'))();//singleton
|
|
const { JembaDb, JembaDbThread } = require('./JembaDb');
|
|
const { JembaDb, JembaDbThread } = require('./JembaDb');
|
|
const log = new (require('../core/AppLogger'))().log;//singleton
|
|
const log = new (require('../core/AppLogger'))().log;//singleton
|
|
|
|
|
|
-const jembaMigrations = {
|
|
|
|
- //'app': require('./jembaMigrations/app'),
|
|
|
|
- 'reader-storage': require('./jembaMigrations/reader-storage'),
|
|
|
|
-};
|
|
|
|
|
|
+const jembaMigrations = require('./jembaMigrations');
|
|
|
|
|
|
let instance = null;
|
|
let instance = null;
|
|
|
|
|
|
@@ -17,6 +14,7 @@ class JembaConnManager {
|
|
constructor() {
|
|
constructor() {
|
|
if (!instance) {
|
|
if (!instance) {
|
|
this.inited = false;
|
|
this.inited = false;
|
|
|
|
+ this.closed = false;
|
|
|
|
|
|
instance = this;
|
|
instance = this;
|
|
}
|
|
}
|
|
@@ -24,7 +22,7 @@ class JembaConnManager {
|
|
return instance;
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
|
|
- async init(config) {
|
|
|
|
|
|
+ async init(config, migs = jembaMigrations) {
|
|
if (this.inited)
|
|
if (this.inited)
|
|
throw new Error('JembaConnManager initialized already');
|
|
throw new Error('JembaConnManager initialized already');
|
|
|
|
|
|
@@ -52,7 +50,6 @@ class JembaConnManager {
|
|
|
|
|
|
log(`Open "${dbConfig.dbName}" start`);
|
|
log(`Open "${dbConfig.dbName}" start`);
|
|
await dbConn.openDb({dbPath, cacheSize: dbConfig.cacheSize, compressed: dbConfig.compressed, forceFileClosing: true});
|
|
await dbConn.openDb({dbPath, cacheSize: dbConfig.cacheSize, compressed: dbConfig.compressed, forceFileClosing: true});
|
|
- ayncExit.add(dbConn.closeDb.bind(dbConn));
|
|
|
|
|
|
|
|
if (dbConfig.openAll) {
|
|
if (dbConfig.openAll) {
|
|
try {
|
|
try {
|
|
@@ -77,7 +74,7 @@ class JembaConnManager {
|
|
log(`Open "${dbConfig.dbName}" finish`);
|
|
log(`Open "${dbConfig.dbName}" finish`);
|
|
|
|
|
|
//миграции
|
|
//миграции
|
|
- const mig = jembaMigrations[dbConfig.dbName];
|
|
|
|
|
|
+ const mig = migs[dbConfig.dbName];
|
|
if (mig && mig.data) {
|
|
if (mig && mig.data) {
|
|
const applied = await this.migrate(dbConn, mig.data, mig.table, force);
|
|
const applied = await this.migrate(dbConn, mig.data, mig.table, force);
|
|
if (applied.length)
|
|
if (applied.length)
|
|
@@ -86,9 +83,27 @@ class JembaConnManager {
|
|
|
|
|
|
this._db[dbConfig.dbName] = dbConn;
|
|
this._db[dbConfig.dbName] = dbConn;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ ayncExit.add(this.close.bind(this));
|
|
|
|
+
|
|
this.inited = true;
|
|
this.inited = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ async close() {
|
|
|
|
+ if (!this.inited)
|
|
|
|
+ throw new Error('JembaConnManager not inited');
|
|
|
|
+
|
|
|
|
+ if (this.closed)
|
|
|
|
+ throw new Error('JembaConnManager closed');
|
|
|
|
+
|
|
|
|
+ for (const dbConfig of this.config.db) {
|
|
|
|
+ await this._db[dbConfig.dbName].closeDb();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._db = {};
|
|
|
|
+ this.closed = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
async migrate(db, migs, table, force) {
|
|
async migrate(db, migs, table, force) {
|
|
const migrations = _.cloneDeep(migs).sort((a, b) => a.id - b.id);
|
|
const migrations = _.cloneDeep(migs).sort((a, b) => a.id - b.id);
|
|
|
|
|
|
@@ -163,6 +178,12 @@ class JembaConnManager {
|
|
}
|
|
}
|
|
|
|
|
|
get db() {
|
|
get db() {
|
|
|
|
+ if (!this.inited)
|
|
|
|
+ throw new Error('JembaConnManager not inited');
|
|
|
|
+
|
|
|
|
+ if (this.closed)
|
|
|
|
+ throw new Error('JembaConnManager closed');
|
|
|
|
+
|
|
return this._db;
|
|
return this._db;
|
|
}
|
|
}
|
|
}
|
|
}
|