all files / node-zoom2/lib/ connection.js

91.18% Statements 62/68
71.43% Branches 20/28
100% Functions 12/12
91.18% Lines 62/68
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          16×           24× 24×                                                                                              
'use strict';
 
var Query_ = require('./binding').Query;
var Options_ = require('./binding').Options;
var Connection_ = require('./binding').Connection;
var noop = require('./noop');
var ResultSet = require('./resultset');
var ReadStream = require('./read-stream');
 
module.exports = Connection;
 
var conn = Connection.prototype;
 
function Connection(host) {
  if (!(this instanceof Connection)) {
    return new Connection(host);
  }
 
  this._connected = false;
  this._options = Options_();
  this._conn = new Connection_(this._options);
  this.set('implementationName', 'node-zoom2');
 
  var parsed = this._parseHost(host || '');
  parsed.database && this.set('databaseName', parsed.database);
  this._host = parsed.host;
  this._port = parsed.port | 0;
}
 
conn.set = function (key, val) {
  this._options.set(key, val);
  return this;
};
 
conn.get = function (key) {
  return this._options.get(key);
};
 
conn.query = function (type, queryString) {
  if (arguments.length < 2) {
    queryString = type;
    type = 'prefix';
  }
  var clone = Object.create(this);
  clone._query = Query_();
 
  Iif (!clone._query[type]) {
    throw new Error('Unknown query type');
  }
 
  clone._query[type](queryString);
 
  return clone;
};
 
conn.sort = function () {
  Eif (!this._query) {
    throw new Error('Query not found');
  }
  this._query.sortBy.apply(this._query, arguments);
  return this;
};
 
conn.connect = function (cb) {
  cb || (cb = noop);
 
  Iif (this._connected) {
    cb(null);
  } else {
    this._conn.connect(this._host, this._port, function (err) {
      this._connected = !err;
      cb(err);
    }.bind(this));
  }
 
  return this;
};
 
conn.createReadStream = function (options) {
  if (!this._query) {
    throw new Error('Query not found');
  }
  return new ReadStream(this, options);
};
 
conn.search = function (cb) {
  if (!this._query) {
    throw new Error('Query not found');
  }
 
  cb || (cb = noop);
 
  this.connect(function (err) {
    Iif (err) {
      cb(err);
      return;
    }
 
    this._conn.search(this._query, function (err, resultset) {
      if (err) {
        cb(err);
        return;
      }
      cb(null, new ResultSet(resultset));
    }.bind(this));
  }.bind(this));
 
  return this;
};
 
conn._parseHost = function (host) {
  var match = host.match(/^(.+?)(?::(\d+))?(?:\/(.+))?$/);
  return match ? {
    host: match[1],
    port: match[2],
    database: match[3]
  } : {};
};