Stub console log during db initialization (#2351)

This commit is contained in:
Opender Singh
2020-07-07 10:59:56 +12:00
committed by GitHub
parent dcd182caf1
commit a35664d4dd
4 changed files with 22 additions and 17 deletions

View File

@@ -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, () => {});
}

View File

@@ -41,7 +41,12 @@ export async function initClient() {
console.log('[db] Initialized DB client');
}
export async function init(types: Array<string>, config: Object = {}, forceReset: boolean = false) {
export async function init(
types: Array<string>,
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<string>, 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<string>, 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<string>, 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<string>, config: Object = {}, forceReset
for (const model of models.all()) {
if (typeof model.hookDatabaseInit === 'function') {
await model.hookDatabaseInit();
await model.hookDatabaseInit(consoleLog);
}
}
}

View File

@@ -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)) {

View File

@@ -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}`);
});
}