From a35664d4dd80b3e4efefd7c33409bdad921951ff Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Tue, 7 Jul 2020 10:59:56 +1200 Subject: [PATCH] Stub console log during db initialization (#2351) --- .../insomnia-app/app/__jest__/before-each.js | 2 +- packages/insomnia-app/app/common/database.js | 25 +++++++++++-------- .../insomnia-app/app/common/send-request.js | 2 +- packages/insomnia-app/app/models/response.js | 10 ++++---- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/insomnia-app/app/__jest__/before-each.js b/packages/insomnia-app/app/__jest__/before-each.js index 9742eb88cf..7f2189fea9 100644 --- a/packages/insomnia-app/app/__jest__/before-each.js +++ b/packages/insomnia-app/app/__jest__/before-each.js @@ -6,5 +6,5 @@ export async function globalBeforeEach() { // Setup the local database in case it's used fetch.setup('insomnia-tests', 'http://localhost:8000'); - await db.init(models.types(), { inMemoryOnly: true }, true); + await db.init(models.types(), { inMemoryOnly: true }, true, () => {}); } diff --git a/packages/insomnia-app/app/common/database.js b/packages/insomnia-app/app/common/database.js index e34ba8aede..0de26f0de4 100644 --- a/packages/insomnia-app/app/common/database.js +++ b/packages/insomnia-app/app/common/database.js @@ -41,7 +41,12 @@ export async function initClient() { console.log('[db] Initialized DB client'); } -export async function init(types: Array, config: Object = {}, forceReset: boolean = false) { +export async function init( + types: Array, + config: Object = {}, + forceReset: boolean = false, + consoleLog: () => void = console.log, +) { if (forceReset) { changeListeners = []; for (const attr of Object.keys(db)) { @@ -56,7 +61,7 @@ export async function init(types: Array, config: Object = {}, forceReset // Fill in the defaults for (const modelType of types) { if (db[modelType]) { - console.log(`[db] Already initialized DB.${modelType}`); + consoleLog(`[db] Already initialized DB.${modelType}`); continue; } @@ -96,7 +101,7 @@ export async function init(types: Array, config: Object = {}, forceReset } if (!config.inMemoryOnly) { - console.log(`[db] Initialized DB at ${getDBFilePath('$TYPE')}`); + consoleLog(`[db] Initialized DB at ${getDBFilePath('$TYPE')}`); } // This isn't the best place for this but w/e @@ -111,25 +116,25 @@ export async function init(types: Array, config: Object = {}, forceReset if (type === CHANGE_REMOVE && typeof m.hookRemove === 'function') { try { - await m.hookRemove(doc); + await m.hookRemove(doc, consoleLog); } catch (err) { - console.log(`[db] Delete hook failed for ${type} ${doc._id}: ${err.message}`); + consoleLog(`[db] Delete hook failed for ${type} ${doc._id}: ${err.message}`); } } if (type === CHANGE_INSERT && typeof m.hookInsert === 'function') { try { - await m.hookInsert(doc); + await m.hookInsert(doc, consoleLog); } catch (err) { - console.log(`[db] Insert hook failed for ${type} ${doc._id}: ${err.message}`); + consoleLog(`[db] Insert hook failed for ${type} ${doc._id}: ${err.message}`); } } if (type === CHANGE_UPDATE && typeof m.hookUpdate === 'function') { try { - await m.hookUpdate(doc); + await m.hookUpdate(doc, consoleLog); } catch (err) { - console.log(`[db] Update hook failed for ${type} ${doc._id}: ${err.message}`); + consoleLog(`[db] Update hook failed for ${type} ${doc._id}: ${err.message}`); } } } @@ -137,7 +142,7 @@ export async function init(types: Array, config: Object = {}, forceReset for (const model of models.all()) { if (typeof model.hookDatabaseInit === 'function') { - await model.hookDatabaseInit(); + await model.hookDatabaseInit(consoleLog); } } } diff --git a/packages/insomnia-app/app/common/send-request.js b/packages/insomnia-app/app/common/send-request.js index 3110ef3903..0a2d5d5ded 100644 --- a/packages/insomnia-app/app/common/send-request.js +++ b/packages/insomnia-app/app/common/send-request.js @@ -5,7 +5,7 @@ import { getBodyBuffer } from '../models/response'; export async function getSendRequestCallbackMemDb(environmentId, memDB) { // Initialize the DB in-memory and fill it with data if we're given one - await db.init(modelTypes(), { inMemoryOnly: true }, true); + await db.init(modelTypes(), { inMemoryOnly: true }, true, () => {}); const docs = []; for (const type of Object.keys(memDB)) { diff --git a/packages/insomnia-app/app/models/response.js b/packages/insomnia-app/app/models/response.js index 56733205c6..b27d6ef582 100644 --- a/packages/insomnia-app/app/models/response.js +++ b/packages/insomnia-app/app/models/response.js @@ -85,19 +85,19 @@ export async function migrate(doc: Object) { return doc; } -export function hookDatabaseInit() { - console.log('Init responses DB'); +export function hookDatabaseInit(consoleLog: () => void = console.log) { + consoleLog('Init responses DB'); process.nextTick(async () => { await models.response.cleanDeletedResponses(); }); } -export function hookRemove(doc: Response) { +export function hookRemove(doc: Response, consoleLog: () => void = console.log) { fs.unlink(doc.bodyPath, () => { - console.log(`[response] Delete body ${doc.bodyPath}`); + consoleLog(`[response] Delete body ${doc.bodyPath}`); }); fs.unlink(doc.timelinePath, () => { - console.log(`[response] Delete timeline ${doc.timelinePath}`); + consoleLog(`[response] Delete timeline ${doc.timelinePath}`); }); }