diff --git a/config/dbConfig.js b/config/dbConfig.js index bd5120b8..c0425820 100644 --- a/config/dbConfig.js +++ b/config/dbConfig.js @@ -58,11 +58,21 @@ var DB = function() { return model; }; + const runCompoundIndex = function(modelName,opts) { + if (opts && opts.length > 0) { + for ( let indexPointer = 0 ; indexPointer < opts.length ; indexPointer++ ) { + let currentIndex = opts[indexPointer]; + db.collection(modelName).createIndex(currentIndex.name, currentIndex.indexType); + } + } + } + return { database: db, createModel: createModel, ObjectId: ObjectId, - models: db.models + models: db.models, + runCompoundIndex: runCompoundIndex }; }; diff --git a/generics/abstract.js b/generics/abstract.js index 5193885b..ec482808 100644 --- a/generics/abstract.js +++ b/generics/abstract.js @@ -2,6 +2,9 @@ let Abstract = class Abstract { constructor(schema) { this.model = database.createModel(schema); this.schema = schema.name; + if ( schema.compoundIndex && schema.compoundIndex.length > 0 ) { + database.runCompoundIndex(schema.name,schema.compoundIndex); + } this.httpStatus = { ok: 200, diff --git a/models/observationSubmissions.js b/models/observationSubmissions.js index fe851647..8f2bcf0a 100644 --- a/models/observationSubmissions.js +++ b/models/observationSubmissions.js @@ -90,5 +90,11 @@ module.exports = { userRoleInformation : Object, criteriaLevelReport : Boolean, userProfile : Object - } + }, + compoundIndex: [ + { + "name" :{ observationId: 1, entityId: 1, submissionNumber:1 }, + "indexType" : { unique: true } + } + ] }; diff --git a/models/observations.js b/models/observations.js index 45cb332c..c4574842 100644 --- a/models/observations.js +++ b/models/observations.js @@ -65,5 +65,11 @@ module.exports = { type : Boolean, index: true } - } + }, + compoundIndex: [ + { + "name" :{ createdBy: 1, solutionId: 1 }, + "indexType" : { unique: true } + } + ] }; \ No newline at end of file diff --git a/models/surveySubmissions.js b/models/surveySubmissions.js index daa5de52..e8d7f2ee 100644 --- a/models/surveySubmissions.js +++ b/models/surveySubmissions.js @@ -4,7 +4,8 @@ module.exports = { surveyId: { type: "ObjectId", index: true, - required: true + required: true, + unique: true }, createdBy: { type: String, diff --git a/models/surveys.js b/models/surveys.js index 2453686e..79c07f60 100644 --- a/models/surveys.js +++ b/models/surveys.js @@ -43,5 +43,11 @@ module.exports = { default : false, type : Boolean } - } + }, + compoundIndex: [ + { + "name" :{ createdBy: 1, solutionId: 1 }, + "indexType" : { unique: true } + } + ] }; \ No newline at end of file