-
Notifications
You must be signed in to change notification settings - Fork 17
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
Bc-5596-poc-update-feathersjs-mongoose #4550
Changes from 3 commits
90ed12b
048d1e1
0ef66e3
0ceff89
4277a2e
d5e620d
8da2018
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,11 +88,8 @@ function connect() { | |
|
||
const mongooseOptions = { | ||
autoIndex: NODE_ENV !== ENVIRONMENTS.PRODUCTION, | ||
poolSize: MONGOOSE_CONNECTION_POOL_SIZE, | ||
minPoolSize: MONGOOSE_CONNECTION_POOL_SIZE, //https://mongoosejs.com/docs/migrating_to_6.html#mongodb-driver-40 ?? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I guess we should add a maxPoolSize too. Default seems to be 100
so, we had a (max) 50 and now is min 50 to 100. Will that not cause any issues? |
||
useNewUrlParser: true, | ||
useFindAndModify: false, | ||
useCreateIndex: true, | ||
useUnifiedTopology: true, | ||
}; | ||
|
||
addAuthenticationToMongooseOptions(options.username, options.password, mongooseOptions); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const errors = require('@feathersjs/errors'); | ||
|
||
const ERROR = Symbol('feathers-mongoose/error'); | ||
|
||
const wrap = (error, original) => Object.assign(error, { [ERROR]: original }); | ||
|
||
exports.ERROR = ERROR; | ||
|
||
exports.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*\}/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(wrap(new errors.Conflict(error), error)); | ||
} | ||
|
||
if (error.name) { | ||
switch (error.name) { | ||
case 'ValidationError': | ||
case 'ValidatorError': | ||
case 'CastError': | ||
case 'VersionError': | ||
return Promise.reject(wrap(new errors.BadRequest(error), error)); | ||
case 'OverwriteModelError': | ||
return Promise.reject(wrap(new errors.Conflict(error), error)); | ||
case 'MissingSchemaError': | ||
case 'DivergentArrayError': | ||
return Promise.reject(wrap(new errors.GeneralError(error), error)); | ||
case 'MongoError': | ||
return Promise.reject(wrap(new errors.GeneralError(error), error)); | ||
default: | ||
return Promise.reject(wrap(new errors.GeneralError(error), error)); | ||
} | ||
} | ||
|
||
return Promise.reject(error); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
exports.toObject = | ||
(options = {}, dataField = 'data') => | ||
(hook) => { | ||
// Only perform this if it's used as an after hook. | ||
if (hook.result) { | ||
const data = hook.result[dataField] || hook.result; | ||
let res; | ||
|
||
// Handle multiple mongoose models | ||
if (Array.isArray(data)) { | ||
res = data.map((obj) => { | ||
if (typeof obj.toObject === 'function') { | ||
return obj.toObject(options); | ||
} | ||
|
||
return obj; | ||
}); | ||
} else if (typeof data.toObject === 'function') { | ||
// Handle single mongoose models | ||
res = data.toObject(options); | ||
} | ||
// If our data is transformed set it to appropriate location on the hook | ||
if (res) { | ||
if (hook.result[dataField]) { | ||
hook.result[dataField] = res; | ||
} else { | ||
hook.result = res; | ||
} | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const hooks = require('./hooks'); | ||
const service = require('./service'); | ||
|
||
Object.assign(service, { hooks, service }); | ||
|
||
module.exports = service; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formating affected, should revert this part...