Comparing version 2.0.1 to 2.1.0
@@ -134,3 +134,3 @@ "use strict"; | ||
function rollbackSave(db, save, task) { | ||
var collection = db.collection(save.name); | ||
var collection = getCollection(db, save); | ||
var _id = save.dataStore[0]._id; | ||
@@ -153,3 +153,3 @@ | ||
function rollbackRemoveOrUpdate(db, step, task) { | ||
var collection = db.collection(step.name); | ||
var collection = getCollection(db, step); | ||
var chain = Promise.resolve(); | ||
@@ -239,2 +239,16 @@ | ||
/** | ||
* Gets the collection for a step | ||
* | ||
* @param db native db | ||
* @param step the step in question | ||
* | ||
* @returns native collection | ||
*/ | ||
function getCollection(db, step){ | ||
return step.useMongoose | ||
? mongoose.model(step.name).collection | ||
: db.collection(step.name) | ||
} | ||
module.exports = RollerProvider; |
@@ -48,9 +48,16 @@ /** | ||
return updateState(task, step.index, PENDING).then(function () { | ||
return Collection.update(condition, data, step.options) | ||
.exec() | ||
.then(function (result) { | ||
results.push(result); | ||
var update; | ||
return updateState(task, step.index, DONE, results); | ||
}); | ||
if(step.useMongoose && step.options && step.options.viaSave){ | ||
var doc = new Collection(data); | ||
doc.isNew = false; | ||
update = doc.save(); | ||
} | ||
else update = Collection.update(condition, data, step.options).exec(); | ||
return update.then(function (result) { | ||
results.push(result); | ||
return updateState(task, step.index, DONE, results); | ||
}); | ||
}); | ||
@@ -88,5 +95,3 @@ }); | ||
return updateState(task, step.index, DONE, results).then(function(){ | ||
return Promise.resolve(results); | ||
}); | ||
return updateState(task, step.index, DONE, results) | ||
}); | ||
@@ -93,0 +98,0 @@ }); |
@@ -18,2 +18,3 @@ /** | ||
, options: {} | ||
, useMongoose: Boolean | ||
}] | ||
@@ -20,0 +21,0 @@ }); |
{ | ||
"name": "fawn", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Promise based library for transactions in MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -93,3 +93,3 @@ ## [![Travis](https://img.shields.io/travis/e-oj/Fawn.svg?style=flat-square)](https://travis-ci.org/e-oj/Fawn) [![npm](https://img.shields.io/npm/l/fawn.svg?style=flat-square)](https://www.npmjs.com/package/fawn) [![npm](https://img.shields.io/npm/v/fawn.svg?style=flat-square)](https://www.npmjs.com/package/fawn) [![npm](https://img.shields.io/npm/dt/fawn.svg?style=flat-square)](https://www.npmjs.com/package/fawn) | ||
task.update("Accounts", {firstName: "The", lastName: "Plug"}, {$inc: {balance: 20}}) | ||
task.run({useMongoose: true) | ||
task.run({useMongoose: true}) | ||
.then(function(){ | ||
@@ -154,3 +154,3 @@ // update is complete | ||
Without mongoose, Initialze Fawn like so: | ||
Without mongoose, Initialize Fawn like so: | ||
@@ -264,3 +264,3 @@ ```javascript | ||
> options (required): Update options - same as in [mongoose][] | ||
> options (required): Update options = [mongoose][] options + {viaSave: Boolean} | ||
@@ -279,2 +279,24 @@ <br> Attach to update call as shown | ||
The <b>viaSave</b> option allows you update a <b><i>mongoose</i></b> document using the save function. It's useful if you want to trigger mongoose pre save hooks. <b><i>For this option to work you must run the task using mongoose</i></b> | ||
with mongoose: | ||
```javascript | ||
var doc = someMongooseDocument; | ||
doc.someProperty = newValue; | ||
doc.save().then(console.log); | ||
``` | ||
with Fawn: | ||
```javascript | ||
var doc = someMongooseDocument; | ||
var newDoc = doc.toObject; | ||
newDoc.someProperty = newValue | ||
task.update(doc, newDoc) | ||
.options({viaSave: true}) | ||
.run({useMongoose: true}) | ||
.then(console.log); | ||
``` | ||
*Note: No changes will be made to to your database until you call task.run()* | ||
@@ -281,0 +303,0 @@ |
@@ -10,3 +10,4 @@ "use strict"; | ||
, TEST_COLLECTION_A: "humans" | ||
, TEST_COLLECTION_B: "animals" | ||
, TEST_COLLECTION_B: "pets" | ||
, TEST_COLLECTION_C: "animal" | ||
, TEST_FILE_TEXT: "This text is used to test file features" | ||
@@ -13,0 +14,0 @@ , TEST_FILE_PATH: "./test.oj" |
481
1
56330
17
1303