summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js
blob: 3becd1b96434850f781c609230c0166a4ed725e0 (plain)
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
/**
 * Module dependencies.
 */

var Connection = require('../../connection')
  , mongo = require('mongodb')
  , Server = mongo.Server
  , ReplSetServers = mongo.ReplSetServers;

/**
 * Connection for mongodb-native driver
 *
 * @api private
 */

function NativeConnection() {
  Connection.apply(this, arguments);
};

/**
 * Inherits from Connection.
 */

NativeConnection.prototype.__proto__ = Connection.prototype;

/**
 * Opens the connection.
 *
 * Example server options:
 *   auto_reconnect (default: false)
 *   poolSize (default: 1)
 *
 * Example db options:
 *   pk - custom primary key factory to generate `_id` values
 *
 * Some of these may break Mongoose. Use at your own risk. You have been warned.
 *
 * @param {Function} callback
 * @api private
 */

NativeConnection.prototype.doOpen = function (fn) {
  var server;

  if (!this.db) {
    server = new mongo.Server(this.host, Number(this.port), this.options.server);
    this.db = new mongo.Db(this.name, server, this.options.db);
  }

  this.db.open(fn);

  return this;
};

/**
 * Opens a set connection
 *
 * See description of doOpen for server options. In this case options.replset
 * is also passed to ReplSetServers. Some additional options there are
 *
 *     reconnectWait (default: 1000)
 *     retries (default: 30)
 *     rs_name (default: false)
 *     read_secondary (default: false) Are reads allowed from secondaries?
 *
 * @param {Function} fn
 * @api private
 */

NativeConnection.prototype.doOpenSet = function (fn) {
  if (!this.db) {
    var servers = []
      , ports = this.port
      , self = this

    this.host.forEach(function (host, i) {
      servers.push(new mongo.Server(host, Number(ports[i]), self.options.server));
    });

    var server = new ReplSetServers(servers, this.options.replset);
    this.db = new mongo.Db(this.name, server, this.options.db);
  }

  this.db.open(fn);

  return this;
};

/**
 * Closes the connection
 *
 * @param {Function} callback
 * @api private
 */

NativeConnection.prototype.doClose = function (fn) {
  this.db.close();
  if (fn) fn();
  return this;
}

/**
 * Module exports.
 */

module.exports = NativeConnection;