UNPKG

@gecogvidanto/plugin-nedb

Version:

Nebd local database management plugin for ĞecoĞvidanto

88 lines 3.78 kB
"use strict"; /* * This file is part of @gecogvidanto/plugin-nedb. * Copyright (C) 2020 Stéphane Veyret * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const Nedb = require("nedb"); const os_1 = require("os"); const path_1 = require("path"); const util_1 = require("util"); const winston = require("winston"); const bindata_1 = require("./bindata"); const NedbDatastore_1 = require("./NedbDatastore"); const NedbPromiseDatastore_1 = require("./NedbPromiseDatastore"); const tools_1 = require("./tools"); const DEFAULT_DIRECTORY = path_1.resolve(os_1.homedir(), '.config/gecogvidanto/'); const INTERNAL_SUBDIRECTORY = '_internal'; const BINARY_STORE = 'binary'; /** * The nedb database. */ class NedbDatabase { constructor(desc, nedbConfig, lang) { if (nedbConfig.directory.trim().length === 0) { winston.info(lang.nedb$defaultDirectory(DEFAULT_DIRECTORY)); } const directory = nedbConfig.directory.trim().length === 0 ? DEFAULT_DIRECTORY : nedbConfig.directory; // Prepare binary store const internalPath = path_1.resolve(directory, INTERNAL_SUBDIRECTORY); const binaryDb = new NedbPromiseDatastore_1.default(new Nedb(path_1.resolve(internalPath, BINARY_STORE + '.nedb'))); this.binaryStore = new bindata_1.BinaryStore(this, binaryDb, internalPath, nedbConfig.compactInterval); // Prepare models const initModels = {}; const initDb = {}; for (const name in desc) { const db = new NedbPromiseDatastore_1.default(new Nedb(path_1.resolve(directory, name + '.nedb'))); initModels[name] = new desc[name](new NedbDatastore_1.default(db, name, nedbConfig.compactInterval, this.binaryStore)); initDb[name] = db; } this.models = initModels; this.dbs = initDb; // Initialize database this.started = new Promise((resolve, reject) => { tools_1.exists(directory) .then(dirExists => { return dirExists ? Promise.resolve() : util_1.promisify(fs_1.mkdir)(directory); }) .then(() => tools_1.exists(internalPath)) .then(dirExists => { return dirExists ? Promise.resolve() : util_1.promisify(fs_1.mkdir)(internalPath); }) .then(() => binaryDb.loadDatabase()) .then(() => Promise.all(Object.keys(desc).map(name => this.startDatastore(this.dbs[name], this.models[name])))) .then(() => this.binaryStore.start()) .then(() => { winston.verbose(lang.nedb$databaseLoaded()); resolve(); }) .catch(err => { reject(err); }); }); } waitForStarted() { return this.started; } async startDatastore(db, model) { await db.loadDatabase(); await Promise.all(model.indices.map(index => db.ensureIndex(index))); await model.init(); } } exports.default = NedbDatabase; //# sourceMappingURL=NedbDatabase.js.map