Skip to content

Commit 10206d9

Browse files
committed
DB: Clean up database initialization
1 parent f22fb13 commit 10206d9

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

src/node/db/DB.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ const ueberDB = require('ueberdb2');
2525
const settings = require('../utils/Settings');
2626
const log4js = require('log4js');
2727
const stats = require('../stats');
28-
const util = require('util');
2928

30-
// set database settings
31-
const db =
32-
new ueberDB.Database(settings.dbType, settings.dbSettings, null, log4js.getLogger('ueberDB'));
29+
const logger = log4js.getLogger('ueberDB');
3330

3431
/**
3532
* The UeberDB Object that provides the database functions
@@ -38,49 +35,26 @@ exports.db = null;
3835

3936
/**
4037
* Initializes the database with the settings provided by the settings module
41-
* @param {Function} callback
4238
*/
43-
exports.init = async () => await new Promise((resolve, reject) => {
44-
db.init((err) => {
45-
if (err) {
46-
// there was an error while initializing the database, output it and stop
47-
console.error('ERROR: Problem while initalizing the database');
48-
console.error(err.stack ? err.stack : err);
49-
process.exit(1);
39+
exports.init = async () => {
40+
exports.db = new ueberDB.Database(settings.dbType, settings.dbSettings, null, logger);
41+
await exports.db.init();
42+
if (exports.db.metrics != null) {
43+
for (const [metric, value] of Object.entries(exports.db.metrics)) {
44+
if (typeof value !== 'number') continue;
45+
stats.gauge(`ueberdb_${metric}`, () => exports.db.metrics[metric]);
5046
}
51-
52-
if (db.metrics != null) {
53-
for (const [metric, value] of Object.entries(db.metrics)) {
54-
if (typeof value !== 'number') continue;
55-
stats.gauge(`ueberdb_${metric}`, () => db.metrics[metric]);
56-
}
57-
}
58-
59-
// everything ok, set up Promise-based methods
60-
['get', 'set', 'findKeys', 'getSub', 'setSub', 'remove'].forEach((fn) => {
61-
exports[fn] = util.promisify(db[fn].bind(db));
62-
});
63-
64-
// set up wrappers for get and getSub that can't return "undefined"
65-
const get = exports.get;
66-
exports.get = async (key) => {
67-
const result = await get(key);
68-
return (result === undefined) ? null : result;
69-
};
70-
71-
const getSub = exports.getSub;
72-
exports.getSub = async (key, sub) => {
73-
const result = await getSub(key, sub);
74-
return (result === undefined) ? null : result;
75-
};
76-
77-
// exposed for those callers that need the underlying raw API
78-
exports.db = db;
79-
resolve();
80-
});
81-
});
47+
}
48+
for (const fn of ['get', 'set', 'findKeys', 'getSub', 'setSub', 'remove']) {
49+
const f = exports.db[fn];
50+
exports[fn] = async (...args) => await f.call(exports.db, ...args);
51+
Object.setPrototypeOf(exports[fn], Object.getPrototypeOf(f));
52+
Object.defineProperties(exports[fn], Object.getOwnPropertyDescriptors(f));
53+
}
54+
};
8255

8356
exports.shutdown = async (hookName, context) => {
84-
await util.promisify(db.close.bind(db))();
85-
console.log('Database closed');
57+
if (exports.db != null) await exports.db.close();
58+
exports.db = null;
59+
logger.log('Database closed');
8660
};

0 commit comments

Comments
 (0)