Permalink
Browse files

add ObjectID support for all xxxxById methods

  • Loading branch information...
1 parent f9174d0 commit 485847ba85e54f46bb41c6b1a9574f6271639293 @fengmk2 fengmk2 committed Nov 17, 2011
Showing with 17 additions and 7 deletions.
  1. +7 −7 lib/mongoskin/collection.js
  2. +10 −0 test/{findById.test.js → collection.test.js}
@@ -43,6 +43,7 @@ var __slice = Array.prototype.slice,
*/
var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
this.skinDb = skinDb;
+ this.ObjectID = this.skinDb.db.bson_serializer.ObjectID;
this.collectionName = collectionName;
this.collection;
this.state = STATE_CLOSE;
@@ -58,7 +59,7 @@ var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
});
this.emitter = new events.EventEmitter();
-}
+};
/**
* bind method of mongodb.Collection to mongoskin.SkinCollection
@@ -123,7 +124,7 @@ SkinCollection.prototype.open = function(fn) {
SkinCollection.prototype.close = function(){
this.state = STATE_CLOSE;
-}
+};
SkinCollection.prototype.drop = function(fn) {
this.skinDb.dropCollection(this.collectionName, fn);
@@ -174,6 +175,9 @@ SkinCollection.prototype.findEach = function() {
};
SkinCollection.prototype.id = function(hex) {
+ if(hex instanceof this.ObjectID) {
+ return hex;
+ }
return this.skinDb.db.bson_serializer.ObjectID.createFromHexString(hex);
};
@@ -187,11 +191,7 @@ SkinCollection.prototype.id = function(hex) {
*/
SkinCollection.prototype.findById = function() {
var args = __slice.call(arguments);
- var _id = args[0];
- if(!(_id instanceof this.skinDb.db.bson_serializer.ObjectID)) {
- _id = this.id(_id);
- }
- args[0] = {_id: _id};
+ args[0] = {_id: this.id(args[0])};
this.findOne.apply(this, args);
};
@@ -8,6 +8,16 @@ var mongoskin = require('../')
module.exports = {
+ 'test id()': function() {
+ var db = mongoskin.db('localhost/test');
+ db.bind('testcollection');
+ var id = '4ec4b2b9f44a927223000001';
+ id = db.testcollection.id(id);
+ id.should.be.instanceof(db.testcollection.ObjectID);
+ id = db.testcollection.id(id);
+ id.should.be.instanceof(db.testcollection.ObjectID);
+ db.close();
+ },
'test findById string id': function() {
var db = mongoskin.db('localhost/test');
var ObjectID = db.db.bson_serializer.ObjectID;

0 comments on commit 485847b

Please sign in to comment.