Comparing version 0.0.8 to 0.0.9
@@ -35,13 +35,10 @@ "use strict"; | ||
return TaskMdl.find().exec() | ||
.then(function(tasks){ | ||
tasks.forEach(function(task){ | ||
chain = chain.then(function(){ | ||
return rollBackTask(task); | ||
}); | ||
return TaskMdl.find().exec().then(function (tasks) { | ||
tasks.forEach(function (task) { | ||
chain = chain.then(function () { | ||
return rollBackTask(task); | ||
}); | ||
return chain; | ||
}); | ||
return chain; | ||
}); | ||
} | ||
@@ -91,7 +88,5 @@ | ||
return Collection.remove({_id: save.dataStore[0]._id}) | ||
.exec() | ||
.then(function(){ | ||
return updateState(task, save.index, ROLLED); | ||
}); | ||
return Collection.remove({_id: save.dataStore[0]._id}).exec().then(function () { | ||
return updateState(task, save.index, ROLLED); | ||
}); | ||
} | ||
@@ -105,12 +100,10 @@ | ||
chain = chain.then(function(){ | ||
return Collection.findById(data._id) | ||
.exec() | ||
.then(function(doc){ | ||
if(doc) { | ||
return doc.update(data).exec(); | ||
} | ||
return Collection.findById(data._id).exec().then(function (doc) { | ||
if (doc) { | ||
return doc.update(data).exec(); | ||
} | ||
doc = new Collection(data); | ||
return doc.save(); | ||
}); | ||
doc = new Collection(data); | ||
return doc.save(); | ||
}); | ||
}); | ||
@@ -117,0 +110,0 @@ }); |
@@ -45,8 +45,10 @@ "use strict"; | ||
task.initModel = function(modelName, schema){ | ||
if(!(modelName && schema)) throw new Error("Missing Args: modelName, Schema, or both"); | ||
// if(!(modelName && schema)) throw new Error("Missing Args: modelName, Schema, or both"); | ||
if(modelCache[modelName]) throw new Error("The schema for this model has already been set"); | ||
if(!isObject(schema)) throw new Error("Invalid Schema"); | ||
if (schema && !isObject(schema)) throw new Error("Invalid Schema"); | ||
setModel(modelName, new Schema(schema, {strict: true})); | ||
var DEFAULT_SCHEMA = new Schema({}, {strict: false}); | ||
setModel(modelName, schema ? new Schema(schema, {strict: true}) : DEFAULT_SCHEMA); | ||
return task; | ||
@@ -155,22 +157,17 @@ }; | ||
return dbTask | ||
.save() | ||
.then(function(_task){ | ||
_task.steps.forEach(function(step){ | ||
chain = chain.then(function(){ | ||
return getResolveFunc(step)(step, _task); | ||
}); | ||
return dbTask.save().then(function (_task) { | ||
_task.steps.forEach(function (step) { | ||
chain = chain.then(function () { | ||
return getResolveFunc(step)(step, _task); | ||
}); | ||
}); | ||
return chain | ||
.then(function(){ | ||
return _task.remove(); | ||
}) | ||
.catch(function(err){ | ||
return Roller.rollOne(_task) | ||
.then(function(){ | ||
throw err; | ||
}); | ||
}); | ||
return chain.then(function () { | ||
return _task.remove(); | ||
}).catch(function (err) { | ||
return Roller.rollOne(_task).then(function () { | ||
throw err; | ||
}); | ||
}); | ||
}); | ||
}; | ||
@@ -219,9 +216,7 @@ }; | ||
return updateState(task, save.index, PENDING) | ||
.then(function(_task){ | ||
return doc.save() | ||
.then(function(){ | ||
return updateState(_task, save.index, DONE); | ||
}); | ||
return updateState(task, save.index, PENDING).then(function (_task) { | ||
return doc.save().then(function () { | ||
return updateState(_task, save.index, DONE); | ||
}); | ||
}); | ||
} | ||
@@ -234,13 +229,9 @@ | ||
return storeOldData(remove, task) | ||
.then(function(){ | ||
return updateState(task, remove.index, PENDING) | ||
.then(function(_task){ | ||
return Collection.remove(remove.condition) | ||
.exec() | ||
.then(function(){ | ||
return updateState(_task, remove.index, DONE); | ||
}); | ||
}); | ||
return storeOldData(remove, task).then(function () { | ||
return updateState(task, remove.index, PENDING).then(function (_task) { | ||
return Collection.remove(remove.condition).exec().then(function () { | ||
return updateState(_task, remove.index, DONE); | ||
}); | ||
}); | ||
}); | ||
} | ||
@@ -247,0 +238,0 @@ |
@@ -12,3 +12,2 @@ "use strict"; | ||
var Schema = mongoose.Schema; | ||
var DEFAULT_SCHEMA = new Schema({}, {strict: false}); | ||
@@ -21,3 +20,7 @@ function updateState(task, index, state){ | ||
function getCollection(name, schema){ | ||
return mongoose.model(name, schema || DEFAULT_SCHEMA, name); | ||
if (schema) { | ||
return mongoose.model(name, schema); | ||
} | ||
return mongoose.model(name); | ||
} | ||
@@ -24,0 +27,0 @@ |
{ | ||
"name": "fawn", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Library for atomic-ish operations in MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -143,3 +143,3 @@ # Fawn | ||
> schema (required): Same as object passed to [mongoose Schema](http://mongoosejs.com/docs/guide.html#definition). Also see [validation](http://mongoosejs.com/docs/validation.html) | ||
> schema (optional): Same as object passed to [mongoose Schema](http://mongoosejs.com/docs/guide.html#definition). Also see [validation](http://mongoosejs.com/docs/validation.html) | ||
@@ -146,0 +146,0 @@ <br>If you're using mongoose, define your models with mongoose wherever possible. If the model has been defined by mongoose before this function is called, mongoose will throw an OverwriteModelError and if it was defined by Fawn, Fawn will throw an Error. Models can be defined only once. |
@@ -20,4 +20,2 @@ "use strict"; | ||
global.TEST_COLLECTION_B = config.TEST_COLLECTION_B; | ||
global.TestMdlA = utils.getModel(TEST_COLLECTION_A); | ||
global.TestMdlB = utils.getModel(TEST_COLLECTION_B); | ||
@@ -30,2 +28,9 @@ describe("ALL TESTS", function(){ | ||
global.taskMdl = task.getTaskCollection(); | ||
task.initModel(TEST_COLLECTION_A); | ||
task.initModel(TEST_COLLECTION_B); | ||
global.TestMdlA = utils.getModel(TEST_COLLECTION_A); | ||
global.TestMdlB = utils.getModel(TEST_COLLECTION_B); | ||
}); | ||
@@ -32,0 +37,0 @@ |
34470
733