Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aravindnc/mongoose-crud-helper
An easy plugin for mongoose models to minimize your code.
https://github.com/aravindnc/mongoose-crud-helper
crud helper mongoose plugin
Last synced: 7 days ago
JSON representation
An easy plugin for mongoose models to minimize your code.
- Host: GitHub
- URL: https://github.com/aravindnc/mongoose-crud-helper
- Owner: aravindnc
- License: mit
- Created: 2018-07-09T10:58:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-17T17:19:47.000Z (over 6 years ago)
- Last Synced: 2024-10-06T08:42:07.651Z (3 months ago)
- Topics: crud, helper, mongoose, plugin
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [mongoose-crud-helper](https://github.com/aravindnc/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.
```javascript
// Require the plugin in the top.
const MCHelper = require('mongoose-crud-helper');// Add below after schema definition as required.
SchemaName.plugin(MCHelper.changeDocStatus);
SchemaName.plugin(MCHelper.getAllDocs);
SchemaName.plugin(MCHelper.getOneDoc);
```
## Usage
You can directly code this to your controller.
### 1.changeDocStatus
```javascript
const data = {
_id:, // ObjectId
status: '' // String (active, deleted, pending)
}; // ObjectModel.changeDocStatus(data).then(function(response){
// Your code here
});
```
### 2.getAllDocsMore info on customLabels referenced below is available at [mongoose-paginate-v2](https://www.npmjs.com/package/mongoose-paginate-v2)
```javascript
const where = {"$in": {status: ['active','pending']}}; // Object
const fieldsToDisplay = {postName: 1, description: 1, createdOn: 1 }; //Object
const 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
}; // ObjectModel.getAllDocs(where, options).then(function(response){
// Your code here
});
```
### 3.getOneDoc
```javascript
const where = {"$in": {status: ['active','pending']}}; // Object
const fieldsToDisplay = {postName: 1, description: 1, createdOn: 1 }; //ObjectModel.getOneDoc(where, fieldsToDisplay).then(function(response){
// Your code here
});
```
### 4.hardDelete
```javascript
const where = {"_id": ObjectId('57f79499cd3aa1000a5643b7')}; // ObjectModel.hardDelete(where).then(function(response){
// Your code here
});
```
### 5.softDelete
```javascript
const where = {"_id": ObjectId('57f79499cd3aa1000a5643b7')}; // ObjectModel.softDelete(where).then(function(response){
// Your code here
});
```