Comparing version 0.6.0 to 0.7.0
@@ -0,1 +1,7 @@ | ||
## 0.7.0 (2015-08-18) | ||
Features: | ||
- Added `loadOneAndUpdate` static method to `Document` class | ||
- Added `loadOneAndDelete` static method to `Document` class | ||
## 0.6.0 (2015-08-10) | ||
@@ -2,0 +8,0 @@ |
@@ -28,2 +28,10 @@ "use strict"; | ||
loadOneAndUpdate(collection, query, values, options) { | ||
throw new TypeError('You must override loadOneAndUpdate.'); | ||
} | ||
loadOneAndDelete(collection, query, options) { | ||
throw new TypeError('You must override loadOneAndDelete.'); | ||
} | ||
loadMany(collection, query) { | ||
@@ -30,0 +38,0 @@ throw new TypeError('You must override loadMany.'); |
@@ -89,2 +89,46 @@ "use strict"; | ||
loadOneAndUpdate(collection, query, values, options) { | ||
var that = this; | ||
if (!options) { | ||
options = {}; | ||
} | ||
// Always return the updated object | ||
options.returnOriginal = false; | ||
return new Promise(function(resolve, reject) { | ||
var db = that._mongo.collection(collection); | ||
var update = values; | ||
if (options.upsert) { | ||
update = { $setOnInsert: update }; | ||
} else { | ||
update = { $set: update }; | ||
} | ||
db.findOneAndUpdate(query, update, options, function(error, result) { | ||
if (error) return reject(error); | ||
resolve(result.value); | ||
}); | ||
}); | ||
} | ||
loadOneAndDelete(collection, query, options) { | ||
var that = this; | ||
if (!options) { | ||
options = {}; | ||
} | ||
return new Promise(function(resolve, reject) { | ||
var db = that._mongo.collection(collection); | ||
db.findOneAndDelete(query, options, function (error, result) { | ||
if (error) return reject(error); | ||
return resolve(result.value === null ? 0 : 1); | ||
}); | ||
}); | ||
} | ||
loadMany(collection, query) { | ||
@@ -91,0 +135,0 @@ var that = this; |
@@ -64,3 +64,2 @@ "use strict"; | ||
if (error) return reject(error); | ||
return resolve(result._id); | ||
@@ -123,2 +122,64 @@ }); | ||
loadOneAndUpdate(collection, query, values, options) { | ||
var that = this; | ||
if (!options) { | ||
options = {}; | ||
} | ||
// Since this is 'loadOne...' we'll only allow user to update | ||
// one document at a time | ||
options.multi = false; | ||
return new Promise(function(resolve, reject) { | ||
var db = getCollection(collection, that._collections, that._path); | ||
// TODO: Would like to just use 'Collection.update' here, but | ||
// it doesn't return objects on update (but will on insert)... | ||
/*db.update(query, values, options, function(error, numReplaced, newDoc) { | ||
if (error) return reject(error); | ||
resolve(newDoc); | ||
});*/ | ||
that.loadOne(collection, query).then(function(data) { | ||
if (!data) { | ||
if (options.upsert) { | ||
return db.insert(values, function(error, result) { | ||
if (error) return reject(error); | ||
return resolve(result); | ||
}); | ||
} else { | ||
return resolve(null); | ||
} | ||
} else { | ||
return db.update(query, { $set: values }, function(error, result) { | ||
if (error) return reject(error); | ||
result = _.assign(data, values); | ||
return resolve(result); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
loadOneAndDelete(collection, query, options) { | ||
var that = this; | ||
if (!options) { | ||
options = {}; | ||
} | ||
// Since this is 'loadOne...' we'll only allow user to update | ||
// one document at a time | ||
options.multi = false; | ||
return new Promise(function(resolve, reject) { | ||
var db = getCollection(collection, that._collections, that._path); | ||
db.remove(query, options, function (error, numRemoved) { | ||
if (error) return reject(error); | ||
return resolve(numRemoved); | ||
}); | ||
}); | ||
} | ||
loadMany(collection, query) { | ||
@@ -125,0 +186,0 @@ var that = this; |
@@ -247,2 +247,52 @@ "use strict"; | ||
static loadOneAndUpdate(query, values, options) { | ||
var that = this; | ||
if (arguments.length < 2) { | ||
throw new Error('loadOneAndUpdate requires at least 2 arguments. Got ' + arguments.length + '.'); | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
var populate = true; | ||
if (options.hasOwnProperty('populate')) { | ||
populate = options.populate; | ||
} | ||
return DB().loadOneAndUpdate(this.collectionName(), query, values, options) | ||
.then(function(data) { | ||
if (!data) { | ||
return null; | ||
} | ||
var doc = that._fromData(data); | ||
if (populate) { | ||
return that.populate(doc); | ||
} | ||
return doc; | ||
}).then(function(doc) { | ||
if (doc) { | ||
return doc; | ||
} | ||
return null; | ||
}); | ||
} | ||
static loadOneAndDelete(query, options) { | ||
var that = this; | ||
if (arguments.length < 1) { | ||
throw new Error('loadOneAndDelete requires at least 1 argument. Got ' + arguments.length + '.'); | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
return DB().loadOneAndDelete(this.collectionName(), query, options); | ||
} | ||
// TODO: Need options to specify whether references should be loaded | ||
@@ -249,0 +299,0 @@ static loadMany(query, options) { |
{ | ||
"name": "camo", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A class-based ES6 ODM for Mongo-like databases.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -242,2 +242,4 @@ # Camo | ||
An alternative to `.save()` is `.loadOneAndUpdate(query, update, options)`. This static method will find and update (or insert) a document in one atomic operation (atomicity is guaranteed in MongoDB only). Using the `{upsert: true}` option will return a new document if one is not found with the given query. | ||
### Loading | ||
@@ -264,2 +266,3 @@ Both the load and delete methods following closely (but not always exactly) to the MongoDB API, so it should feel fairly familiar. To retrieve an object, you have a few methods available to you. | ||
- `.deleteMany(query, options)` (static method) | ||
- `.loadOneAndDelete(query, options)` (static method) | ||
@@ -266,0 +269,0 @@ The `.delete()` method should only be used on an instantiated document with a valid `id`. The other two methods should be used on the class of the document(s) you want to delete. |
@@ -67,2 +67,52 @@ "use strict"; | ||
describe('#loadOneAndUpdate()', function() { | ||
it('should load and update a single object from the collection', function(done) { | ||
var data = getData1(); | ||
data.save().then(function() { | ||
validateId(data); | ||
return Data.loadOneAndUpdate({number: 1}, {source: 'wired'}); | ||
}).then(function(d) { | ||
validateId(d); | ||
expect(d.number).to.equal(1); | ||
expect(d.source).to.equal('wired'); | ||
}).then(done, done); | ||
}); | ||
it('should insert a single object to the collection', function(done) { | ||
Data.loadOne({number: 1}).then(function(d) { | ||
expect(d).to.be.null; | ||
return Data.loadOneAndUpdate({number: 1}, {number: 1}, {upsert: true}); | ||
}).then(function(data) { | ||
validateId(data); | ||
expect(data.number).to.equal(1); | ||
return Data.loadOne({number: 1}); | ||
}).then(function(d) { | ||
validateId(d); | ||
expect(d.number).to.equal(1); | ||
}).then(done, done); | ||
}); | ||
}); | ||
describe('#loadOneAndDelete()', function() { | ||
it('should load and delete a single object from the collection', function(done) { | ||
var data = getData1(); | ||
data.save().then(function() { | ||
validateId(data); | ||
return Data.count({ number: 1 }); | ||
}).then(function(count) { | ||
expect(count).to.be.equal(1); | ||
return Data.loadOneAndDelete({number: 1}); | ||
}).then(function(numDeleted) { | ||
expect(numDeleted).to.equal(1); | ||
return Data.count({ number: 1 }); | ||
}).then(function(count) { | ||
expect(count).to.equal(0); | ||
}).then(done, done); | ||
}); | ||
}); | ||
describe('#loadMany()', function() { | ||
@@ -69,0 +119,0 @@ it('should load multiple objects from the collection', function(done) { |
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
203293
4836
344