# mongoose-previous

> Mongoose Previous Data

Latest version **1.0.2** (published 2018-12-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install mongoose-previous
pnpm add mongoose-previous
yarn add mongoose-previous
bun add mongoose-previous
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2018-12-12 |
| First published | 2018-12-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexandr Dronko |
| Maintainers | d.alexandr |
| Keywords | mongoose-previous, mongoose, mongodb, previous, nodejs, data |

## Links

- npm: https://www.npmjs.com/package/mongoose-previous
- Repository: https://github.com/rayozerets/mongoose-previous
- Homepage: https://github.com/rayozerets/mongoose-previous#readme
- Issues: https://github.com/rayozerets/mongoose-previous/issues
- npm.io page: https://npm.io/package/mongoose-previous

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^5.3.15

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2018-12-12
- 1.0.0 — 2018-12-12

## README

**Mongoose Previous Data**

is a mongoose plugin with which you can get the previous data. For example after update or after save some data you can get only changed fields or even new/deleted/edited rows array.

**Installation**

```
$ npm install mongoose-previous --save
```

**Setup**

```
const mongoosePreData = require('mongoose-previous');

schemaOrders.plugin(mongoosePreData, {populate: 'city ordersArray.order', class: 'mpd'});

```
**Option keys and defaults**

 populate: use if you need get some field from other collections

 class: name for context mongoose data. Defaul 'mpd'

**Usage**

getPreviousData: Function return previous data or some field from data
* @param {Array} fields for checked changes. Default: null - return all prev data
```
schemaOrders.post('findOneAndUpdate', function (doc, next) {
  // return only prev sum
  const prevSum = this.mpd.getPreviousData(['sum']);
  // return all prev data
  const prevData = this.mpd.getPreviousData();
});
```
getChangesForArray: Function return changes by Array { new: [some new rows], edit: [some edit rows], delete: [some del rows] }
* @param {String} name name array in mongoose
* @param {String} key unique field for find changes
* @param {Array} kind enum ['N' - new, 'D' - delete, 'E' - edit]
* @param {Array} fields
```
schemaOrders.post('findOneAndUpdate', function (doc, next) {
  // return new and delete orders
  const newAndDelOrders = this.mpd.getChangesForArray('ordersArray', 'order._id', ['N', 'D']);
});
```
isFieldChanged: Function return boolean (changed fields or not)
* @param {Array} fields for checked changes
```
schemaOrders.post('findOneAndUpdate', function (doc, next) {
  const changed = this.mpd.isFieldChanged(['sum', 'interval', 'date']);
});
```
getCurrentData: Function return current data
* @param {Array} fields for checked changes
```
schemaOrders.post('findOneAndUpdate', function (doc, next) {
  const updatedData = context.mpd.getCurrentData();
});
```

All this functions you can use for "post save" too

---
_Source: https://npm.io/package/mongoose-previous · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
