All files / src/helpers driver.js

67.8% Statements 40/59
55% Branches 11/20
85.71% Functions 6/7
67.8% Lines 40/59

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 1051x   1x   1x 8x     1x   1457x     1457x                                 114x 114x 26x 26x     114x       114x   114x 114x       114x 114x 114x     114x           1337x 1337x 667x 667x     1337x       1337x   1337x 1337x       1337x 1337x       2x 2x 2x       2x 2x       2x 2x 2x       2x 2x         1x  
const _ = require('lodash');
 
const debug = require('debug')('express-cassandra');
 
const Driver = function f(properties) {
  this._properties = properties;
};
 
Driver.prototype = {
  ensure_init(callback) {
    Iif (!this._properties.cql) {
      this._properties.init(callback);
    } else {
      callback();
    }
  },
 
  execute_definition_query(query, callback) {
    this.ensure_init((err) => {
      if (err) {
        callback(err);
        return;
      }
      debug('executing definition query: %s', query);
      const properties = this._properties;
      const conn = properties.define_connection;
      conn.execute(query, [], { prepare: false, fetchSize: 0 }, callback);
    });
  },
 
  execute_query(query, params, options, callback) {
    if (arguments.length === 3) {
      callback = options;
      options = {};
    }
 
    const defaults = {
      prepare: true,
    };
 
    options = _.defaultsDeep(options, defaults);
 
    this.ensure_init((err) => {
      Iif (err) {
        callback(err);
        return;
      }
      debug('executing query: %s with params: %j', query, params);
      this._properties.cql.execute(query, params, options, (err1, result) => {
        Iif (err1 && err1.code === 8704) {
          this.execute_definition_query(query, callback);
        } else {
          callback(err1, result);
        }
      });
    });
  },
 
  execute_batch(queries, options, callback) {
    if (arguments.length === 2) {
      callback = options;
      options = {};
    }
 
    const defaults = {
      prepare: true,
    };
 
    options = _.defaultsDeep(options, defaults);
 
    this.ensure_init((err) => {
      Iif (err) {
        callback(err);
        return;
      }
      debug('executing batch queries: %j', queries);
      this._properties.cql.batch(queries, options, callback);
    });
  },
 
  execute_eachRow(query, params, options, onReadable, callback) {
    this.ensure_init((err) => {
      Iif (err) {
        callback(err);
        return;
      }
      debug('executing eachRow query: %s with params: %j', query, params);
      this._properties.cql.eachRow(query, params, options, onReadable, callback);
    });
  },
 
  execute_stream(query, params, options, onReadable, callback) {
    this.ensure_init((err) => {
      Iif (err) {
        callback(err);
        return;
      }
      debug('executing stream query: %s with params: %j', query, params);
      this._properties.cql.stream(query, params, options).on('readable', onReadable).on('end', callback);
    });
  },
};
 
module.exports = Driver;