From 686106d544ecc3b6ffd4db2b665d3bc879a58d8c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 24 Sep 2012 16:22:07 -0400 Subject: ok --- .../lib/drivers/node-mongodb-native/binary.js | 8 + .../lib/drivers/node-mongodb-native/collection.js | 176 +++++++++++++++++++++ .../lib/drivers/node-mongodb-native/connection.js | 106 +++++++++++++ .../lib/drivers/node-mongodb-native/objectid.js | 43 +++++ 4 files changed, 333 insertions(+) create mode 100644 node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js create mode 100644 node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js create mode 100644 node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js create mode 100644 node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js (limited to 'node_modules/mongoose/lib/drivers') diff --git a/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js b/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js new file mode 100644 index 0000000..52e6a03 --- /dev/null +++ b/node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js @@ -0,0 +1,8 @@ + +/** + * Module dependencies. + */ + +var Binary = require('mongodb').BSONPure.Binary; + +module.exports = exports = Binary; diff --git a/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js b/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js new file mode 100644 index 0000000..06176da --- /dev/null +++ b/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js @@ -0,0 +1,176 @@ + +/** + * Module dependencies. + */ + +var Collection = require('../../collection') + , NativeCollection = require('mongodb').Collection + , utils = require('../../utils') + +/** + * Native collection + * + * @api private + */ + +function MongooseCollection () { + Collection.apply(this, arguments); +}; + +/** + * Inherit from abstract Collection. + */ + +MongooseCollection.prototype.__proto__ = Collection.prototype; + +/** + * Called when the connection opens + * + * @api private + */ + +MongooseCollection.prototype.onOpen = function () { + var self = this; + this.conn.db.collection(this.name, function (err, collection) { + if (err) { + // likely a strict mode error + self.conn.emit('error', err); + } else { + self.collection = collection; + Collection.prototype.onOpen.call(self); + } + }); +}; + +/** + * Called when the connection closes + * + * @api private + */ + +MongooseCollection.prototype.onClose = function () { + Collection.prototype.onClose.call(this); +}; + +/** + * Copy the collection methods and make them subject to queues + */ + +for (var i in NativeCollection.prototype) + (function(i){ + MongooseCollection.prototype[i] = function () { + // BENCHMARKME: is it worth caching the prototype methods? probably + if (!this.buffer) { + var collection = this.collection + , args = arguments + , self = this; + + process.nextTick(function(){ + var debug = self.conn.base.options.debug; + + if (debug) { + if ('function' === typeof debug) { + debug.apply(debug + , [self.name, i].concat(utils.args(args, 0, args.length-1))); + } else { + console.error('\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s' + , self.name + , i + , print(args[0]) + , print(args[1]) + , print(args[2]) + , print(args[3])) + } + } + + collection[i].apply(collection, args); + }); + } else { + this.addQueue(i, arguments); + } + }; + })(i); + +/** + * Debug print helper + * @private + */ + +function print (arg) { + var type = typeof arg; + if ('function' === type || 'undefined' === type) return ''; + return format(arg); +} + +/** + * Debug print helper + * @private + */ + +function format (obj, sub) { + var x = utils.clone(obj); + if (x) { + if ('Binary' === x.constructor.name) { + x = '[object Buffer]'; + } else if ('Object' === x.constructor.name) { + var keys = Object.keys(x) + , i = keys.length + , key + while (i--) { + key = keys[i]; + if (x[key]) { + if ('Binary' === x[key].constructor.name) { + x[key] = '[object Buffer]'; + } else if ('Object' === x[key].constructor.name) { + x[key] = format(x[key], true); + } else if (Array.isArray(x[key])) { + x[key] = x[key].map(function (o) { + return format(o, true) + }); + } + } + } + } + if (sub) return x; + } + + return require('util') + .inspect(x, false, 10, true) + .replace(/\n/g, '') + .replace(/\s{2,}/g, ' ') +} + +/** + * Implement getIndexes + * + * @param {Function} callback + * @api private + */ + +MongooseCollection.prototype.getIndexes = +MongooseCollection.prototype.indexInformation; + +/** + * Override signature of ensureIndex. -native one is not standard. + * + * @param {Object} spec + * @param {Object} options + * @param {Function} callback + * @api public + */ + +var oldEnsureIndex = NativeCollection.prototype.ensureIndex; + +function noop () {}; + +NativeCollection.prototype.ensureIndex = function(fields, options, fn){ + if (!this.buffer) { + return oldEnsureIndex.apply(this, arguments); + } +}; + +/** + * Module exports. + */ + +module.exports = MongooseCollection; diff --git a/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js b/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js new file mode 100644 index 0000000..3becd1b --- /dev/null +++ b/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js @@ -0,0 +1,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; diff --git a/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js b/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js new file mode 100644 index 0000000..ee6d598 --- /dev/null +++ b/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js @@ -0,0 +1,43 @@ + +/** + * Module dependencies. + */ + +var ObjectId = require('mongodb').BSONPure.ObjectID; + +/** + * Constructor export + * + * @api private + */ + +var ObjectIdToString = ObjectId.toString.bind(ObjectId); + +module.exports = exports = ObjectId; +/** + * Creates an ObjectID for this driver + * + * @param {Object} hex string or ObjectId + * @api private + */ + +exports.fromString = function(str){ + // patch native driver bug in V0.9.6.4 + if (!('string' === typeof str && 24 === str.length)) { + throw new Error("Invalid ObjectId"); + } + + return ObjectId.createFromHexString(str); +}; + +/** + * Gets an ObjectId and converts it to string. + * + * @param {ObjectId} -native objectid + * @api private + */ + +exports.toString = function(oid){ + if (!arguments.length) return ObjectIdToString(); + return oid.toHexString(); +}; -- cgit v1.2.3-70-g09d2