mongodb-wrapper
Version:
Exactly-like-the-console wrapper for node-mongodb-native
281 lines (239 loc) • 9.1 kB
JavaScript
// Generated by CoffeeScript 1.4.0
(function() {
var CONN_CLOSED, CONN_OPEN, CONN_OPENING, Collection, Database, EventEmitter, async, mongodb, url,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
CONN_CLOSED = 0;
CONN_OPENING = 1;
CONN_OPEN = 2;
mongodb = require("mongodb");
async = require("async");
EventEmitter = require('events').EventEmitter;
url = require("url");
Collection = require("./Collection");
Database = (function(_super) {
__extends(Database, _super);
function Database(host, port, dbName, prefix, username, password) {
if (prefix == null) {
prefix = "";
}
this._state = {};
this._state.prefix = prefix;
this._state.cbCache = new EventEmitter;
this._state.cbCache.setMaxListeners(100);
this._normalizeParams(host, port, dbName, username, password);
this._state.connection = null;
this._state.status = CONN_CLOSED;
this._state.collections = {};
}
Database.prototype._normalizeParams = function(host, port, dbName, username, password) {
var parsed, userpass;
userpass = username && password ? "" + username + ":" + password + "@" : "";
if (Array.isArray(host)) {
this._state.connectionString = this._makeReplConnString(host, dbName, userpass);
this._state.opts = port;
} else if (host.match(/^mongodb:\/\/.*/)) {
this._state.connectionString = host;
this._state.opts = port;
} else {
this._state.connectionString = this._makeConnString(host, port, dbName, userpass);
this._state.opts = {};
}
parsed = url.parse(this._state.connectionString);
this._state.dbName = parsed.pathname.replace("/", "");
this._state.hostname = parsed.hostname;
return this._state.port = parsed.port;
};
Database.prototype._makeReplConnString = function(hosts, dbName, userpass) {
var host, parts, prefix, _i, _len;
prefix = "mongodb://" + userpass;
parts = [];
for (_i = 0, _len = hosts.length; _i < _len; _i++) {
host = hosts[_i];
parts.push("" + host.host + ":" + host.port);
}
return prefix + parts.join(",") + ("/" + dbName);
};
Database.prototype._makeConnString = function(host, port, dbName, userpass) {
return "mongodb://" + userpass + host + ":" + port + "/" + dbName;
};
Database.prototype.host = function() {
return this._state.hostname;
};
Database.prototype.port = function() {
return this._state.port;
};
Database.prototype.prefix = function() {
return this._state.prefix;
};
Database.prototype.name = function() {
return this._state.dbName;
};
Database.prototype.prefixName = function(collName) {
if (!this._state.prefix) {
return collName;
}
if (collName.match(new RegExp("^" + this._state.prefix + "\\."))) {
return collName;
}
return this._state.prefix + "." + collName;
};
Database.prototype._getConnection = function(cb) {
switch (this._state.status) {
case CONN_CLOSED:
this._state.cbCache.once("open", cb);
return this._openConnection();
case CONN_OPENING:
return this._state.cbCache.once("open", cb);
case CONN_OPEN:
return cb(null, this._state.connection);
default:
return cb(new Error("invalid connection state"));
}
};
Database.prototype._openConnection = function() {
var _this = this;
this._state.status = CONN_OPENING;
return mongodb.MongoClient.connect(this._state.connectionString, this._state.opts, function(err, connection) {
if (err) {
_this._state.status = CONN_CLOSED;
_this.emit("open", err);
return _this.emit("error", err);
}
_this.emit("opened");
_this._state.status = CONN_OPEN;
_this._state.connection = connection;
_this._state.connection.on("error", function(err) {
return _this.emit("error", err);
});
_this._state.connection.on("closed", function() {
if (_this._state.status !== CONN_CLOSED) {
_this.emit("error", new Error("connection disconnected unexpectedly"));
_this._state.status = CONN_CLOSED;
}
return _this.emit("closed");
});
return _this._state.cbCache.emit("open", null, connection);
});
};
Database.prototype.currentConnection = function() {
return this._state.connection;
};
Database.prototype.close = function() {
this._state.status = CONN_CLOSED;
return this._state.connection.close();
};
Database.prototype.runCommand = function(command, params, cb) {
if (typeof params === "function") {
cb = params;
params = [];
}
params.push(cb);
return this._getConnection(function(err, connection) {
if (err != null) {
return cb(err);
}
return connection[command].apply(connection, params);
});
};
Database.prototype.collection = function(collName) {
var bottomLevelName, collection, container, nameParts, part, _i, _len,
_this = this;
if (this._state.collections[collName] != null) {
return this._state.collections[collName];
}
nameParts = collName.split(".");
bottomLevelName = nameParts.pop();
container = null;
for (_i = 0, _len = nameParts.length; _i < _len; _i++) {
part = nameParts[_i];
container = this[part] || {};
if (typeof container !== "object") {
throw new Error("trying to attach collection name of existing property " + part);
}
this[part] = container;
}
container || (container = this);
collection = new Collection(this, collName);
collection.on("error", function(err) {
return _this.emit("error", err);
});
container[bottomLevelName] = collection;
this._state.collections[collName] = collection;
return collection;
};
Database.prototype.auth = function(username, password, cb) {
return this.runCommand("authenticate", [username, password], cb);
};
Database.prototype.addUser = function(username, password, cb) {
return this.runCommand("addUser", [username, password], cb);
};
Database.prototype.removeUser = function(username, cb) {
return this.runCommand("removeUser", [username], cb);
};
Database.prototype.lastError = function(cb) {
return this.runCommand("lastError", cb);
};
Database.prototype["eval"] = function(code, params, cb) {
if (typeof params === "function") {
cb = params;
params || (params = {});
}
return this.runCommand("eval", [code, params], cb);
};
Database.prototype.dropDatabase = function(cb) {
return this.runCommand("dropDatabase", cb);
};
Database.prototype.createCollection = function(collName, opts, cb) {
return this.runCommand("createCollection", [collName, opts], cb);
};
Database.prototype.getCollectionNames = function(cb) {
var _this = this;
return this.runCommand("collectionNames", function(err, names) {
var filtered, name, _i, _len;
if (err) {
return cb(err);
}
filtered = [];
for (_i = 0, _len = names.length; _i < _len; _i++) {
name = names[_i];
filtered.push(names.replace(_this._state.dbName + ".", ""));
}
return cb(null, filtered);
});
};
Database.prototype.dropCollection = function(collectionName, cb) {
return this.runCommand("dropCollection", [collectionName], cb);
};
Database.prototype.dropAllCollections = function(cb) {
var dropSingle;
dropSingle = function(coll, acb) {
return coll.drop(acb);
};
return async.forEach(this._state.collections, dropSingle, cb);
};
Database.prototype.dropAllCollectionsOnServer = function(cb) {
var _this = this;
return this.getCollectionNames(function(err, names) {
var dropSingle;
if (err) {
return cb(err);
}
dropSingle = function(collName, acb) {
var coll;
if (collName.match(/^system.*/)) {
return acb();
}
coll = new Collection(_this, collName);
return coll.drop(acb);
};
return async.forEach(names, dropSingle, cb);
});
};
Database.prototype.renameCollection = function(fromCollection, toCollection, cb) {
return this.runCommand("renameCollection", [fromCollection, toCollection], cb);
};
return Database;
})(EventEmitter);
module.exports = Database;
}).call(this);