Skip to content

Commit e50de50

Browse files
committed
module logo
1 parent 8a49968 commit e50de50

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

README.md

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Mongoose Patch History
1+
<img title="redux-active" src="docs/mongoose-patch-history.png" width="500" style="margin-top:20px;">
22

33
[![Greenkeeper badge](https://badges.greenkeeper.io/codepunkt/mongoose-patch-history.svg)](https://greenkeeper.io/)
44

@@ -11,30 +11,31 @@ Mongoose Patch History is a mongoose plugin that saves a history of [JSON Patch]
1111
$ npm install mongoose-patch-history
1212

1313
## Usage
14-
To use __mongoose-patch-history__ for an existing mongoose schema you can simply plug it in. As an example, the following schema definition defines a `Post` schema, and uses mongoose-patch-history with default options:
14+
15+
To use **mongoose-patch-history** for an existing mongoose schema you can simply plug it in. As an example, the following schema definition defines a `Post` schema, and uses mongoose-patch-history with default options:
1516

1617
```javascript
1718
import mongoose, { Schema } from 'mongoose'
1819
import patchHistory from 'mongoose-patch-history'
1920

2021
const PostSchema = new Schema({
2122
title: { type: String, required: true },
22-
comments: Array
23+
comments: Array,
2324
})
2425

2526
PostSchema.plugin(patchHistory, { mongoose, name: 'postPatches' })
2627
const Post = mongoose.model('Post', PostSchema)
2728
```
2829

29-
__mongoose-patch-history__ will define a schema that has a `ref` field containing the `ObjectId` of the original document, a `ops` array containing all json patch operations and a `date` field storing the date where the patch was applied.
30+
**mongoose-patch-history** will define a schema that has a `ref` field containing the `ObjectId` of the original document, a `ops` array containing all json patch operations and a `date` field storing the date where the patch was applied.
3031

3132
### Storing a new document
3233

3334
Continuing the previous example, a new patch is added to the associated patch collection whenever a new post is added to the posts collection:
3435

3536
```javascript
3637
Post.create({ title: 'JSON patches' })
37-
.then((post) => post.patches.findOne({ ref: post.id }))
38+
.then(post => post.patches.findOne({ ref: post.id }))
3839
.then(console.log)
3940

4041
// {
@@ -51,17 +52,17 @@ Post.create({ title: 'JSON patches' })
5152

5253
### Updating an existing document
5354

54-
__mongoose-patch-history__ also adds a static field `Patches` to the model that can be used to access the patch model associated with the model, for example to query all patches of a document. Whenever a post is edited, a new patch that reflects the update operation is added to the associated patch collection:
55+
**mongoose-patch-history** also adds a static field `Patches` to the model that can be used to access the patch model associated with the model, for example to query all patches of a document. Whenever a post is edited, a new patch that reflects the update operation is added to the associated patch collection:
5556

5657
```javascript
5758
const data = {
5859
title: 'JSON patches with mongoose',
59-
comments: [{ message: 'Wow! Such Mongoose! Very NoSQL!' }]
60+
comments: [{ message: 'Wow! Such Mongoose! Very NoSQL!' }],
6061
}
6162

6263
Post.create({ title: 'JSON patches' })
63-
.then((post) => post.set(data).save())
64-
.then((post) => post.patches.find({ ref: post.id }))
64+
.then(post => post.set(data).save())
65+
.then(post => post.patches.find({ ref: post.id }))
6566
.then(console.log)
6667

6768
// [{
@@ -87,15 +88,16 @@ Post.create({ title: 'JSON patches' })
8788

8889
### Rollback to a specific patch
8990

90-
Documents have a `rollback` method that accepts the *ObjectId* of a patch doc and sets the document to the state of that patch, adding a new patch to the history.
91+
Documents have a `rollback` method that accepts the _ObjectId_ of a patch doc and sets the document to the state of that patch, adding a new patch to the history.
9192

9293
```javascript
9394
Post.create({ title: 'First version' })
94-
.then((post) => post.set({ title: 'Second version' }).save())
95-
.then((post) => post.set({ title: 'Third version' }).save())
96-
.then((post) => {
97-
return post.patches.find({ ref: post.id })
98-
.then((patches) => post.rollback(patches[1].id))
95+
.then(post => post.set({ title: 'Second version' }).save())
96+
.then(post => post.set({ title: 'Third version' }).save())
97+
.then(post => {
98+
return post.patches
99+
.find({ ref: post.id })
100+
.then(patches => post.rollback(patches[1].id))
99101
})
100102
.then(console.log)
101103

@@ -107,40 +109,44 @@ Post.create({ title: 'First version' })
107109
```
108110

109111
The `rollback` method will throw an Error when invoked with an ObjectId that is
110-
- not a patch of the document
111-
- the latest patch of the document
112+
113+
* not a patch of the document
114+
* the latest patch of the document
112115

113116
## Options
117+
114118
```javascript
115119
PostSchema.plugin(patchHistory, {
116120
mongoose,
117-
name: 'postPatches'
121+
name: 'postPatches',
118122
})
119123
```
120124

121-
* `mongoose` :pushpin: *required* <br/>
122-
The mongoose instance to work with
123-
* `name` :pushpin: *required* <br/>
124-
String where the names of both patch model and patch collection are generated from. By default, model name is the pascalized version and collection name is an undercore separated version
125+
* `mongoose` :pushpin: _required_ <br/>
126+
The mongoose instance to work with
127+
* `name` :pushpin: _required_ <br/>
128+
String where the names of both patch model and patch collection are generated from. By default, model name is the pascalized version and collection name is an undercore separated version
125129
* `removePatches` <br/>
126-
Removes patches when origin document is removed. Default: `true`
130+
Removes patches when origin document is removed. Default: `true`
127131
* `transforms` <br/>
128-
An array of two functions that generate model and collection name based on the `name` option. Default: An array of [humps](https://github.com/domchristie/humps).pascalize and [humps](https://github.com/domchristie/humps).decamelize
132+
An array of two functions that generate model and collection name based on the `name` option. Default: An array of [humps](https://github.com/domchristie/humps).pascalize and [humps](https://github.com/domchristie/humps).decamelize
129133
* `includes` <br/>
130-
Property definitions that will be included in the patch schema. Read more about includes in the next chapter of the documentation. Default: `{}`
134+
Property definitions that will be included in the patch schema. Read more about includes in the next chapter of the documentation. Default: `{}`
131135
* `trackOriginalValue` <br/>
132136
If enabled, the original value will be stored in the change patches under the attribute `originalValue`. Default: `false`
133137

134138
### Includes
139+
135140
```javascript
136141
PostSchema.plugin(patchHistory, {
137142
mongoose,
138143
name: 'postPatches',
139144
includes: {
140-
title: { type: String, required: true }
141-
}
145+
title: { type: String, required: true },
146+
},
142147
})
143148
```
149+
144150
This will add a `title` property to the patch schema. All options that are available in mongoose's schema property definitions such as `required`, `default` or `index` can be used.
145151

146152
```javascript
@@ -154,11 +160,12 @@ Post.create({ title: 'Included in every patch' })
154160
The value of the patch documents properties is read from the versioned documents property of the same name.
155161
156162
#### Reading from virtuals
157-
There is an additional option that allows storing information in the patch documents that is not stored in the versioned documents. To do so, you can use a combination of [virtual type setters](http://mongoosejs.com/docs/guide.html#virtuals) on the versioned document and an additional `from` property in the include options of __mongoose-patch-history__:
163+
164+
There is an additional option that allows storing information in the patch documents that is not stored in the versioned documents. To do so, you can use a combination of [virtual type setters](http://mongoosejs.com/docs/guide.html#virtuals) on the versioned document and an additional `from` property in the include options of **mongoose-patch-history**:
158165
159166
```javascript
160167
// save user as _user in versioned documents
161-
PostSchema.virtual('user').set(function (user) {
168+
PostSchema.virtual('user').set(function(user) {
162169
this._user = user
163170
})
164171

@@ -167,20 +174,20 @@ PostSchema.plugin(patchHistory, {
167174
mongoose,
168175
name: 'postPatches',
169176
includes: {
170-
user: { type: Schema.Types.ObjectId, required: true, from: '_user' }
171-
}
177+
user: { type: Schema.Types.ObjectId, required: true, from: '_user' },
178+
},
172179
})
173180

174181
// create post, pass in user information
175182
Post.create({
176183
title: 'Why is hiring broken?',
177-
user: mongoose.Types.ObjectId()
184+
user: mongoose.Types.ObjectId(),
178185
})
179-
.then((post) => {
186+
.then(post => {
180187
console.log(post.user) // undefined
181188
return post.patches.findOne({ ref: post.id })
182189
})
183-
.then((patch) => {
190+
.then(patch => {
184191
console.log(patch.user) // 4edd40c86762e0fb12000012
185192
})
186193
```
@@ -189,19 +196,25 @@ In case of a rollback in this scenario, the `rollback` method accepts an object
189196
190197
```javascript
191198
Post.create({ title: 'v1', user: mongoose.Types.ObjectId() })
192-
.then((post) => post.set({
193-
title: 'v2',
194-
user: mongoose.Types.ObjectId()
195-
}).save())
196-
.then((post) => {
197-
return post.patches.find({ ref: post.id })
198-
.then((patches) => post.rollback(patches[0].id, {
199-
user: mongoose.Types.ObjectId()
200-
}))
199+
.then(post =>
200+
post
201+
.set({
202+
title: 'v2',
203+
user: mongoose.Types.ObjectId(),
204+
})
205+
.save()
206+
)
207+
.then(post => {
208+
return post.patches.find({ ref: post.id }).then(patches =>
209+
post.rollback(patches[0].id, {
210+
user: mongoose.Types.ObjectId(),
211+
})
212+
)
201213
})
202214
```
203215
204216
#### Reading from query options
217+
205218
In situations where you are running Mongoose queries directly instead of via a document, you can specify the extra fields in the query options:
206219
207220
```javascript

docs/mongoose-patch-history.png

18.4 KB
Loading

0 commit comments

Comments
 (0)