Skip to content

Commit e5c8964

Browse files
author
emmanuel
committed
fixed OverwriteModel bug
1 parent 46bb0fd commit e5c8964

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ var task = Fawn.Task();
140140

141141
> modelName (required): Name of the collection associated with this model
142142
143-
> 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)
143+
> 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)
144144
145145
<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.
146146

lib/task.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ var Task = function(){
4242
};
4343

4444
task.initModel = function(modelName, schema){
45-
if(!(modelName && schema)) throw new Error("Missing Args: modelName, Schema, or both");
45+
// if(!(modelName && schema)) throw new Error("Missing Args: modelName, Schema, or both");
4646
if(modelCache[modelName]) throw new Error("The schema for this model has already been set");
47-
if(!isObject(schema)) throw new Error("Invalid Schema");
47+
if (schema && !isObject(schema)) throw new Error("Invalid Schema");
4848

49-
setModel(modelName, new Schema(schema, {strict: true}));
49+
var DEFAULT_SCHEMA = new Schema({}, {strict: false});
50+
51+
setModel(modelName, schema ? new Schema(schema, {strict: true}) : DEFAULT_SCHEMA);
5052

5153
return task;
5254
};

lib/utils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ var modelCache = {};
99
module.exports = function(_mongoose){
1010
var mongoose = _mongoose || require("mongoose");
1111
var Schema = mongoose.Schema;
12-
var DEFAULT_SCHEMA = new Schema({}, {strict: false});
1312

1413
function updateState(task, index, state){
1514
task.steps[index].state = state;
1615
return task.save();
1716
}
1817

1918
function getCollection(name, schema){
20-
return mongoose.model(name, schema || DEFAULT_SCHEMA, name);
19+
if (schema) {
20+
return mongoose.model(name, schema);
21+
}
22+
23+
return mongoose.model(name);
2124
}
2225

2326
function setModel(name, schema){

tests/all.tests.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ global.expect = config.expect;
1717
global.Promise = config.Promise;
1818
global.TEST_COLLECTION_A = config.TEST_COLLECTION_A;
1919
global.TEST_COLLECTION_B = config.TEST_COLLECTION_B;
20-
global.TestMdlA = utils.getModel(TEST_COLLECTION_A);
21-
global.TestMdlB = utils.getModel(TEST_COLLECTION_B);
2220

2321
describe("ALL TESTS", function(){
2422
before(function(){
2523
Lint.init(config.db + DB, TASKS);
2624
global.Task = Lint.Task;
2725
global.task = Lint.Task();
2826
global.taskMdl = task.getTaskCollection();
27+
28+
task.initModel(TEST_COLLECTION_A);
29+
task.initModel(TEST_COLLECTION_B);
30+
31+
global.TestMdlA = utils.getModel(TEST_COLLECTION_A);
32+
global.TestMdlB = utils.getModel(TEST_COLLECTION_B);
33+
2934
});
3035

3136
after(function(){

0 commit comments

Comments
 (0)