Небольшой рефакторинг

This commit is contained in:
Book Pauk
2021-12-01 22:09:48 +07:00
parent 714eb3ae83
commit 14ca2daa39

View File

@@ -14,7 +14,6 @@ class JembaConnManager {
constructor() { constructor() {
if (!instance) { if (!instance) {
this.inited = false; this.inited = false;
this.closed = false;
instance = this; instance = this;
} }
@@ -22,15 +21,13 @@ class JembaConnManager {
return instance; return instance;
} }
async init(config, migs = jembaMigrations) { async init(config, migs = jembaMigrations, undoLastMigration = false) {
if (this.inited) if (this.inited)
throw new Error('JembaConnManager initialized already'); throw new Error('JembaConnManager initialized already');
this.config = config; this.config = config;
this._db = {}; this._db = {};
const force = null;//(config.branch == 'development' ? 'last' : null);
for (const dbConfig of this.config.db) { for (const dbConfig of this.config.db) {
const dbPath = `${this.config.dataDir}/db/${dbConfig.dbName}`; const dbPath = `${this.config.dataDir}/db/${dbConfig.dbName}`;
@@ -49,7 +46,7 @@ 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: dbConfig.forceFileClosing});
if (dbConfig.openAll) { if (dbConfig.openAll) {
try { try {
@@ -76,7 +73,7 @@ class JembaConnManager {
//миграции //миграции
const mig = migs[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, undoLastMigration);
if (applied.length) if (applied.length)
log(`${applied.length} migrations applied to "${dbConfig.dbName}"`); log(`${applied.length} migrations applied to "${dbConfig.dbName}"`);
} }
@@ -91,20 +88,17 @@ class JembaConnManager {
async close() { async close() {
if (!this.inited) if (!this.inited)
throw new Error('JembaConnManager not inited'); return;
if (this.closed)
throw new Error('JembaConnManager closed');
for (const dbConfig of this.config.db) { for (const dbConfig of this.config.db) {
await this._db[dbConfig.dbName].closeDb(); await this._db[dbConfig.dbName].closeDb();
} }
this._db = {}; this._db = {};
this.closed = true; this.inited = false;
} }
async migrate(db, migs, table, force) { async migrate(db, migs, table, undoLastMigration) {
const migrations = _.cloneDeep(migs).sort((a, b) => a.id - b.id); const migrations = _.cloneDeep(migs).sort((a, b) => a.id - b.id);
if (!migrations.length) { if (!migrations.length) {
@@ -144,11 +138,11 @@ class JembaConnManager {
}; };
// Undo migrations that exist only in the database but not in migs, // Undo migrations that exist only in the database but not in migs,
// also undo the last migration if the `force` option was set to `last`. // also undo the last migration if the undoLastMigration
const lastMigration = migrations[migrations.length - 1]; const lastMigration = migrations[migrations.length - 1];
for (const migration of dbMigrations.slice().sort((a, b) => b.id - a.id)) { for (const migration of dbMigrations.slice().sort((a, b) => b.id - a.id)) {
if (!migrations.some(x => x.id === migration.id) || if (!migrations.some(x => x.id === migration.id) ||
(force === 'last' && migration.id === lastMigration.id)) { (undoLastMigration && migration.id === lastMigration.id)) {
await execUpDown(migration.down); await execUpDown(migration.down);
await db.delete({ await db.delete({
table, table,
@@ -181,9 +175,6 @@ class JembaConnManager {
if (!this.inited) if (!this.inited)
throw new Error('JembaConnManager not inited'); throw new Error('JembaConnManager not inited');
if (this.closed)
throw new Error('JembaConnManager closed');
return this._db; return this._db;
} }
} }