|
1 |
| -# Mongoose difference plugin |
| 1 | +# mongoose-history-diff :rocket: :fire: |
| 2 | + |
| 3 | + |
| 4 | +This is a [mongoose](https://mongoosejs.com/) plugin for tracking history and differences of your docs. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```bash |
| 9 | +yarn add mongoose-history-diff |
| 10 | +``` |
| 11 | + |
| 12 | +```bash |
| 13 | +npm i mongoose-history-diff |
| 14 | +``` |
| 15 | + |
| 16 | + ## Usage |
| 17 | + |
| 18 | + |
| 19 | + ### Add plugin to your schema: |
| 20 | + ```js |
| 21 | +import DiffPlugin from 'mongoose-history-diff'; |
| 22 | + ``` |
| 23 | + ```js |
| 24 | + PostSchema.plugin(DiffPlugin, { |
| 25 | + orderIndependent: true, |
| 26 | + diffCollectionName: 'my_diffs', |
| 27 | + }); |
| 28 | + ``` |
| 29 | + `orderIndependent` option define whether the order of array elements is important or not. If `true` then it won't create a new diff. By default **false**. |
| 30 | + |
| 31 | + `diffCollectionName` option define the name of collection with diffs. If not provided `${parent_collection_name}_diffs` will be used. |
| 32 | + |
| 33 | + ### Exclude fields |
| 34 | + |
| 35 | + You can exlude document fields from tracking by adding `{ track_diff: false }` property to your field definition inside schema: |
| 36 | + |
| 37 | + ```js |
| 38 | + export const PostSchema: MongooseSchema<PostDoc> = new mongoose.Schema( |
| 39 | + { |
| 40 | + title: { |
| 41 | + type: String, |
| 42 | + }, |
| 43 | + text: { |
| 44 | + type: String, |
| 45 | + track_diff: false, |
| 46 | + }, |
| 47 | + authors: [ |
| 48 | + { |
| 49 | + name: { type: String } |
| 50 | + lname: { type: String }, track_diff: false }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + { |
| 54 | + timestamps: true, |
| 55 | + collection: 'post', |
| 56 | + } |
| 57 | +); |
| 58 | + ``` |
| 59 | + |
| 60 | + The **_id** field is excluded from the tracking by default. |
| 61 | + |
| 62 | + |
| 63 | +## Track diffs |
| 64 | + |
| 65 | +After adding, plugin will create a diff document with a following shape in separate collection on every changing of your documents. |
| 66 | + |
| 67 | +```js |
| 68 | +{ |
| 69 | + _id: 5c33240bd7cce8cba92030aa, |
| 70 | + dId: 5c25abc9c9a367742cd5341b, |
| 71 | + c : [ |
| 72 | + { |
| 73 | + p : [ |
| 74 | + "lastname" |
| 75 | + ], |
| 76 | + k : "E", |
| 77 | + l : "borodaev", |
| 78 | + r : "Borodayev" |
| 79 | + } |
| 80 | + ], |
| 81 | + v: 4, |
| 82 | + createdAt: 2019-01-07T10:03:55.933Z, |
| 83 | + updatedAt: 2019-01-07T10:03:55.933Z, |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +**Important!** Plugin creates diffs on a `preSave` mongoose hook. That's why all methods which directly operates with MongoDB won't call creating diff. |
| 88 | + |
| 89 | +Diffs are represented as one or more change records. Change records have the following structure: |
| 90 | + |
| 91 | +* `k` - indicates the kind of change; will be one of the following: |
| 92 | + * `N` - indicates a newly added property/element |
| 93 | + * `D` - indicates a property/element was deleted |
| 94 | + * `E` - indicates a property/element was edited |
| 95 | + * `A` - indicates a change occurred within an array |
| 96 | +* `p` - the property path |
| 97 | +* `l` - the value that was (undefined if `k === 'N'`) |
| 98 | +* `r` - the value that become (undefined if `k === 'D'`) |
| 99 | +* `i` - when `k === 'A'`, indicates the array index where the change occurred |
| 100 | +* `it` - when `k === 'A'`, contains a nested change record indicating the change that occurred at the array index |
| 101 | + |
| 102 | +Under the hood plugin uses refactored and simplified algorithm of `deep-diff` package, that is why this plugin has similar structure. You can explore that [repo](https://github.com/flitbit/diff) too if you are interested in. |
| 103 | + |
| 104 | + |
| 105 | +## Methods |
| 106 | + |
| 107 | +Also, plugin will add static `diffModel` method that return the model of diff collection. |
| 108 | + |
| 109 | +```js |
| 110 | +const Diff = Post.diffModel(); |
| 111 | +``` |
| 112 | + |
| 113 | +This model contains several static methods as well: |
| 114 | +* `findByDocId(_id: ObjectId)` - finds all diffs docs by parent doc `_id` |
| 115 | +* `findAfterVersion(_id: ObjectId, v: number)` - finds all diffs docs by parent doc `_id` after specific version |
| 116 | +* `findBeforeVersion(_id: ObjectId, v: number)` - finds all diffs docs by parent doc `_id` before specific version |
| 117 | +* `revertToVersion(doc: Object, v: number)` - reverts changes of specific doc to a specific version. |
| 118 | +* `mergeDiffs(doc: MongooseDocument)` - return all diffs between current doc state and initial doc state. |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +## Contribution |
| 130 | + |
| 131 | +Feel free to submit pull request. Also, be sure all tests has passed otherwise pull request won't be merged. |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +[MIT](https://github.com/FrankAst/sms-sender/blob/master/LICENSE.md) |
0 commit comments