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
* Added excludes option
* Added deep removal of excluded paths in json-patch-operations
* Moved filtering inside condition to get a little performance if 'excludes' option is not used
* Extracted ambiguous functionality from getArrayFromPath()
* Reworked some property names. Extraced methods for better readability.
* Moved some comments.
* Added documentation examples
* Added link to excludes chapter
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
167
-
*`removePatches` <br/>
167
+
-`removePatches` <br/>
168
168
Removes patches when origin document is removed. Default: `true`
169
-
*`transforms` <br/>
169
+
-`transforms` <br/>
170
170
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
171
-
*`includes` <br/>
171
+
-`includes` <br/>
172
172
Property definitions that will be included in the patch schema. Read more about includes in the next chapter of the documentation. Default: `{}`
173
-
*`trackOriginalValue` <br/>
173
+
-`excludes` <br/>
174
+
Property paths that will be excluded in patches. Read more about excludes in the [excludes chapter of the documentation](https://github.com/codepunkt/mongoose-patch-history#excludes). Default: `[]`
175
+
-`trackOriginalValue` <br/>
174
176
If enabled, the original value will be stored in the change patches under the attribute `originalValue`. Default: `false`
175
177
176
178
### Includes
@@ -203,7 +205,7 @@ There is an additional option that allows storing information in the patch docum
203
205
204
206
```javascript
205
207
// save user as _user in versioned documents
206
-
PostSchema.virtual('user').set(function(user) {
208
+
PostSchema.virtual('user').set(function(user) {
207
209
this._user= user
208
210
})
209
211
@@ -262,3 +264,36 @@ Post.findOneAndUpdate(
262
264
{ _user:mongoose.Types.ObjectId() }
263
265
)
264
266
```
267
+
268
+
### Excludes
269
+
270
+
```javascript
271
+
PostSchema.plugin(patchHistory, {
272
+
mongoose,
273
+
name:'postPatches',
274
+
excludes: [
275
+
'/path/to/hidden/property',
276
+
'/path/into/array/*/property',
277
+
'/path/to/one/array/1/element',
278
+
],
279
+
})
280
+
281
+
// Properties
282
+
// /path/to/hidden: included
283
+
// /path/to/hidden/property: excluded
284
+
// /path/to/hidden/property/nesting: excluded
285
+
286
+
// Array element properties
287
+
// /path/into/array/0: included
288
+
// /path/into/array/345345/property: excluded
289
+
// /path/to/one/array/0/element: included
290
+
// /path/to/one/array/1/element: excluded
291
+
```
292
+
293
+
This will exclude the given properties and _all nested_ paths. Excluding `/` however will not work, since then you can just disable the plugin.
294
+
295
+
- If a property is `{}` or `undefined` after processing all excludes statements, it will _not_ be included in the patch.
296
+
- Arrays work a little different. Since json-patch-operations work on the array index, array elements that are `{}` or `undefined` are still added to the patch. This brings support for later `remove` or `replace` operations to work as intended.<br/>
297
+
The `ARRAY_WILDCARD``*` matches every array element.
298
+
299
+
If there are any bugs experienced with the `excludes` feature please write an issue so we can fix it!
* Checks the provided `json-patch-operation` on `excludePath`. This check joins the `path` and `value` property of the `operation` and removes any hit.
368
+
*
369
+
* @param {import('fast-json-patch').Operation} patch operation to check with `excludePath`
370
+
* @param {String[]} excludePath Path to property to remove from value of `operation`
371
+
*
372
+
* @return `false` if `patch.value` is `{}` or `undefined` after remove, `true` in any other case
* Checks the provided `json-patch-operation` on `excludePath`. This check joins the `path` and `value` property of the `operation` and removes any hit.
56
+
*
57
+
* @param {import('fast-json-patch').Operation} patch operation to check with `excludePath`
58
+
* @param {String[]} excludePath Path to property to remove from value of `operation`
59
+
*
60
+
* @return `false` if `patch.value` is `{}` or `undefined` after remove, `true` in any other case
0 commit comments