All files / src/drivers RethinkDB.ts

73.17% Statements 30/41
25% Branches 2/8
75% Functions 9/12
74.36% Lines 29/39

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134  5x 5x 5x 5x 5x   5x       2x         1x 1x 1x     1x     2x 2x 2x                             2x 2x                                         2x   2x       2x 2x   2x 1x   2x                                                     1x 1x           1x                 1x 1x         1x                  
"use strict";
import { defaultsDeep } from "lodash";
import * as rethinkdb from "rethinkdbdash";
import { AppLogger, logger as defaultLogger } from "../utils/Logger";
import { AppError } from "../errors";
import { loadConfig } from "my-nconf";
 
export class RethinkDB {
    private logger: any;
    private cluster_conf: any;
    private r: rethinkdb;
    private health = false;
 
    private static _instance;
 
    static get Instance() {
        Eif (!this._instance) {
            let c = loadConfig();
            this._instance = new RethinkDB(c.get("rethinkdb_cluster"));
        }
 
        return this._instance;
    }
    constructor(_cluster_conf, _logger = defaultLogger) {
        let my = this;
        my.logger = _logger;
        my.cluster_conf = defaultsDeep({}, _cluster_conf, {
            authKey: "",
            buffer: 2,
            max: 5,
            servers: [
                {
                    host: "localhost",
                    port: 28016
                }
            ],
            discovery: false,
            silent: true,
            pool: true,
            cursor: false
        });
        my.r = new rethinkdb(my.cluster_conf);
        my.r.getPoolMaster().on("healthy", function(healthy) {
            if (my.health == healthy) return; //nothing to do
 
            if (healthy === true) {
                my.logger.info(
                    "RethinkDB cluster online @:",
                    my.cluster_conf.servers
                );
            } else {
                my.logger.error(
                    new AppError(
                        "No queries can be run.",
                        "RethinkDBConnectionError"
                    )
                );
                my.logger.error(my.cluster_conf);
            }
 
            my.health = healthy;
        });
 
        my.patchmyR();
 
        return my.r;
    }
 
    private patchmyR() {
        let my = this;
        my.r.ensure_db_exist = db_name => my.ensure_db_exist(db_name);
 
        my.r.ensure_table_exist = (db_name, table_name, options?) =>
            my.ensure_table_exist(db_name, table_name, options);
 
        my.r.ensure_dbTable_exist = (
            db_name,
            table_name,
            options?: { dbCreateOptions; tableCreateOptions }
        ) => my.ensure_dbTable_exist(db_name, table_name, options);
    }
 
    private async ensure_dbTable_exist(
        db_name,
        table_name,
        options: { dbCreateOptions; tableCreateOptions } = {
            dbCreateOptions: undefined, //place holder
            tableCreateOptions: undefined
        }
    ) {
        let db_res = await this.ensure_db_exist(db_name);
        let tb_res = await this.ensure_table_exist(
            db_name,
            table_name,
            options.tableCreateOptions
        );
 
        return [db_res, tb_res];
    }
 
    //https://rethinkdb.com/api/javascript/table_create/
    private async ensure_db_exist(db_name) {
        let my = this;
        return await my.r
            .dbList()
            .contains(db_name)
            .do(function(databaseExists) {
                //logger.debug(databaseExists);
 
                return my.r.branch(
                    databaseExists,
                    { dbs_created: 0 },
                    my.r.dbCreate(db_name)
                );
            })
            .run();
    }
    private async ensure_table_exist(db_name, table_name, options?) {
        let my = this;
        return await my.r
            .db(db_name)
            .tableList()
            .contains(table_name)
            .do(function(tableExists) {
                return my.r.branch(
                    tableExists,
                    { tables_created: 0 },
                    my.r.db(db_name).tableCreate(table_name, options)
                );
            })
            .run();
    }
}