Skip to content

Commit

Permalink
Merge pull request #95 from shikshalokam/master
Browse files Browse the repository at this point in the history
compound index added
  • Loading branch information
aks30 authored Jun 15, 2022
2 parents 0dd56af + 15e8bc1 commit 668ca2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion config/db/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,21 @@ const 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
};
};

Expand Down
4 changes: 4 additions & 0 deletions generics/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ let Abstract = class Abstract {

constructor(schema) {
database.createModel(schemas[schema]);
if ( schemas[schema].compoundIndex && schemas[schema].compoundIndex.length > 0 ) {
database.runCompoundIndex(schemas[schema].name,schemas[schema].compoundIndex);
}

}
};

Expand Down
12 changes: 11 additions & 1 deletion models/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,15 @@ module.exports = {
},
remarks : String,
userProfile : Object
}
},
compoundIndex: [
{
"name" :{ userId: 1, solutionId: 1 },
"indexType" : { unique: true, partialFilterExpression: { solutionId: { $exists: true }}}
}
]



};

0 comments on commit 668ca2b

Please sign in to comment.