Comparing version 1.1.0 to 1.2.0
@@ -18,2 +18,3 @@ "use strict"; | ||
, DEFAULT_COLLECTION: "OJLINTTASKCOLLECTION" | ||
, OJ_FUTURE: "$ojFuture" | ||
}; |
@@ -71,11 +71,7 @@ "use strict"; | ||
var clean = {}; | ||
var properties = ["db", "server", "replset", "user", "pass", "auth", "mongos", "promiseLibrary"]; | ||
if(options.db) clean.db = options.db; | ||
if(options.server) clean.server = options.server; | ||
if(options.replset) clean.replset = options.replset; | ||
if(options.user) clean.user = options.user; | ||
if(options.pass) clean.pass = options.pass; | ||
if(options.auth) clean.auth = options.auth; | ||
if(options.mongos) clean.mongos = options.mongos; | ||
if(options.promiseLibrary) clean.promiseLibrary = options.promiseLibrary; | ||
properties.forEach(function (prop) { | ||
if (options[prop]) clean[prop] = options[prop]; | ||
}); | ||
@@ -82,0 +78,0 @@ return clean; |
@@ -146,2 +146,4 @@ "use strict"; | ||
handle$Token(doc); | ||
var saveObj = { | ||
@@ -285,2 +287,5 @@ index: index | ||
resolveFuture(update.condition, results); | ||
resolveFuture(update.data, results); | ||
// console.log(update); | ||
@@ -314,2 +319,6 @@ return storeOldData(update, task) | ||
var Collection = getModel(save.name); | ||
handle$Token(save.data, true); | ||
resolveFuture(save.data, results); | ||
var doc = new Collection(save.data); | ||
@@ -346,2 +355,3 @@ var dataStore = []; | ||
handle$Token(remove.condition, true); | ||
resolveFuture(remove.condition, results); | ||
@@ -474,2 +484,47 @@ return storeOldData(remove, task).then(function () { | ||
/** | ||
* Replaces a placeholder with real data. | ||
* | ||
* e.g: | ||
* var testResults = [{name: "Bob"}] | ||
* var testObj = {name: {$ojFuture: "0.name"}} | ||
* | ||
* resolveFuture(testObj, testResults); | ||
* | ||
* console.log(testObj) // {name: "Bob"} | ||
* | ||
* @param obj Object that might contain templated data | ||
* @param results results used to replace templated data | ||
*/ | ||
function resolveFuture(obj, results) { | ||
var ojf = constants.OJ_FUTURE; | ||
Object.keys(obj).forEach(function (key) { | ||
if (!isObject(obj[key])) return; | ||
if (!obj[key][ojf]) return resolveFuture(obj[key], results); | ||
var placeholder = obj[key][ojf]; | ||
// format: "index.property.property.-----.property" | ||
var parts = placeholder.split("."); | ||
if (isNaN(parts[0])) throw new Error("index must be a number"); | ||
var index = parseInt(parts[0]); | ||
if (index >= results.length) throw new Error("Invalid index. The result at that index does not exist (yet)"); | ||
var result = results[index]; | ||
for (var i = 1; i < parts.length; i++) { | ||
if (result.toObject) result = result.toObject(); | ||
if (!isObject(result))throw new Error("Can't use keys on non-object"); | ||
if (!result[parts[i]]) throw new Error("No such key exists in result"); | ||
result = result[parts[i]] | ||
} | ||
obj[key] = result; | ||
}); | ||
} | ||
module.exports = TaskProvider; |
{ | ||
"name": "fawn", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Library for atomic-ish operations in MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -45,3 +45,3 @@ # Fawn | ||
//result from second operation | ||
var secondUpdateResult = results[1] | ||
var secondUpdateResult = results[1]; | ||
}) | ||
@@ -281,3 +281,3 @@ .catch(function(err){ | ||
//result from second operation | ||
var secondUpdateResult = results[1] | ||
var secondUpdateResult = results[1]; | ||
}) | ||
@@ -317,2 +317,20 @@ .catch(function(err){ | ||
## <a name="misc"></a>Miscellaneous | ||
### Using the result of previous steps in subsequent steps | ||
You might want to use the result of a previous step in a subsequent step. You can do this using an object with the key "$ojFuture". Syntax: {$ojFuture: "indexOfStep.resultProperty1.property2.-----.propertyN"}. Here's how: | ||
``` | ||
task.save("Kids", {name: {full: "Brody Obi"}}) //result will be {_id: someMongoId, name: {full: "Brody Obi"}} | ||
.update("Parents", {_id: parentId}, {firstChild: {id: {$ojFuture: "0._id"} , fullName: {$ojFuture: "0.name.full"}}) | ||
.run() | ||
.then(function(){ | ||
// task is complete | ||
}); | ||
``` | ||
To use this feature you need to know the exact format of the step's result. For Reference: | ||
- the result of a save is the saved object | ||
- the result of a remove or update is the raw response from mongodb | ||
## <a name="test"></a>Test | ||
@@ -319,0 +337,0 @@ |
@@ -228,5 +228,44 @@ "use strict"; | ||
.run() | ||
).to.eventually.have.length(4); | ||
.then(function (results) { | ||
results.forEach(function (r) { | ||
// console.log("==>", r); | ||
}); | ||
return Promise.resolve(results); | ||
})) | ||
.to.eventually.have.length(4); | ||
}); | ||
}) | ||
}); | ||
describe("Templating tests for future data", function () { | ||
it("task with templated data should run successfully", function () { | ||
var mickey = new TestMdlB({name: "Mickey Mouse", age: 53}); | ||
var mick = new TestMdlA({name: "Mick", age: 3}); | ||
return task.save(mickey) | ||
.save(mick) | ||
.save(TEST_COLLECTION_A, {name: "Alfie", age: {$ojFuture: "1.age"}}) | ||
.save(TEST_COLLECTION_B, {name: "Minnie Mouse", age: {$ojFuture: "0.age"}}) | ||
.update(TEST_COLLECTION_B, {name: {$ojFuture: "0.name"}}, {age: {$ojFuture: "1.age"}}) | ||
.update(TEST_COLLECTION_A, {name: {$ojFuture: "1.name"}}, {age: {$ojFuture: "3.age"}}) | ||
.remove(TEST_COLLECTION_A, {name: {$ojFuture: "2.name"}, age: 3}) | ||
.run(); | ||
}); | ||
it("Should have Mickey Mouse in " + TEST_COLLECTION_B + " with age 3", function () { | ||
return expect(TestMdlB.find({name: "Mickey Mouse", age: 3}).exec()).to.eventually.have.length(1); | ||
}); | ||
it("Should have Mick in " + TEST_COLLECTION_A + " with age 53", function () { | ||
return expect(TestMdlA.find({name: "Mick", age: 53}).exec()).to.eventually.have.length(1); | ||
}); | ||
it("Should have Minnie Mouse in " + TEST_COLLECTION_B + " with age 53", function () { | ||
return expect(TestMdlB.find({name: "Minnie Mouse", age: 53}).exec()).to.eventually.have.length(1); | ||
}); | ||
it("Should not have Alfie in " + TEST_COLLECTION_A, function () { | ||
return expect(TestMdlA.find({name: "Alfie"}).exec()).to.eventually.have.length(0); | ||
}); | ||
}); | ||
}); |
46775
1082
346