You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -11,30 +11,31 @@ Mongoose Patch History is a mongoose plugin that saves a history of [JSON Patch]
11
11
$ npm install mongoose-patch-history
12
12
13
13
## 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:
__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.
30
31
31
32
### Storing a new document
32
33
33
34
Continuing the previous example, a new patch is added to the associated patch collection whenever a new post is added to the posts collection:
__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:
55
56
56
57
```javascript
57
58
constdata= {
58
59
title:'JSON patches with mongoose',
59
-
comments: [{ message:'Wow! Such Mongoose! Very NoSQL!' }]
60
+
comments: [{ message:'Wow! Such Mongoose! Very NoSQL!' }],
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.
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
112
115
113
116
## Options
117
+
114
118
```javascript
115
119
PostSchema.plugin(patchHistory, {
116
120
mongoose,
117
-
name:'postPatches'
121
+
name:'postPatches',
118
122
})
119
123
```
120
124
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
125
129
*`removePatches` <br/>
126
-
Removes patches when origin document is removed. Default: `true`
130
+
Removes patches when origin document is removed. Default: `true`
127
131
*`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
129
133
*`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: `{}`
131
135
*`trackOriginalValue` <br/>
132
136
If enabled, the original value will be stored in the change patches under the attribute `originalValue`. Default: `false`
133
137
134
138
### Includes
139
+
135
140
```javascript
136
141
PostSchema.plugin(patchHistory, {
137
142
mongoose,
138
143
name:'postPatches',
139
144
includes: {
140
-
title: { type:String, required:true }
141
-
}
145
+
title: { type:String, required:true },
146
+
},
142
147
})
143
148
```
149
+
144
150
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.
145
151
146
152
```javascript
@@ -154,11 +160,12 @@ Post.create({ title: 'Included in every patch' })
154
160
The value of the patch documents properties is read from the versioned documents property of the same name.
155
161
156
162
#### 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**:
0 commit comments