etherpad-lite/src/node/db/DB.js

61 lines
2 KiB
JavaScript
Raw Normal View History

'use strict';
2011-05-14 18:57:07 +01:00
/**
2021-02-03 00:30:07 +01:00
* The DB Module provides a database initialized with the settings
2011-05-30 15:53:11 +01:00
* provided by the settings module
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
2011-05-14 18:57:07 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-11-23 13:24:19 -05:00
const ueberDB = require('ueberdb2');
const settings = require('../utils/Settings');
const log4js = require('log4js');
2021-03-08 16:30:13 -05:00
const stats = require('../stats');
2011-05-14 18:57:07 +01:00
2022-04-19 17:48:44 -04:00
const logger = log4js.getLogger('ueberDB');
2011-05-14 18:57:07 +01:00
2011-05-30 15:53:11 +01:00
/**
* The UeberDB Object that provides the database functions
*/
exports.db = null;
2011-05-14 18:57:07 +01:00
2011-05-30 15:53:11 +01:00
/**
2021-02-03 00:30:07 +01:00
* Initializes the database with the settings provided by the settings module
2011-05-30 15:53:11 +01:00
*/
2022-04-19 17:48:44 -04:00
exports.init = async () => {
exports.db = new ueberDB.Database(settings.dbType, settings.dbSettings, null, logger);
await exports.db.init();
if (exports.db.metrics != null) {
for (const [metric, value] of Object.entries(exports.db.metrics)) {
if (typeof value !== 'number') continue;
stats.gauge(`ueberdb_${metric}`, () => exports.db.metrics[metric]);
}
2022-04-19 17:48:44 -04:00
}
for (const fn of ['get', 'set', 'findKeys', 'getSub', 'setSub', 'remove']) {
const f = exports.db[fn];
exports[fn] = async (...args) => await f.call(exports.db, ...args);
Object.setPrototypeOf(exports[fn], Object.getPrototypeOf(f));
Object.defineProperties(exports[fn], Object.getOwnPropertyDescriptors(f));
}
};
exports.shutdown = async (hookName, context) => {
2022-04-19 17:48:44 -04:00
if (exports.db != null) await exports.db.close();
exports.db = null;
logger.log('Database closed');
};