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

Bc-5596-poc-update-feathersjs-mongoose #4550

Closed
Show file tree
Hide file tree
Changes from 3 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
881 changes: 439 additions & 442 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@
},
"dependencies": {
"@aws-sdk/lib-storage": "^3.100.0",
"@feathersjs/adapter-commons": "^5.0.11",
"@feathersjs/authentication": "^5.0.11",
"@feathersjs/authentication-local": "^5.0.11",
"@feathersjs/commons": "^5.0.11",
"@feathersjs/configuration": "^5.0.11",
"@feathersjs/errors": "^5.0.11",
"@feathersjs/express": "^5.0.11",
Expand Down Expand Up @@ -158,7 +160,6 @@
"express-openapi-validator": "^4.13.2",
"express-session": "^1.17.3",
"feathers-hooks-common": "^8.1.1",
"feathers-mongoose": "^6.3.0",
"feathers-swagger": "^3.0.0",
"file-type": "^18.5.0",
"freeport": "^1.0.5",
Expand All @@ -173,7 +174,7 @@
"mixwith": "^0.1.1",
"moment": "^2.19.2",
"mongodb-uri": "^0.9.7",
"mongoose": "^5.13.20",
"mongoose": "^6.5.4",
"mongoose-delete": "^0.5.4",
"mongoose-id-validator": "^0.6.0",
"mongoose-lean-virtuals": "^0.8.1",
Expand Down
2 changes: 1 addition & 1 deletion src/services/activation/services/ActivationModelService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const feathersMongooseService = require('feathers-mongoose');
const feathersMongooseService = require('../../../utils/feathers-mongoose');
const auth = require('@feathersjs/authentication');
const { iff, isProvider, disallow } = require('feathers-hooks-common');
const { activationModel } = require('../model');
Expand Down
2 changes: 1 addition & 1 deletion src/services/base64Files/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/consent/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/content/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line max-classes-per-file
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');
const material = require('./material-model');
Expand Down
2 changes: 1 addition & 1 deletion src/services/datasources/services/datasources.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Ajv = require('ajv');
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
const { authenticate } = require('@feathersjs/authentication');
const { iff, isProvider, validateSchema, disallow } = require('feathers-hooks-common');
const { datasourceModel } = require('../model');
Expand Down
2 changes: 1 addition & 1 deletion src/services/federalState/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/fileStorage/model-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { FileModel } = require('./model');
const hooks = require('./hooks/model-hooks');

Expand Down
8 changes: 6 additions & 2 deletions src/services/helpdesk/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand All @@ -9,7 +9,11 @@ const { BODYPARSER_JSON_LIMIT, MAXIMUM_ALLOWABLE_TOTAL_ATTACHMENTS_SIZE_BYTE } =

if (BODYPARSER_JSON_LIMIT === undefined) {
/* eslint-disable-next-line */
logger.warning(`please set the environment variable BODYPARSER_JSON_LIMIT to min. '${Math.ceil(1.36*(MAXIMUM_ALLOWABLE_TOTAL_ATTACHMENTS_SIZE_BYTE/1024/1024))}mb' for helpdesk to work correctly! (Currently: ${BODYPARSER_JSON_LIMIT})`);
logger.warning(
`please set the environment variable BODYPARSER_JSON_LIMIT to min. '${Math.ceil(
1.36 * (MAXIMUM_ALLOWABLE_TOTAL_ATTACHMENTS_SIZE_BYTE / 1024 / 1024)
)}mb' for helpdesk to work correctly! (Currently: ${BODYPARSER_JSON_LIMIT})`
);
}
Copy link
Contributor

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...


module.exports = function () {
Expand Down
2 changes: 1 addition & 1 deletion src/services/homework/model-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { homeworkModel, submissionModel } = require('./model');
const hooks = require('./hooks');
const submissionHooks = require('./hooks/submissions');
Expand Down
2 changes: 1 addition & 1 deletion src/services/lesson/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/link/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-classes-per-file */
const { Configuration } = require('@hpi-schul-cloud/commons');
const queryString = require('qs');
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');
const { BadRequest } = require('@feathersjs/errors');
Expand Down
2 changes: 1 addition & 1 deletion src/services/ltiTool/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { authenticate } = require('@feathersjs/authentication');
const fs = require('fs');
const jwt = require('jsonwebtoken');
Expand Down
2 changes: 1 addition & 1 deletion src/services/news/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* The public API has been moved to apps/server/src/modules/news
*/

const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');

const { newsModel } = require('./model');
const newsModelHooks = require('./hooks/newsModel.hooks');
Expand Down
2 changes: 1 addition & 1 deletion src/services/pseudonym/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/releases/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const request = require('request-promise-native');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');
Expand Down
2 changes: 1 addition & 1 deletion src/services/role/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/school/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { Configuration } = require('@hpi-schul-cloud/commons');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');
Expand Down
2 changes: 1 addition & 1 deletion src/services/serviceTemplate/services/modelService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
const Ajv = require('ajv');
const auth = require('@feathersjs/authentication');
const { iff, isProvider, validateSchema, disallow } = require('feathers-hooks-common');
Expand Down
2 changes: 1 addition & 1 deletion src/services/storageProvider/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/system/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/teams/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line max-classes-per-file
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { Configuration } = require('@hpi-schul-cloud/commons');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');
Expand Down
2 changes: 2 additions & 0 deletions src/services/teams/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const teamsSchema = getUserGroupSchema({
color: { type: String, default: '#ACACAC' },
features: [{ type: String, enum: Object.values(TEAM_FEATURES) }],
filePermission: [permissionSchema],
// https://mongoosejs.com/docs/migrating_to_6.html#strictpopulate
ltiToolIds: [{ type: Schema.Types.ObjectId, required: true, ref: 'ltiTool' }],
});

/*
Expand Down
2 changes: 1 addition & 1 deletion src/services/user-group/hooks/courses.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require('lodash');
const { Configuration } = require('@hpi-schul-cloud/commons/lib');
const { service } = require('feathers-mongoose');
const { service } = require('../../../utils/feathers-mongoose');

const { BadRequest } = require('../../../errors');
const globalHooks = require('../../../hooks');
Expand Down
2 changes: 1 addition & 1 deletion src/services/user-group/services/classModelService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
const { iff, isProvider, disallow } = require('feathers-hooks-common');
const auth = require('@feathersjs/authentication');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
const { iff, isProvider, disallow } = require('feathers-hooks-common');
const auth = require('@feathersjs/authentication');

Expand Down
2 changes: 1 addition & 1 deletion src/services/user-group/services/courseModelService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
const { iff, isProvider, disallow } = require('feathers-hooks-common');
const auth = require('@feathersjs/authentication');

Expand Down
2 changes: 1 addition & 1 deletion src/services/user/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const hooks = require('feathers-hooks-common');
const service = require('feathers-mongoose');
const service = require('../../utils/feathers-mongoose');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion src/services/user/services/UsersModelService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const feathersMongooseService = require('feathers-mongoose');
const auth = require('@feathersjs/authentication');
const { iff, isProvider, disallow } = require('feathers-hooks-common');
const feathersMongooseService = require('../../../utils/feathers-mongoose');
const { userModel } = require('../model');
const { addDates: addConsentDate } = require('../hooks/consent');
const { enableQuery, enableQueryAfter, resolveToIds } = require('../../../hooks');
Expand Down
2 changes: 1 addition & 1 deletion src/services/webuntis/services/webuntisMetadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const service = require('feathers-mongoose');
const service = require('../../../utils/feathers-mongoose');
// const Ajv = require('ajv');
const auth = require('@feathersjs/authentication');
const { iff, isProvider, /* validateSchema, */ disallow } = require('feathers-hooks-common');
Expand Down
5 changes: 1 addition & 4 deletions src/utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mongoose 5.x poolSize option is equivalent to the Mongoose 6 maxPoolSize option. The default value of maxPoolSize has been increased to 100.

I guess we should add a maxPoolSize too. Default seems to be 100
In dof app we have

MONGOOSE_CONNECTION_POOL_SIZE:
    value: "50"

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);
Expand Down
53 changes: 53 additions & 0 deletions src/utils/feathers-mongoose/error-handler.js
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);
};
31 changes: 31 additions & 0 deletions src/utils/feathers-mongoose/hooks.js
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;
}
}
}
};
6 changes: 6 additions & 0 deletions src/utils/feathers-mongoose/index.js
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;
Loading
Loading