All files / src Database.ts

100% Statements 26/26
100% Branches 9/9
100% Functions 6/6
100% Lines 26/26

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                              1x   1x   1x   1x       37x       5x 1x     4x 4x       2x 2x       3x   3x 3x   3x       2x 1x 1x     1x 1x       13x   13x 1x   12x 3x     13x                
// Copyright (C) 2017  Norman Breau
 
// 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 <http://www.gnu.org/licenses/>.
 
import * as UUID from 'uuid';
import {DatabaseConnection} from './DatabaseConnection';
import {getApplicationLogger} from './instance';
 
const MASTER_NAME: string = 'MASTER';
 
export abstract class Database {
    private clusterConfigMap: any;
 
    constructor() {
        this.clusterConfigMap = {};
    }
 
    public addMaster(config: any): void {
        if (this.clusterConfigMap[MASTER_NAME]) {
            throw new Error(`Node "${MASTER_NAME}" already exists.`);
        }
 
        this.clusterConfigMap[MASTER_NAME] = config;
        this._addNode(MASTER_NAME, config);
    }
 
    public removeMaster(): void {
        delete this.clusterConfigMap[MASTER_NAME];
        this._removeNode(MASTER_NAME);
    }
 
    public addSlave(slaveID: string, config: any): string {
        let id = `SLAVE.${UUID.v4()}.${slaveID}`;
 
        this.clusterConfigMap[id] = config;
        this._addNode(id, config);
 
        return id;
    }
 
    public removeSlave(slaveID: string): void {
        if (!this.clusterConfigMap[slaveID]) {
            getApplicationLogger().warn(`Node ${slaveID} is not a part of this cluster.`);
            return;
        }
 
        delete this.clusterConfigMap[slaveID];
        this._removeNode(slaveID);
    }
 
    public getConnection(requireWriteAccess: boolean = false, nodeID?: string): Promise<DatabaseConnection> {
        let query: string = 'SLAVE*';
        
        if (nodeID) {
            query = nodeID;
        }
        else if (requireWriteAccess) {
            query = 'MASTER';
        }
 
        return this._getConnection(query, requireWriteAccess);
    }
 
    protected abstract _addNode(name: string, config: any): void;
    protected abstract _removeNode(name: string): void;
    protected abstract _getConnection(query: string, requireWriteAccess: boolean): Promise<DatabaseConnection>;
    public abstract escape(query: string): string;
}