Skip to content

Commit

Permalink
Merge pull request hagopj13#3 from KrastanD/patch-1
Browse files Browse the repository at this point in the history
Remove mongoose plugins from README
  • Loading branch information
antonio-lazaro authored Dec 3, 2023
2 parents 9e56a6a + 2810c27 commit 5d541b7
Showing 1 changed file with 0 additions and 67 deletions.
67 changes: 0 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,73 +340,6 @@ This app uses pm2 in production mode, which is already configured to store the l

Note: API request information (request url, response code, timestamp, etc.) are also automatically logged (using [morgan](https://github.com/expressjs/morgan)).

## Custom Mongoose Plugins

The app also contains 2 custom mongoose plugins that you can attach to any mongoose model schema. You can find the plugins in `src/models/plugins`.

```javascript
const mongoose = require('mongoose');
const { toJSON, paginate } = require('./plugins');

const userSchema = mongoose.Schema(
{
/* schema definition here */
},
{ timestamps: true }
);

userSchema.plugin(toJSON);
userSchema.plugin(paginate);

const User = mongoose.model('User', userSchema);
```

### toJSON

The toJSON plugin applies the following changes in the toJSON transform call:

- removes \_\_v, createdAt, updatedAt, and any schema path that has private: true
- replaces \_id with id

### paginate

The paginate plugin adds the `paginate` static method to the mongoose schema.

Adding this plugin to the `User` model schema will allow you to do the following:

```javascript
const queryUsers = async (filter, options) => {
const users = await User.paginate(filter, options);
return users;
};
```

The `filter` param is a regular mongo filter.

The `options` param can have the following (optional) fields:

```javascript
const options = {
sortBy: 'name:desc', // sort order
limit: 5, // maximum results per page
page: 2 // page number
};
```

The plugin also supports sorting by multiple criteria (separated by a comma): `sortBy: name:desc,role:asc`

The `paginate` method returns a Promise, which fulfills with an object having the following properties:

```json
{
"results": [],
"page": 2,
"limit": 5,
"totalPages": 10,
"totalResults": 48
}
```

## Linting

Linting is done using [ESLint](https://eslint.org/) and [Prettier](https://prettier.io).
Expand Down

0 comments on commit 5d541b7

Please sign in to comment.