Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Commit

Permalink
Merge 85dc076 into d8fa566
Browse files Browse the repository at this point in the history
  • Loading branch information
blugavere committed Sep 21, 2017
2 parents d8fa566 + 85dc076 commit ee5d69a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 37 deletions.
83 changes: 51 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

'use strict';

const autoBind = require('auto-bind');
const pg = require('polygoat');

/**
* IRepository implementation for Mongoose
* @class MongooseRepository
*/
class MongooseRepository {
constructor(mongoose, model) {
if (!mongoose || !model) {throw new Error('Mongoose model type cannot be null.');}
if (!mongoose || !model) {
throw new Error('Mongoose model type cannot be null.');
}
this.mongoose = mongoose;

if (typeof model === 'string') {
Expand All @@ -26,7 +29,7 @@ class MongooseRepository {
* @returns {void}
*/
clear(cb) {
this.collection.find({}).remove(cb);
return pg(done => this.collection.find({}).remove(done), cb);
}

/**
Expand All @@ -35,7 +38,7 @@ class MongooseRepository {
* @returns {void}
*/
count(cb) {
this.collection.count(cb);
return pg(done => this.collection.count(done), cb);
}

/**
Expand All @@ -44,7 +47,7 @@ class MongooseRepository {
* @returns {void}
*/
disconnect(cb) {
this.mongoose.connection.close(cb);
return pg(done => this.mongoose.connection.close(done), cb);
}

/**
Expand All @@ -53,11 +56,13 @@ class MongooseRepository {
* @returns {void}
*/
findAll(cb) {
this.collection.find({}).exec((err, res) => {
if (err) {return cb(err);}
return pg(done => this.collection.find({}).exec((err, res) => {
if (err) {
return done(err);
}
const obj = JSON.parse(JSON.stringify(res));
return cb(null, obj);
});
return done(null, obj);
}), cb);
}

/**
Expand All @@ -68,12 +73,14 @@ class MongooseRepository {
*/
findOne(id, cb) {
const self = this;
self.collection.findOne({
return pg(done => self.collection.findOne({
_id: id
}).lean().exec((err, res) => {
if (err) { return cb(err); }
cb(null, JSON.parse(JSON.stringify(res)));
});
if (err) {
return cb(err);
}
done(null, JSON.parse(JSON.stringify(res)));
}), cb);
}

/**
Expand All @@ -83,11 +90,13 @@ class MongooseRepository {
* @returns {void}
*/
add(entity, cb) {
this.collection.create(entity, (err, res) => {
if (err) {return cb(err);}
return pg(done => this.collection.create(entity, (err, res) => {
if (err) {
return done(err);
}
const obj = JSON.parse(JSON.stringify(res));
cb(null, obj);
});
done(null, obj);
}), cb);
}

/**
Expand All @@ -98,15 +107,21 @@ class MongooseRepository {
* @returns {void}
*/
patch(id, obj, cb) {
this.collection.findOneAndUpdate(
{ _id: id },
{ $set: obj },
{ new: true, lean: true },
return pg(done => this.collection.findOneAndUpdate({
_id: id
}, {
$set: obj
}, {
new: true,
lean: true
},
(err, res) => {
if (err) { return cb(err); }
if (err) {
return done(err);
}
const obj = JSON.parse(JSON.stringify(res));
cb(null, obj);
});
done(null, obj);
}), cb);
}

/**
Expand All @@ -117,15 +132,17 @@ class MongooseRepository {
*/
update(entity, cb) {
const self = this;
self.collection.findByIdAndUpdate(entity._id, entity, {
return pg(done => self.collection.findByIdAndUpdate(entity._id, entity, {
new: true,
passRawResult: true,
lean: true
}).exec((err, res) => {
if (err) { return cb(err); }
if (err) {
return done(err);
}
const obj = JSON.parse(JSON.stringify(res));
cb(null, obj);
});
done(null, obj);
}), cb);
}

/**
Expand All @@ -136,11 +153,13 @@ class MongooseRepository {
*/
remove(id, cb) {
const self = this;
self.collection.findByIdAndRemove(id, (err, res) => {
if (err) {return cb(err);}
return pg(done => self.collection.findByIdAndRemove(id, (err, res) => {
if (err) {
return done(err);
}
const obj = JSON.parse(JSON.stringify(res));
cb(null, obj);
});
done(null, obj);
}), cb);
}
}

Expand Down
105 changes: 102 additions & 3 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MongooseRepository = require('.');

/**
* to run this test standalone:
* mocha lib/MongooseRepository.test.js --watch
* mocha lib/index.test.js --watch
*/

describe('Mongoose Repository', () => {
Expand All @@ -21,7 +21,9 @@ describe('Mongoose Repository', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
for (const i in Model) {
if (i !== 'toString') { sandbox.stub(Model, i); }
if (i !== 'toString') {
sandbox.stub(Model, i);
}
}
repo = new MongooseRepository(mongoose, Model);
});
Expand All @@ -37,7 +39,7 @@ describe('Mongoose Repository', () => {
expect(() => new MongooseRepository(mongoose, 'Cat')).toNotThrow(); //getting mongoose model
});
it('should should take a func arg', () => {
expect(() => new MongooseRepository(mongoose, () => { })).toNotThrow();
expect(() => new MongooseRepository(mongoose, () => {})).toNotThrow();
});
});

Expand Down Expand Up @@ -225,4 +227,101 @@ describe('Mongoose Repository', () => {

});

describe('Promise Impementation', () => {
describe('findAll', () => {
it('should return an array', () => {
Model.find.returns(Model);
Model.lean.returns(Model);
Model.exec.yields(null, []);
return repo.findAll().then(res => {
expect(Model.find.called).toBe(true);
expect(Model.exec.called).toBe(true);
expect(res).toEqual([]);
});
});
});

describe('findOne', () => {
it('should return an object', () => {
Model.findOne.returns(Model);
Model.lean.returns(Model);
Model.exec.yields(null, new Model());
return repo.findOne('foo').then(res => {
expect(Model.findOne.called).toBe(true);
expect(Model.lean.called).toBe(true);
expect(Model.exec.called).toBe(true);
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});

describe('add', () => {
it('should add an object', () => {
Model.create.yields(null, new Model());
return repo.add({}).then(res => {
expect(Model.create.called).toBe(true, 'create method not called');
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});

describe('remove', () => {
it('should remove an object', () => {
Model.findByIdAndRemove.yields(null, new Model());
return repo.remove({
_id: 'foo'
}, (err, res) => {
assert(!err);
expect(Model.findByIdAndRemove.called).toBe(true, 'remove method not called');
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});
describe('update', () => {
it('should update an object', () => {
Model.findByIdAndUpdate.returns(Model);
Model.exec.yields(null, new Model());
return repo.update({
_id: 'foo'
}).then(res => {
expect(Model.findByIdAndUpdate.called).toBe(true, 'create method not called');
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});

describe('patch', () => {
it('should patch an object', () => {
Model.findOneAndUpdate.yields(null, new Model());
return repo.patch('abc', {}).then(res => {
expect(Model.findOneAndUpdate.called).toBe(true);
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});

describe('clear', () => {
it('should clear a collection', () => {
Model.find.returns(Model);
Model.remove.yields(null, {});
return repo.clear().then(res => {
expect(Model.remove.called).toBe(true, 'remove method not called');
expect(typeof res).toEqual('object');
expect(res instanceof Mockgoose).toEqual(false);
expect(res).toEqual({});
});
});
});

});
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"email": "b.lugavere@gmail.com",
"url": "http://benlugavere.com/#/"
},
"contributors": [],
"typings": "mongoose-repository.d.ts",
"files": [
"dist",
Expand Down Expand Up @@ -46,6 +45,7 @@
"repository": "blugavere/mongoose-repository",
"license": "MIT",
"dependencies": {
"auto-bind": "^1.1.0"
"auto-bind": "^1.1.0",
"polygoat": "1.1.4"
}
}

0 comments on commit ee5d69a

Please sign in to comment.