Skip to content

Commit

Permalink
Upgrades to mongoose v5
Browse files Browse the repository at this point in the history
  • Loading branch information
viveleroi committed Jan 24, 2018
1 parent e62c915 commit d27cd93
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 72 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const get = require('lodash.get');
// Function typecheck helper
const isFunc = (val) => typeof val === 'function';

var deepPath = function(schema, pathName) {
const deepPath = function(schema, pathName) {
let path;
const paths = pathName.split('.');

Expand All @@ -29,8 +29,12 @@ module.exports = function(schema, options) {
const type = options.type || 'unique';
const message = options.message || 'Error, expected `{PATH}` to be unique. Value: `{VALUE}`';

// Mongoose Schema objects don't describe default _id indexes
// https://github.com/Automattic/mongoose/issues/5998
const indexes = [[{ _id: 1 }, { unique: true }]].concat(schema.indexes());

// Dynamically iterate all indexes
each(schema.indexes(), (index) => {
each(indexes, (index) => {
const indexOptions = index[1];

if (indexOptions.unique) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"chai": "^4.1.2",
"eslint": "^4.16.0",
"mocha": "^5.0.0",
"mongoose": "^4.13.9"
"mongoose": "^5.0.0"
},
"peerDependencies": {
"mongoose": "^4.9.0"
"mongoose": "^5.0.0"
},
"dependencies": {
"lodash.foreach": "^4.1.0",
Expand Down
4 changes: 0 additions & 4 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ module.exports = {

createCustomIdSchema: function() {
return new mongoose.Schema({
_id: {
type: String,
unique: true
},
position: Number
});
},
Expand Down
8 changes: 4 additions & 4 deletions test/tests/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ module.exports = function(mongoose) {

it('throws error on unique violation for custom _id field', function(done) {
var Planet = mongoose.model('Planet', helpers.createCustomIdSchema().plugin(uniqueValidator));
var id = new mongoose.Types.ObjectId('aporfghtyuqi');

// Save the first user
var promise = new Planet({ _id: 'mercury' }).save();
var promise = new Planet({ _id: id }).save();
promise.then(function() {
// Try saving a duplicate
new Planet({ _id: 'mercury' }).save().catch(function(err) {
new Planet({ _id: id }).save().catch(function(err) {
expect(err.errors._id.name).to.equal('ValidatorError');
expect(err.errors._id.kind).to.equal('unique');
expect(err.errors._id.path).to.equal('_id');
expect(err.errors._id.value).to.equal('mercury');

done();
});
Expand All @@ -255,7 +255,7 @@ module.exports = function(mongoose) {
it('does not throw error when saving self (with custom _id field)', function(done) {
var Planet = mongoose.model('Planet', helpers.createCustomIdSchema().plugin(uniqueValidator));

var planet = new Planet({ _id: 'mercury' });
var planet = new Planet({ _id: new mongoose.Types.ObjectId('aporfghtyuqi') });

// Save a user
var promise = planet.save();
Expand Down
88 changes: 28 additions & 60 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ bson@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/bson/-/bson-1.0.4.tgz#93c10d39eaa5b58415cbc4052f3e53e562b0b72c"

buffer-shims@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51"

caller-path@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f"
Expand Down Expand Up @@ -270,10 +266,6 @@ doctrine@^2.1.0:
dependencies:
esutils "^2.0.2"

[email protected]:
version "3.2.1"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.2.1.tgz#ec56233868032909207170c39448e24449dd1fc4"

[email protected], escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
Expand Down Expand Up @@ -465,10 +457,6 @@ [email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/hooks-fixed/-/hooks-fixed-2.0.2.tgz#20076daa07e77d8a6106883ce3f1722e051140b0"

iconv-lite@^0.4.17:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
Expand All @@ -488,7 +476,7 @@ inflight@^1.0.4:
once "^1.3.0"
wrappy "1"

inherits@2, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3:
inherits@2, inherits@^2.0.3, inherits@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

Expand Down Expand Up @@ -582,9 +570,9 @@ jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"

kareem@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/kareem/-/kareem-1.5.0.tgz#e3e4101d9dcfde299769daf4b4db64d895d17448"
kareem@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.0.1.tgz#f17f77e9032f64aa402b334f91fb4407fe4c042c"

levn@^0.3.0, levn@~0.3.0:
version "0.3.0"
Expand Down Expand Up @@ -647,50 +635,46 @@ mocha@^5.0.0:
mkdirp "0.5.1"
supports-color "4.4.0"

mongodb-core@2.1.18:
version "2.1.18"
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-2.1.18.tgz#4c46139bdf3a1f032ded91db49f38eec01659050"
mongodb-core@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-3.0.1.tgz#ff6dc36ee96ff596953d80a6840d6731bc92efed"
dependencies:
bson "~1.0.4"
require_optional "~1.0.0"
require_optional "^1.0.1"

mongodb@2.2.34:
version "2.2.34"
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-2.2.34.tgz#a34f59bbeb61754aec432de72c3fe21526a44c1a"
mongodb@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.0.1.tgz#278ee8006257ec22798594a6259546825d6de1b2"
dependencies:
es6-promise "3.2.1"
mongodb-core "2.1.18"
readable-stream "2.2.7"
mongodb-core "3.0.1"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.1.tgz#31ae25db45c30f1448c0f93f52769e903367c701"

mongoose@^4.13.9:
version "4.13.9"
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-4.13.9.tgz#ca4d99aed6e36e87854c2295387e7ea17966cfe3"
mongoose@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.0.1.tgz#ded04311b9f13c8004e2b0097f3a61653a394361"
dependencies:
async "2.1.4"
bson "~1.0.4"
hooks-fixed "2.0.2"
kareem "1.5.0"
kareem "2.0.1"
lodash.get "4.4.2"
mongodb "2.2.34"
mongodb "3.0.1"
mongoose-legacy-pluralize "1.0.1"
mpath "0.3.0"
mpromise "0.5.5"
mquery "2.3.3"
mquery "3.0.0-rc0"
ms "2.0.0"
muri "1.3.0"
regexp-clone "0.0.1"
sliced "1.0.1"

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/mpath/-/mpath-0.3.0.tgz#7a58f789e9b5fd3c94520634157960f26bd5ef44"

[email protected]:
version "0.5.5"
resolved "https://registry.yarnpkg.com/mpromise/-/mpromise-0.5.5.tgz#f5b24259d763acc2257b0a0c8c6d866fd51732e6"

[email protected]:
version "2.3.3"
resolved "https://registry.yarnpkg.com/mquery/-/mquery-2.3.3.tgz#221412e5d4e7290ca5582dd16ea8f190a506b518"
[email protected]:
version "3.0.0-rc0"
resolved "https://registry.yarnpkg.com/mquery/-/mquery-3.0.0-rc0.tgz#05ec656e92f079828bedf4202e60fb8eaacb9f47"
dependencies:
bluebird "3.5.0"
debug "2.6.9"
Expand All @@ -701,10 +685,6 @@ [email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

[email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/muri/-/muri-1.3.0.tgz#aeccf3db64c56aa7c5b34e00f95b7878527a4721"

[email protected]:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down Expand Up @@ -790,18 +770,6 @@ pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

[email protected]:
version "2.2.7"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.7.tgz#07057acbe2467b22042d36f98c5ad507054e95b1"
dependencies:
buffer-shims "~1.0.0"
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
string_decoder "~1.0.0"
util-deprecate "~1.0.1"

readable-stream@^2.2.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
Expand All @@ -825,7 +793,7 @@ require-uncached@^1.0.3:
caller-path "^0.1.0"
resolve-from "^1.0.0"

require_optional@~1.0.0:
require_optional@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/require_optional/-/require_optional-1.0.1.tgz#4cf35a4247f64ca3df8c2ef208cc494b1ca8fc2e"
dependencies:
Expand Down Expand Up @@ -914,7 +882,7 @@ string-width@^2.0.0, string-width@^2.1.0:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string_decoder@~1.0.0, string_decoder@~1.0.3:
string_decoder@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"
dependencies:
Expand Down

0 comments on commit d27cd93

Please sign in to comment.