sails-mongo
Advanced tools
Comparing version 0.10.4 to 0.10.5
@@ -9,4 +9,7 @@ /*--------------------------------------------------------------- | ||
var Errors = require('waterline-errors').adapter; | ||
var ObjectId = require('mongodb').ObjectID; | ||
var _runJoins = require('waterline-cursor'); | ||
var util = require('util'); | ||
module.exports = (function() { | ||
@@ -59,3 +62,3 @@ | ||
noDelay: false, | ||
keepAlive: true, | ||
keepAlive: 0, | ||
connectTimeoutMS: 5000, | ||
@@ -92,4 +95,12 @@ socketTimeoutMS: 5000 | ||
// Create a new active connection | ||
new Connection(connection, function(err, db) { | ||
if(err) return cb(err); | ||
new Connection(connection, function(_err, db) { | ||
if(_err) { | ||
return cb((function _createError(){ | ||
var msg = util.format('Failed to connect to MongoDB. Are you sure your configured Mongo instance is running?\n Error details:\n%s', util.inspect(_err, false, null)); | ||
var err = new Error(msg); | ||
err.originalError = _err; | ||
return err; | ||
})()); | ||
} | ||
connections[connection.identity].connection = db; | ||
@@ -116,14 +127,14 @@ | ||
teardown: function(connectionName, cb) { | ||
if(!connections[connectionName]) return cb(); | ||
// Drain the connection pool if available | ||
connections[connectionName].connection.db.close(function(err) { | ||
if(err) return cb(err); | ||
// Remove the connection from the registry | ||
delete connections[connectionName]; | ||
cb(); | ||
}); | ||
teardown: function (conn, cb) { | ||
if (typeof conn == 'function') { | ||
cb = conn; | ||
conn = null; | ||
} | ||
if (conn == null) { | ||
connections = {}; | ||
return cb(); | ||
} | ||
if(!connections[conn]) return cb(); | ||
delete connections[conn]; | ||
cb(); | ||
}, | ||
@@ -224,2 +235,26 @@ | ||
/** | ||
* Mongo object with mongoDB native methods | ||
*/ | ||
mongo: { | ||
/** | ||
* ObjectId | ||
* | ||
* Return a Mongo ObjectID from a string | ||
* | ||
* @param {String} id | ||
*/ | ||
objectId: function(id){ | ||
if(!id) return null; | ||
try { | ||
return new ObjectId(id); | ||
} catch(err) { | ||
return null; | ||
} | ||
} | ||
}, | ||
/** | ||
* Create | ||
@@ -431,3 +466,22 @@ * | ||
/** | ||
* Stream | ||
* | ||
* Stream one or more documents from the collection | ||
* using where, limit, skip, and order | ||
* In where: handle `or`, `and`, and `like` queries | ||
* | ||
* @param {String} connectionName | ||
* @param {String} collectionName | ||
* @param {Object} options | ||
* @param {Object} stream | ||
*/ | ||
stream: function(connectionName, collectionName, options, stream) { | ||
options = options || {}; | ||
var connectionObject = connections[connectionName]; | ||
var collection = connectionObject.collections[collectionName]; | ||
collection.stream(options, stream); | ||
}, | ||
identity: 'sails-mongo' | ||
@@ -434,0 +488,0 @@ }; |
@@ -108,2 +108,57 @@ | ||
/** | ||
* Stream Documents | ||
* | ||
* @param {Object} criteria | ||
* @param {Object} stream | ||
* @api public | ||
*/ | ||
Collection.prototype.stream = function find(criteria, stream) { | ||
var self = this, | ||
query; | ||
// Ignore `select` from waterline core | ||
if (typeof criteria === 'object') { | ||
delete criteria.select; | ||
} | ||
// Catch errors from building query and return to the callback | ||
try { | ||
query = new Query(criteria, this.schema); | ||
} catch(err) { | ||
return stream.end(err); // End stream | ||
} | ||
var collection = this.connection.db.collection(self.identity); | ||
var where = query.criteria.where || {}; | ||
var queryOptions = _.omit(query.criteria, 'where'); | ||
// Run Normal Query on collection | ||
var dbStream = collection.find(where, queryOptions).stream(); | ||
// For each data item | ||
dbStream.on('data', function(item) { | ||
// Pause stream | ||
dbStream.pause(); | ||
var obj = utils.rewriteIds([item], self.schema)[0]; | ||
stream.write(obj, function() { | ||
dbStream.resume(); | ||
}); | ||
}); | ||
// Handle error, an 'end' event will be emitted after this as well | ||
dbStream.on('error', function(err) { | ||
stream.end(err); // End stream | ||
}); | ||
// all rows have been received | ||
dbStream.on('close', function() { | ||
stream.end(); | ||
}); | ||
}; | ||
/** | ||
* Insert A New Document | ||
@@ -110,0 +165,0 @@ * |
@@ -120,2 +120,5 @@ | ||
// Support for encoded auth credentials | ||
connectionOptions.uri_decode_auth = this.config.uri_decode_auth || false; | ||
// Build A Mongo Connection String | ||
@@ -122,0 +125,0 @@ var connectionString = 'mongodb://'; |
@@ -196,4 +196,4 @@ | ||
modifier = _.isArray(val) ? '$nin' : '$ne'; | ||
val = self.parseValue(field, modifier, val); | ||
modifier = _.isArray(val) ? '$nin' : '$ne'; | ||
obj[modifier] = val; | ||
@@ -317,6 +317,14 @@ return obj; | ||
// If we can verify that the field is NOT a string type, translate | ||
// certain values into booleans or null. Otherwise they'll be left | ||
// certain values into booleans, date or null. Otherwise they'll be left | ||
// as strings. | ||
if (hop(self.schema, field) && self.schema[field].type != 'string') { | ||
if(self.schema[field].type === 'integer'){ | ||
return parseInt(val); | ||
} | ||
if(self.schema[field].type === 'float'){ | ||
return parseFloat(val); | ||
} | ||
if (val === "false") { | ||
@@ -369,2 +377,2 @@ return false; | ||
}, {}); | ||
}; | ||
}; |
@@ -40,3 +40,7 @@ | ||
if(hop.call(model, '_id')) { | ||
model.id = model._id.toString(); | ||
// change id to string only if it's necessary | ||
if(typeof model._id === 'object') | ||
model.id = model._id.toString(); | ||
else | ||
model.id = model._id; | ||
delete model._id; | ||
@@ -43,0 +47,0 @@ } |
{ | ||
"name": "sails-mongo", | ||
"version": "0.10.4", | ||
"version": "0.10.5", | ||
"description": "Mongo DB adapter for Sails.js", | ||
@@ -42,3 +42,3 @@ "main": "./lib/adapter.js", | ||
"lodash": "~2.4.1", | ||
"mongodb": "1.4.4", | ||
"mongodb": "1.4.26", | ||
"waterline-errors": "~0.10.0", | ||
@@ -45,0 +45,0 @@ "waterline-cursor": "~0.0.5" |
65402
1894
+ Addedabort-controller@3.0.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbson@0.2.22(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedkerberos@0.0.7(transitive)
+ Addedmongodb@1.4.26(transitive)
+ Addednan@1.3.01.8.4(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@4.7.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedbson@0.2.8(transitive)
- Removedkerberos@0.0.3(transitive)
- Removedmongodb@1.4.4(transitive)
- Removednan@1.0.0(transitive)
Updatedmongodb@1.4.26