All files database.js

81.25% Statements 65/80
83.33% Branches 10/12
66.66% Functions 6/9
81.25% Lines 65/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x         1x 1x 1x     1x 2x 2x     2x 2x 4x 4x 2x 2x 2x 2x 2x 1x 4x 4x 4x 4x 1x         1x 3x 3x 2x 2x 1x 1x 1x 3x 3x 3x 1x       1x 1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Database = void 0;
const fs_1 = require("fs");
const util_1 = require("util");
const logger_1 = require("./logger");
class Database {
    constructor(_filePath) {
        this._filePath = _filePath;
        this._changed = false;
        this._plogger = logger_1.LoggerFactory.getInstance().createLogger('address_book');
        this._data = new Map();
    }
    load(defaultData = new Map()) {
        this._plogger.debug('load()');
        if ((0, fs_1.existsSync)(this._filePath)) {
            const jsonObject = JSON.parse((0, fs_1.readFileSync)(this._filePath, 'utf8'));
            const entries = Object.entries(jsonObject);
            entries.forEach(([key, value]) => {
                // console.log('load A', key, value);
                const obj = new this._typex();
                obj.fromJSON(value, key);
                // console.log('load Ba', obj.toString());
                // console.log('load Bb', typeof obj);
                this._data.set(key, obj);
                // console.log('load Z', this._data.get(key as K));
            });
            this._plogger.debug((0, util_1.format)('load() - %s', this._data));
            this._changed = false;
        }
        else {
            this._data = defaultData;
            this._changed = true;
        }
        this.postLoad();
    }
    postLoad() {
        this._plogger.debug('postLoad()');
    }
    save() {
        this._plogger.debug('save()', this._changed);
        if (!this._changed) {
            return;
        }
        const _map = new Map();
        for (const [_key, _val] of this._data.entries()) {
            _map.set(_key, _val.toJSON());
        }
        const entries = Array.from(_map.entries());
        const obj = Object.fromEntries(entries);
        (0, fs_1.writeFileSync)(this._filePath, JSON.stringify(obj, null, 4));
        this._changed = false;
    }
    add(key, value) {
        this._plogger.debug((0, util_1.format)('add(%s, %s)', key, value));
        this._data.set(key, value);
        this._changed = true;
    }
    remove(key) {
        this._plogger.debug((0, util_1.format)('remove(%s)', key));
        this._data.delete(key);
        this._changed = true;
    }
    get(key) {
        this._plogger.debug((0, util_1.format)('get(%s)', key));
        if (this._data.has(key)) {
            return this._data.get(key);
        }
        return null;
    }
    getAll() {
        this._plogger.debug('getAll()');
        return this._data;
    }
    changed() {
        this._plogger.debug('changed()');
        this._changed = true;
    }
}
exports.Database = Database;