Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mongoose to the latest version 🚀 #228

Merged
merged 3 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
const errors = require('@feathersjs/errors');

module.exports = function errorHandler (error) {
if (error.code === 11000 || error.code === 11001) {
// NOTE (EK): Error parsing as discussed in this github thread
// https://github.com/Automattic/mongoose/issues/2129
const match1 = error.message.match(/_?([a-zA-Z]*)_?\d?\s*dup key/i);
const match2 = error.message.match(/\s*dup key:\s*\{\s*:\s*"?(\S+)"?\s*\}/i);

const key = match1 ? match1[1] : 'path';
let value = match2 ? match2[1] : 'value';

if (value === 'null') {
value = null;
} else if (value === 'undefined') {
value = undefined;
}

error.message = `${key}: ${value} already exists.`;
error.errors = {
[key]: value
};

return Promise.reject(new errors.Conflict(error));
}

if (error.name) {
switch (error.name) {
case 'ValidationError':
Expand All @@ -14,29 +37,6 @@ module.exports = function errorHandler (error) {
case 'DivergentArrayError':
return Promise.reject(new errors.GeneralError(error));
case 'MongoError':
if (error.code === 11000 || error.code === 11001) {
// NOTE (EK): Error parsing as discussed in this github thread
// https://github.com/Automattic/mongoose/issues/2129
const match1 = error.message.match(/_?([a-zA-Z]*)_?\d?\s*dup key/i);
const match2 = error.message.match(/\s*dup key:\s*\{\s*:\s*"?(\S+)"?\s*\}/i);

const key = match1 ? match1[1] : 'path';
let value = match2 ? match2[1] : 'value';

if (value === 'null') {
value = null;
} else if (value === 'undefined') {
value = undefined;
}

error.message = `${key}: ${value} already exists.`;
error.errors = {
[key]: value
};

return Promise.reject(new errors.Conflict(error));
}

return Promise.reject(new errors.GeneralError(error));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Service {
// we create a list of the ids of all items that will be changed
// to re-query them after the update
const ids = id === null ? this._find(params)
.then(mapIds) : Promise.resolve([ id ]);
.then(mapIds) : Promise.resolve([ id ]);

// Handle case where data might be a mongoose model
if (typeof data.toObject === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"feathers-service-tests": "^0.10.0",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^5.0.0",
"mongoose": "^4.10.4",
"mongoose": "^5.0.0",
"semistandard": "^12.0.0",
"sinon": "^4.0.0",
"sinon-chai": "^2.8.0"
Expand Down
72 changes: 36 additions & 36 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,39 +404,39 @@ describe('Feathers Mongoose Service', () => {

afterEach(done => {
posts.remove(null, { query: {} })
.then(data => {
done();
});
.then(data => {
done();
});
});

it('can get a discriminated model', function (done) {
posts.create(data)
.then(data => posts.get(data._id))
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Feathers!!!');
done();
});
.then(data => posts.get(data._id))
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Feathers!!!');
done();
});
});

it('can find discriminated models by the type', function (done) {
posts.create(data)
.then(data => posts.find({ query: { _type: 'text' } }))
.then(data => {
data.forEach(element => {
expect(element._type).to.equal('text');
.then(data => posts.find({ query: { _type: 'text' } }))
.then(data => {
data.forEach(element => {
expect(element._type).to.equal('text');
});
done();
});
done();
});
});

it('can create a discriminated model', function (done) {
posts.create(data)
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Feathers!!!');
done();
});
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Feathers!!!');
done();
});
});

it('can update a discriminated model', function (done) {
Expand All @@ -452,12 +452,12 @@ describe('Feathers Mongoose Service', () => {
}
};
posts.create(data)
.then(data => posts.update(data._id, update, params))
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Hello, world!');
done();
});
.then(data => posts.update(data._id, update, params))
.then(data => {
expect(data._type).to.equal('text');
expect(data.text).to.equal('Hello, world!');
done();
});
});

it('can patch a discriminated model', function (done) {
Expand All @@ -470,20 +470,20 @@ describe('Feathers Mongoose Service', () => {
}
};
posts.create(data)
.then(data => posts.patch(data._id, update, params))
.then(data => {
expect(data.text).to.equal('Howdy folks!');
done();
});
.then(data => posts.patch(data._id, update, params))
.then(data => {
expect(data.text).to.equal('Howdy folks!');
done();
});
});

it('can remove a discriminated model', function (done) {
posts.create(data)
.then(data => posts.remove(data._id, { query: { _type: 'text' } }))
.then(data => {
expect(data._type).to.equal('text');
done();
});
.then(data => posts.remove(data._id, { query: { _type: 'text' } }))
.then(data => {
expect(data._type).to.equal('text');
done();
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/models/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Schema = mongoose.Schema;

var TodoSchema = new Schema({
text: {type: String, required: true},
complete: {type: Boolean, 'default': false, index: true},
complete: {type: Boolean, 'default': false},
createdAt: {type: Date, 'default': Date.now},
updatedAt: {type: Date, 'default': Date.now}
});
Expand Down