Permalink
Browse files

exposing general query-by-example interface for update and remove

  • Loading branch information...
1 parent e964ec8 commit 18e6090ecd96909588384aa76c1d72304be9f3d5 @brenden brenden committed with Ryan Fitzgerald Mar 15, 2012
Showing with 35 additions and 27 deletions.
  1. +21 −24 lib/resourceful-mongo.js
  2. +14 −3 test/resourceful-mongo-test.js
View
@@ -8,10 +8,8 @@ exports.deferred = {};
var Mongo = exports.Mongo = function Mongo(config) {
var self = this;
- //
// This function is being called to specify a database connection, define a model, or both. If
- // neither a collection name nor an onConnect callback is given, throw an error.
- //
+ // neither a collection name nor an onConnect callback is given, throw an error.
if ((!config.collection || typeof config.collection !== 'string') && typeof config.onConnect !== 'function') {
throw new Error("A collection string or onConnect callback must be specified in the config parameter.");
}
@@ -47,14 +45,11 @@ var Mongo = exports.Mongo = function Mongo(config) {
this.config = config;
this.cache = new resourceful.Cache();
- //
// If a connection for this URI has already been made, use it.
- //
if (exports.connections[config.uri]) {
this.connection = exports.connections[config.uri];
- //
+
// Otherwise if an onConnect callback was given, open a new connection.
- //
} else if (typeof config.onConnect === 'function') {
new mongo.Db(config.database, new mongo.Server(config.host, config.port, {})).open(function(err, db) {
if (err) {
@@ -75,12 +70,11 @@ var Mongo = exports.Mongo = function Mongo(config) {
})(0);
});
- //
// Failing that, just remember to set `this.connection` when the appropriate connection does get made.
// This makes it easy to define resources before opening their database connection.
- //
} else {
var self = this;
+
if (!exports.deferred[config.uri]) {
exports.deferred[config.uri] = [];
}
@@ -94,9 +88,7 @@ var Mongo = exports.Mongo = function Mongo(config) {
Mongo.prototype.protocol = 'mongodb';
-//
// Lazy-load the resource's collection
-//
Mongo.prototype.collection = function(callback) {
var self = this;
@@ -121,7 +113,7 @@ Mongo.prototype.save = function (id, doc, callback) {
}
if(doc._id && typeof doc._id === 'string') {
- doc._id = mongo.ObjectID.createFromHexString(doc._id);
+ doc._id = mongo.ObjectID(doc._id);
}
var config = this.config;
@@ -143,56 +135,61 @@ Mongo.prototype.save = function (id, doc, callback) {
Mongo.prototype.update = function (id, doc, callback) {
var self = this;
-
- // if(id && typeof id === 'string') {
- // id = new this.database.bson_serializer.ObjectID(id);
- // }
+
+ if(id && typeof id === 'string') {
+ id = {'_id': mongo.ObjectID(id)};
+ }
+
var config = this.config;
this.collection(function(err, collection){
if(err) return callback(err);
- collection.update({"_id" : id}, {$set: doc}, {safe : config.safe}, function(err) {
+ collection.update(id, {$set: doc}, {safe : config.safe}, function(err) {
if(err) return callback(err);
-
+
return self.get(id, callback);
});
-
});
};
Mongo.prototype.get = function(id, callback) {
if(id && typeof id === 'string') {
- id = {'_id': new this.database.bson_serializer.ObjectID(id)};
+ id = {'_id': mongo.ObjectID(id)};
}
this.collection(function(err, collection) {
if(err) return callback(err);
collection.findOne(id, function(err, doc) {
- if (err) return callback(err);
+ if(err) return callback(err);
callback(null, doc || {});
});
});
};
Mongo.prototype.find = function(criteria, callback) {
- this.collection(function(err, collection){
+ this.collection(function(err, collection) {
collection.find(criteria).toArray(callback);
});
};
Mongo.prototype.destroy = function(id, callback) {
+
+ if(id && typeof id === 'string') {
+ id = {'_id': mongo.ObjectID(id)};
+ }
+
var config = this.config;
this.collection(function(err, collection) {
- collection.remove({"_id": id}, {safe : config.safe}, callback);
+ collection.remove(id, {safe : config.safe}, callback);
});
};
-// register engine with resourceful
+//register engine with resourceful
resourceful.engines.Mongodb = Mongo;
//export resourceful
@@ -74,11 +74,22 @@ describe("Creating", function() {
});
});
+describe("Saving", function() {
+
+ it("saves without error", function(done) {
+
+ db.Person.create({ name: 'Bob', age: 99 }, function (err, person) {
+ if (err) done(err);
+ done();
+ });
+ });
+});
+
describe("Updating", function() {
it("paritally updates model", function(done) {
db.Person.create({ name: 'Bob', age: 99 }, function (err, person) {
- if (err) { done(err); }
+ if (err) done(err);
person.update({name:"Steve"}, function(err, person){
if(err) return done(err);
@@ -141,13 +152,13 @@ describe("Finding", function(){
describe("Destroying", function() {
beforeEach(function(done) {
- db.createPeople([{"_id": "34", name :"bob", age: 22}, db.people.steve], function(err) {
+ db.createPeople([{name: "bob", age: 22}, db.people.steve], function(err) {
done();
});
});
it("by id", function(done) {
- db.Person.destroy("34", function(err, result) {
+ db.Person.destroy({name: "bob"}, function(err, result) {
if(err) done(err);
result.should.equal(1);

0 comments on commit 18e6090

Please sign in to comment.