fh-wfm-mongoose-store
Advanced tools
Comparing version 0.4.4-pre.1 to 0.4.4-pre.2
@@ -25,2 +25,22 @@ 'use strict'; | ||
/** | ||
* | ||
* Updating an existing object saved as a mongoose document | ||
* | ||
* @param objectToUpdate | ||
*/ | ||
function update(objectToUpdate) { | ||
var self = this; | ||
//The object being updated may not have had an id parameter set yet | ||
//when syncing. So the localuid is used as a query instead | ||
var uid = objectToUpdate.id || objectToUpdate._localuid; | ||
self.update(objectToUpdate).then(function(updatedObject) { | ||
self.mediator.publish([self.topics.getTopic('update', 'done'), uid].join(':'), updatedObject); | ||
}).catch(function(err) { | ||
self.mediator.publish([self.topics.getTopic('update', 'error'), uid].join(':'), err); | ||
}); | ||
} | ||
module.exports = function decorate(Class) { | ||
@@ -54,3 +74,3 @@ | ||
read: self.read, | ||
update: self.update, | ||
update: update, | ||
remove: self.remove, | ||
@@ -57,0 +77,0 @@ list: self.list, |
@@ -118,5 +118,23 @@ 'use strict'; | ||
return this.model.findOne({id: object.id}).exec().then(function(foundDocument) { | ||
var query; | ||
console.log("*** Update ",object); | ||
if (!_.isObject(object)) { | ||
return self.handleError(null, new Error("Expected an object to update")); | ||
} | ||
var uid = object._localuid || object.id; | ||
if (object.id) { | ||
query = {id: object.id}; | ||
} else if (object._localuid) { | ||
query = {_localuid: object._localuid}; | ||
} else { | ||
return self.handleError(null, new Error("Expected the object to have either an id or _localuid field")); | ||
} | ||
return this.model.findOne(query).exec().then(function(foundDocument) { | ||
if (!foundDocument) { | ||
return createNoDocumentError(object.id); | ||
return createNoDocumentError(uid); | ||
} else { | ||
@@ -127,3 +145,3 @@ _.extend(foundDocument, object); | ||
}).then(convertToJSON).catch(function(err) { | ||
return self.handleError(object.id, err); | ||
return self.handleError(uid, err); | ||
}); | ||
@@ -130,0 +148,0 @@ }; |
{ | ||
"name": "fh-wfm-mongoose-store", | ||
"version": "0.4.4-pre.1", | ||
"version": "0.4.4-pre.2", | ||
"description": "Direct mongoose storage", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var expect = require('expect'); | ||
var _ = require('lodash'); | ||
@@ -96,3 +97,5 @@ var mongoUri = 'mongodb://localhost:27017/raincatcher-mongo-connector'; | ||
testDal.create({ | ||
id: "testid", | ||
status: 'test', | ||
_localuid: "localid", | ||
workorderId: '1234567890' | ||
@@ -125,2 +128,11 @@ }).then(function(doc) { | ||
it('should update with a local id', function(done) { | ||
testDal.update(_.omit(testDoc, 'id')).then(function(result) { | ||
testDoc = result; | ||
done(assert.equal(result._localuid, 'localid')); | ||
}, function(error) { | ||
done(error); | ||
}); | ||
}); | ||
it('should return an error if no record exists', function(done) { | ||
@@ -127,0 +139,0 @@ var id = "idontexist"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37898
940