mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
chore: Add migration helper functions
This commit is contained in:
35
migrations/utils/connect.js
Normal file
35
migrations/utils/connect.js
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient;
|
||||
const logger = require('./logger');
|
||||
|
||||
let db;
|
||||
|
||||
function connectToDb (dbUri) {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(dbUri, (err, database) => {
|
||||
if (err) {
|
||||
logger.error(`Uh oh... Problem connecting to the database at ${dbUri}`);
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
db = database;
|
||||
|
||||
logger.success(`Connected to ${dbUri}`);
|
||||
|
||||
resolve(database);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function closeDb () {
|
||||
if (db) db.close();
|
||||
|
||||
logger.success('CLosed connection to the database');
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
connectToDb,
|
||||
closeDb,
|
||||
}
|
||||
44
migrations/utils/timer.js
Normal file
44
migrations/utils/timer.js
Normal file
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
let logger = require('./logger');
|
||||
|
||||
class Timer {
|
||||
constructor (options) {
|
||||
options = options || {};
|
||||
let warningThreshold = options.minutesWarningThreshold || 10;
|
||||
|
||||
this.count = 0;
|
||||
this._minutesWarningThreshold = warningThreshold * 60;
|
||||
|
||||
if (!options.disableAutoStart) this.start();
|
||||
}
|
||||
start () {
|
||||
this._internalTimer = setInterval(() =>{
|
||||
this.count++;
|
||||
|
||||
let shouldWarn = this._minutesWarningThreshold < this.count;
|
||||
let logStyle = shouldWarn ? 'error' : 'warn';
|
||||
let dangerMessage = shouldWarn ? 'DANGER: ' : '';
|
||||
|
||||
if (this.count % 30 === 0) {
|
||||
logger[logStyle](`${dangerMessage}Process has been running for`, this.count / 60, 'minutes');
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
stop () {
|
||||
if (!this._internalTimer) {
|
||||
throw new Error('Timer has not started');
|
||||
}
|
||||
clearInterval(this._internalTimer);
|
||||
}
|
||||
|
||||
get seconds () {
|
||||
return this.count;
|
||||
}
|
||||
|
||||
get minutes () {
|
||||
return this.count / 60;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Timer;
|
||||
7
migrations/utils/unique.js
Normal file
7
migrations/utils/unique.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
function unique (array) {
|
||||
return Array.from(new Set(array));
|
||||
}
|
||||
|
||||
module.exports = unique;
|
||||
Reference in New Issue
Block a user