mongoose-crud-helper
A simple helper library for Mongoose projects.
Installation
npm install mongoose-crud-helper
Configuration
Sample usage scenarios are explained below.
Changes to Model.js
You can directly code this to your model.
// Require the plugin in the top.const MCHelper = ; // Add below after schema definition as required.SchemaName;SchemaName;SchemaName;
Usage
You can directly code this to your controller.
1.changeDocStatus
const data = _id:<Your-Object-ID> // ObjectId status: '<New-Status>' // String (active, deleted, pending); // Object Model;
2.getAllDocs
More info on customLabels referenced below is available at mongoose-paginate-v2
const where = "$in": status: 'active''pending'; // Objectconst fieldsToDisplay = postName: 1 description: 1 createdOn: 1 ; //Objectconst myCustomLabels = docs: 'data' nextPage: 'next' prevPage: 'prev' totalPages: 'pageCount'; const options = select: fieldsToDisplay // Object page: 1 // Number limit: 10 // Number lean: false // Bool sortBy: 'createdOn' // String sortOrder: 'desc' // String populate: '' // String customLabels: myCustomLabels // Object; // Object Model;
3.getOneDoc
const where = "$in": status: 'active''pending'; // Objectconst fieldsToDisplay = postName: 1 description: 1 createdOn: 1 ; //Object Model;
4.hardDelete
const where = "_id": ; // Object Model;
5.softDelete
const where = "_id": ; // Object Model;