sails-mongo
Advanced tools
Comparing version 0.10.0-rc3 to 0.10.0-rc4
@@ -338,2 +338,25 @@ /*--------------------------------------------------------------- | ||
/** | ||
* Count | ||
* | ||
* Return a count of the number of records matching a criteria. | ||
* | ||
* @param {String} connectionName | ||
* @param {String} collectionName | ||
* @param {Object} options | ||
* @param {Function} callback | ||
*/ | ||
count: function(connectionName, collectionName, options, cb) { | ||
var connectionObject = connections[connectionName]; | ||
var collection = connectionObject.collections[collectionName]; | ||
// Find matching documents and return the count | ||
collection.count(options, function(err, results) { | ||
if(err) return cb(err); | ||
cb(null, results); | ||
}); | ||
}, | ||
identity: 'sails-mongo' | ||
@@ -340,0 +363,0 @@ }; |
@@ -123,3 +123,3 @@ | ||
if(err) return cb(err); | ||
cb(null, utils.rewriteIds(results)); | ||
cb(null, utils.rewriteIds(results, self.schema)); | ||
}); | ||
@@ -177,3 +177,3 @@ }; | ||
if(err) return cb(err); | ||
cb(null, utils.rewriteIds(records)); | ||
cb(null, utils.rewriteIds(records, self.schema)); | ||
}); | ||
@@ -221,7 +221,33 @@ }); | ||
cb(null, utils.rewriteIds(resultArray)); | ||
cb(null, utils.rewriteIds(resultArray, self.schema)); | ||
}); | ||
}; | ||
/** | ||
* Count Documents | ||
* | ||
* @param {Object} criteria | ||
* @param {Function} callback | ||
* @api public | ||
*/ | ||
Collection.prototype.count = function count(criteria, cb) { | ||
var self = this; | ||
var query; | ||
// Catch errors build query and return to the callback | ||
try { | ||
query = new Query(criteria); | ||
} catch(err) { | ||
return cb(err); | ||
} | ||
this.connection.db.collection(this.identity).count(query.criteria.where, function(err, count) { | ||
if (err) return cb(err); | ||
cb(null, count); | ||
}); | ||
}; | ||
///////////////////////////////////////////////////////////////////////////////// | ||
@@ -267,5 +293,10 @@ // PRIVATE METHODS | ||
Object.keys(this.schema).forEach(function(key) { | ||
var index = {}, | ||
options = {}; | ||
var index = {}; | ||
var options = {}; | ||
// If index key is `id` ignore it because Mongo will automatically handle this | ||
if(key === 'id') { | ||
return; | ||
} | ||
// Handle Unique Indexes | ||
@@ -272,0 +303,0 @@ if(self.schema[key].unique) { |
@@ -34,5 +34,5 @@ | ||
options = this.parseTypes(options); | ||
// Normalize Criteria | ||
this.criteria = this.normalizeCriteria(options); | ||
// console.log("Q", require('util').inspect(options, false, null)); | ||
@@ -117,2 +117,7 @@ return this; | ||
// if value appears to be a mongo id normalize it as such | ||
if(_.isString(val) && utils.matchMongoId(val)) { | ||
obj[key] = ObjectId(val); | ||
} | ||
// Normalize `or` key into mongo $or | ||
@@ -165,3 +170,3 @@ if(key === 'or') { | ||
var self = this; | ||
// console.log("C", require('util').inspect(options, false, null)); | ||
Object.keys(options).forEach(function(key) { | ||
@@ -208,2 +213,20 @@ var original = _.cloneDeep(options[key]); | ||
} | ||
if(Array.isArray(obj)) { | ||
obj.forEach(function(arrayVal, i) { | ||
if(_.isString(arrayVal) && utils.matchMongoId(arrayVal)) { | ||
obj[i] = ObjectId(arrayVal); | ||
} | ||
}); | ||
original[key] = { '$in': obj }; | ||
// Normalize id, if used, into _id | ||
if(key === 'id') { | ||
var data = _.cloneDeep(original[key]); | ||
delete original[key]; | ||
original['_id'] = data; | ||
} | ||
} | ||
return; | ||
@@ -262,7 +285,2 @@ } | ||
// Check for Mongo IDs | ||
if(utils.matchMongoId(obj[attr])) { | ||
return; | ||
} | ||
// Handle Sorting Order with binary or -1/1 values | ||
@@ -396,2 +414,7 @@ if(key === 'sort') { | ||
// Check for Mongo IDs | ||
if(utils.matchMongoId(obj[attr])) { | ||
return; | ||
} | ||
// Ignore special attributes | ||
@@ -398,0 +421,0 @@ if(['_bsontype', '_id', 'id'].indexOf(attr) >= 0) return; |
{ | ||
"name": "sails-mongo", | ||
"version": "0.10.0-rc3", | ||
"version": "0.10.0-rc4", | ||
"description": "Mongo DB adapter for Sails.js", | ||
@@ -42,3 +42,3 @@ "main": "./lib/adapter.js", | ||
"lodash": "~2.4.1", | ||
"mongodb": "~1.3.23", | ||
"mongodb": "1.4.2", | ||
"waterline-errors": "~0.10.0" | ||
@@ -45,0 +45,0 @@ }, |
@@ -72,2 +72,4 @@ ![image_squidhome@2x.png](http://i.imgur.com/RIvu9.png) | ||
Don't forget that Mongo uses the ObjectId type for ids. | ||
## Sails.js | ||
@@ -74,0 +76,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
51221
19
1503
103
0
+ Addedbson@0.2.7(transitive)
+ Addedmongodb@1.4.2(transitive)
+ Addednan@0.8.0(transitive)
- Removedbson@0.2.5(transitive)
- Removedmongodb@1.3.23(transitive)
Updatedmongodb@1.4.2