cosa
Advanced tools
Comparing version 2.3.5 to 2.3.6
@@ -24,2 +24,31 @@ var util = require('util'); | ||
function normalizeResult (r) { | ||
var result = { | ||
result: r.result, | ||
ops: r.ops | ||
}; | ||
if ('undefined' !== typeof r.insertedCount) { | ||
result.insertedCount = r.insertedCount; | ||
} | ||
if ('undefined' !== typeof r.insertedIds) { | ||
result.insertedIds = r.insertedIds; | ||
} | ||
if ('undefined' !== typeof r.matchedCount) { | ||
result.matchedCount = r.matchedCount; | ||
} | ||
if ('undefined' !== typeof r.modifiedCount) { | ||
result.modifiedCount = r.modifiedCount; | ||
} | ||
if ('undefined' !== typeof r.upsertedCount) { | ||
result.upsertedCount = r.upsertedCount; | ||
} | ||
if ('undefined' !== typeof r.deletedCount) { | ||
result.deletedCount = r.deletedCount; | ||
} | ||
if ('undefined' !== typeof r.upsertedIds) { | ||
result.upsertedIds = r.upsertedIds; | ||
} | ||
return result; | ||
} | ||
Database.prototype.init = function (uri) { | ||
@@ -37,2 +66,4 @@ var deferred = q.defer(); | ||
debug('failed to connect', err); | ||
this._db = null; | ||
this._connectionStatus = 'disconnected'; | ||
return deferred.reject(err); | ||
@@ -42,2 +73,8 @@ } | ||
this._connectionStatus = 'connected'; | ||
this._db.on('close', function () { | ||
debug('database connection closed'); | ||
this._db = null; | ||
this._connectionStatus = 'disconnected'; | ||
this.emit('disconnect'); | ||
}.bind(this)); | ||
this.emit('connect', this); | ||
@@ -94,2 +131,3 @@ debug('connected to database'); | ||
.then(function (collection) { | ||
var findOptions; | ||
if (options.count) { | ||
@@ -100,7 +138,7 @@ var countOptions = { limit: options.limit, skip: options.skip, sort: options.sort }; | ||
} else if (options.findOne) { | ||
var findOptions = { fields: options.fields, limit: options.limit, skip: options.skip, sort: options.sort }; | ||
findOptions = { fields: options.fields, limit: options.limit, skip: options.skip, sort: options.sort }; | ||
debug('db.' + collection.collectionName + '.findOne', query, findOptions); | ||
return collection.findOne(query, options); | ||
} else { | ||
var findOptions = { fields: options.fields, limit: options.limit, skip: options.skip, sort: options.sort }; | ||
findOptions = { fields: options.fields, limit: options.limit, skip: options.skip, sort: options.sort }; | ||
debug('db.' + collection.collectionName + '.find (toArray)', query, findOptions); | ||
@@ -110,3 +148,3 @@ return collection.find(query, options); | ||
}); | ||
} | ||
}; | ||
@@ -134,3 +172,3 @@ Database.prototype.insert = function (collection, docs) { | ||
}); | ||
} | ||
}; | ||
@@ -164,3 +202,3 @@ Database.prototype.update = function (collection, query, update, options) { | ||
}); | ||
} | ||
}; | ||
@@ -191,3 +229,3 @@ Database.prototype.remove = function (collection, query, options) { | ||
}); | ||
} | ||
}; | ||
@@ -209,3 +247,3 @@ Database.prototype.aggregate = function (collection, pipeline, options) { | ||
}); | ||
} | ||
}; | ||
@@ -225,34 +263,4 @@ Database.prototype.distinct = function (collection, key, query, options) { | ||
}); | ||
} | ||
}; | ||
function normalizeResult (r) { | ||
var result = { | ||
result: r.result, | ||
ops: r.ops | ||
}; | ||
if ('undefined' !== typeof r.insertedCount) { | ||
result.insertedCount = r.insertedCount; | ||
} | ||
if ('undefined' !== typeof r.insertedIds) { | ||
result.insertedIds = r.insertedIds; | ||
} | ||
if ('undefined' !== typeof r.matchedCount) { | ||
result.matchedCount = r.matchedCount; | ||
} | ||
if ('undefined' !== typeof r.modifiedCount) { | ||
result.modifiedCount = r.modifiedCount; | ||
} | ||
if ('undefined' !== typeof r.upsertedCount) { | ||
result.upsertedCount = r.upsertedCount; | ||
} | ||
if ('undefined' !== typeof r.deletedCount) { | ||
result.deletedCount = r.deletedCount; | ||
} | ||
if ('undefined' !== typeof r.upsertedIds) { | ||
result.upsertedIds = r.upsertedIds; | ||
} | ||
return result; | ||
} | ||
module.exports = new Database(); |
@@ -23,3 +23,3 @@ var defaults = require('defaults'); | ||
isImmutable: function (value) { | ||
if ('undefined' === typeof value) { return true; } | ||
if ('undefined' === typeof value || value === null) { return true; } | ||
return (IMMUTABLE_TYPES.indexOf(typeof value) > -1 || !!value.__immutable); | ||
@@ -93,6 +93,6 @@ }, | ||
options.get = ('function' === typeof getterOrValue) ? | ||
getterOrValue : function () { return Immutable.create(getterOrValue); } | ||
getterOrValue : function () { return Immutable.create(getterOrValue); }; | ||
options.set = function () { | ||
throw new Error('Cannot modify ' + name + ' of immutable ' + this.type); | ||
} | ||
}; | ||
this.props[name] = options; | ||
@@ -99,0 +99,0 @@ }; |
@@ -155,3 +155,3 @@ var q = require('q'); | ||
}; | ||
} | ||
@@ -166,4 +166,4 @@ function _create (data, definition) { | ||
obj.__modified = obj.__modified.filter(function (value) { | ||
shouldAdd = shouldAdd && !(path.indexOf(value + '.', 0) === 0); | ||
return !(value.indexOf(path + '.', 0) === 0); | ||
shouldAdd = shouldAdd && path.indexOf(value + '.', 0) !== 0; | ||
return value.indexOf(path + '.', 0) !== 0; | ||
}); | ||
@@ -301,3 +301,3 @@ if (shouldAdd) { | ||
err = errors.Validation(err); | ||
return deferred.reject(err); | ||
return deferred.reject(err); | ||
} | ||
@@ -320,3 +320,3 @@ deferred.resolve(value); | ||
.then(function (obj) { | ||
return this._validate(obj, options) | ||
return this._validate(obj, options); | ||
}.bind(this)) | ||
@@ -387,3 +387,3 @@ .then(function (value) { | ||
return Immutable.create(data, { definition: definition }); | ||
}; | ||
} | ||
@@ -399,3 +399,3 @@ function _extend (definition, superDef) { | ||
return define(newDef); | ||
}; | ||
} | ||
@@ -402,0 +402,0 @@ module.exports = { |
{ | ||
"name": "cosa", | ||
"version": "2.3.5", | ||
"version": "2.3.6", | ||
"description": "Cosa Models for MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -359,2 +359,58 @@ var chai = require('chai'); | ||
describe('db', function () { | ||
var _db, db = require('../lib/db'); | ||
before(function (done) { | ||
MongoClient.connect(process.env.COSA_DB_URI, function (err, db) { | ||
if (err) { return done(err); } | ||
_db = db; | ||
_db.collection('mocha_test', function (err, collection) { | ||
collection.deleteMany({}, function (err, result) { | ||
if (err) { return done(err); } | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
after(function (done) { | ||
_db.collection('mocha_test', function (err, collection) { | ||
collection.deleteMany({}, function (err, result) { | ||
if (err) { return done(err); } | ||
_db.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should auto connect to db if connection lost', function (done) { | ||
var model = FullTestModel.create({ | ||
str: 'foo', | ||
obj: { prop1: 'bar' } | ||
}); | ||
model | ||
.save() | ||
.then(function (updatedModel) { | ||
return q.all([ | ||
updatedModel, | ||
FullTestModel.count({ _id: updatedModel._id }) | ||
]); | ||
}) | ||
.spread(function (updatedModel, count) { | ||
expect(count).to.equal(1); | ||
db._db.close(); | ||
return updatedModel; | ||
}) | ||
.then(function(updatedModel) { | ||
return FullTestModel.count({ _id: updatedModel._id }); | ||
}) | ||
.then(function (count) { | ||
expect(count).to.equal(1); | ||
done(); | ||
}) | ||
.done(null, done); | ||
}); | ||
}); | ||
describe('.save()', function () { | ||
@@ -371,4 +427,9 @@ var _db, model; | ||
_db = db; | ||
done(); | ||
}) | ||
_db.collection('mocha_test', function (err, collection) { | ||
collection.deleteMany({}, function (err, result) { | ||
if (err) { return done(err); } | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}) | ||
@@ -375,0 +436,0 @@ |
Sorry, the diff of this file is not supported yet
88717
2538
21