@@ -5,15 +5,15 @@ import jsonpatch from 'fast-json-patch'
5
5
import { decamelize , pascalize } from 'humps'
6
6
import { dropRightWhile , each , map , merge , omit , get , tail } from 'lodash'
7
7
8
- export const RollbackError = function ( message , extra ) {
8
+ export const RollbackError = function ( message , extra ) {
9
9
Error . captureStackTrace ( this , this . constructor )
10
10
this . name = 'RollbackError'
11
11
this . message = message
12
12
}
13
13
14
14
require ( 'util' ) . inherits ( RollbackError , Error )
15
15
16
- const createPatchModel = options => {
16
+ const createPatchModel = ( options ) => {
17
17
const def = {
18
18
date : { type : Date , required : true , default : Date . now } ,
19
19
ops : { type : [ ] , required : true } ,
@@ -49,7 +49,7 @@ const ARRAY_INDEX_WILDCARD = '*'
49
49
*
50
50
* @param {string } path Path to split
51
51
*/
52
- const getArrayFromPath = path => path . replace ( / ^ \/ / , '' ) . split ( '/' )
52
+ const getArrayFromPath = ( path ) => path . replace ( / ^ \/ / , '' ) . split ( '/' )
53
53
54
54
/**
55
55
* Checks the provided `json-patch-operation` on `excludePath`. This check joins the `path` and `value` property of the `operation` and removes any hit.
@@ -74,7 +74,7 @@ const deepRemovePath = (patch, excludePath) => {
74
74
// start over with each array element and make a fresh check
75
75
// Note: it can happen that array elements are rendered to: {}
76
76
// we need to keep them to keep the order of array elements consistent
77
- value . forEach ( elem => {
77
+ value . forEach ( ( elem ) => {
78
78
deepRemovePath ( { path : '/' , value : elem } , excludePath . slice ( i + 1 ) )
79
79
} )
80
80
@@ -101,7 +101,7 @@ const deepRemovePath = (patch, excludePath) => {
101
101
* Sanitizes a path `['']` to be used with `isPathContained()`
102
102
* @param {String[] } path
103
103
*/
104
- const sanitizeEmptyPath = path =>
104
+ const sanitizeEmptyPath = ( path ) =>
105
105
path . length === 1 && path [ 0 ] === '' ? [ ] : path
106
106
107
107
// Checks if 'fractionPath' is contained in fullPath
@@ -119,15 +119,15 @@ const entryIsIdentical = (entry1, entry2) => entry1 === entry2
119
119
const matchesArrayWildcard = ( entry1 , entry2 ) =>
120
120
isArrayIndexWildcard ( entry1 ) && isIntegerGreaterEqual0 ( entry2 )
121
121
122
- const isArrayIndexWildcard = entry => entry === ARRAY_INDEX_WILDCARD
122
+ const isArrayIndexWildcard = ( entry ) => entry === ARRAY_INDEX_WILDCARD
123
123
124
- const isIntegerGreaterEqual0 = entry =>
124
+ const isIntegerGreaterEqual0 = ( entry ) =>
125
125
Number . isInteger ( Number ( entry ) ) && Number ( entry ) >= 0
126
126
127
127
// used to convert bson to json - especially ObjectID references need
128
128
// to be converted to hex strings so that the jsonpatch `compare` method
129
129
// works correctly
130
- const toJSON = obj => JSON . parse ( JSON . stringify ( obj ) )
130
+ const toJSON = ( obj ) => JSON . parse ( JSON . stringify ( obj ) )
131
131
132
132
// helper function to merge query conditions after an update has happened
133
133
// usefull if a property which was initially defined in _conditions got overwritten
@@ -137,13 +137,13 @@ const mergeQueryConditionsWithUpdate = (_conditions, _update) => {
137
137
const conditions = Object . assign ( { } , conditions , update )
138
138
139
139
// excluding updates other than $set
140
- Object . keys ( conditions ) . forEach ( key => {
140
+ Object . keys ( conditions ) . forEach ( ( key ) => {
141
141
if ( key . includes ( '$' ) ) delete conditions [ key ]
142
142
} )
143
143
return conditions
144
144
}
145
145
146
- export default function ( schema , opts ) {
146
+ export default function ( schema , opts ) {
147
147
const options = merge ( { } , defaultOptions , opts )
148
148
149
149
// get _id type from schema
@@ -160,7 +160,7 @@ export default function(schema, opts) {
160
160
161
161
// used to compare instance data snapshots. depopulates instance,
162
162
// removes version key and object id
163
- schema . methods . data = function ( ) {
163
+ schema . methods . data = function ( ) {
164
164
return this . toObject ( {
165
165
depopulate : true ,
166
166
versionKey : false ,
@@ -182,15 +182,18 @@ export default function(schema, opts) {
182
182
. sort ( { date : 1 } )
183
183
. exec ( )
184
184
. then (
185
- patches =>
185
+ ( patches ) =>
186
186
new Promise ( ( resolve , reject ) => {
187
187
// patch doesn't exist
188
188
if ( ! ~ map ( patches , 'id' ) . indexOf ( patchId ) ) {
189
189
return reject ( new RollbackError ( "patch doesn't exist" ) )
190
190
}
191
191
192
192
// get all patches that should be applied
193
- const apply = dropRightWhile ( patches , patch => patch . id !== patchId )
193
+ const apply = dropRightWhile (
194
+ patches ,
195
+ ( patch ) => patch . id !== patchId
196
+ )
194
197
195
198
// if the patches that are going to be applied are all existing patches,
196
199
// the rollback attempts to rollback to the latest patch
@@ -200,7 +203,7 @@ export default function(schema, opts) {
200
203
201
204
// apply patches to `state`
202
205
const state = { }
203
- apply . forEach ( patch => {
206
+ apply . forEach ( ( patch ) => {
204
207
jsonpatch . applyPatch ( state , patch . ops , true )
205
208
} )
206
209
@@ -223,7 +226,7 @@ export default function(schema, opts) {
223
226
224
227
// after a document is initialized or saved, fresh snapshots of the
225
228
// documents data are created
226
- const snapshot = function ( ) {
229
+ const snapshot = function ( ) {
227
230
this . _original = toJSON ( this . data ( ) )
228
231
}
229
232
schema . post ( 'init' , snapshot )
@@ -235,10 +238,10 @@ export default function(schema, opts) {
235
238
const { _id : ref } = document
236
239
return document . patches
237
240
. find ( { ref : document . _id } )
238
- . then ( patches => join ( patches . map ( patch => patch . remove ( ) ) ) )
241
+ . then ( ( patches ) => join ( patches . map ( ( patch ) => patch . remove ( ) ) ) )
239
242
}
240
243
241
- schema . pre ( 'remove' , function ( next ) {
244
+ schema . pre ( 'remove' , function ( next ) {
242
245
if ( ! options . removePatches ) {
243
246
return next ( )
244
247
}
@@ -259,12 +262,12 @@ export default function(schema, opts) {
259
262
toJSON ( document . data ( ) )
260
263
)
261
264
if ( options . excludes . length > 0 ) {
262
- ops = ops . filter ( op => {
265
+ ops = ops . filter ( ( op ) => {
263
266
const pathArray = getArrayFromPath ( op . path )
264
267
return (
265
- ! options . excludes . some ( exclude =>
268
+ ! options . excludes . some ( ( exclude ) =>
266
269
isPathContained ( exclude , pathArray )
267
- ) && options . excludes . every ( exclude => deepRemovePath ( op , exclude ) )
270
+ ) && options . excludes . every ( ( exclude ) => deepRemovePath ( op , exclude ) )
268
271
)
269
272
} )
270
273
}
@@ -276,7 +279,7 @@ export default function(schema, opts) {
276
279
277
280
// track original values if enabled
278
281
if ( options . trackOriginalValue ) {
279
- ops . map ( entry => {
282
+ ops . map ( ( entry ) => {
280
283
const path = tail ( entry . path . split ( '/' ) ) . join ( '.' )
281
284
entry . originalValue = get (
282
285
document . isNew ? { } : document . _original ,
@@ -295,20 +298,20 @@ export default function(schema, opts) {
295
298
return document . patches . create ( data )
296
299
}
297
300
298
- schema . pre ( 'save' , function ( next ) {
301
+ schema . pre ( 'save' , function ( next ) {
299
302
createPatch ( this )
300
303
. then ( ( ) => next ( ) )
301
304
. catch ( next )
302
305
} )
303
306
304
- schema . pre ( 'findOneAndRemove' , function ( next ) {
307
+ schema . pre ( 'findOneAndRemove' , function ( next ) {
305
308
if ( ! options . removePatches ) {
306
309
return next ( )
307
310
}
308
311
309
312
this . model
310
313
. findOne ( this . _conditions )
311
- . then ( original => deletePatches ( original ) )
314
+ . then ( ( original ) => deletePatches ( original ) )
312
315
. then ( ( ) => next ( ) )
313
316
. catch ( next )
314
317
} )
@@ -318,7 +321,7 @@ export default function(schema, opts) {
318
321
function preUpdateOne ( next ) {
319
322
this . model
320
323
. findOne ( this . _conditions )
321
- . then ( original => {
324
+ . then ( ( original ) => {
322
325
if ( original ) this . _originalId = original . _id
323
326
original = original || new this . model ( { } )
324
327
this . _original = toJSON ( original . data ( ) )
@@ -327,7 +330,7 @@ export default function(schema, opts) {
327
330
. catch ( next )
328
331
}
329
332
330
- schema . post ( 'findOneAndUpdate' , function ( doc , next ) {
333
+ schema . post ( 'findOneAndUpdate' , function ( doc , next ) {
331
334
if ( ! this . options . new ) {
332
335
return postUpdateOne . call ( this , { } , next )
333
336
}
@@ -351,7 +354,7 @@ export default function(schema, opts) {
351
354
352
355
this . model
353
356
. findOne ( conditions )
354
- . then ( doc => {
357
+ . then ( ( doc ) => {
355
358
if ( ! doc ) return next ( )
356
359
doc . _original = this . _original
357
360
return createPatch ( doc , this . options )
@@ -366,7 +369,7 @@ export default function(schema, opts) {
366
369
function preUpdateMany ( next ) {
367
370
this . model
368
371
. find ( this . _conditions )
369
- . then ( originals => {
372
+ . then ( ( originals ) => {
370
373
const originalIds = [ ]
371
374
const originalData = [ ]
372
375
for ( const original of originals ) {
@@ -393,7 +396,7 @@ export default function(schema, opts) {
393
396
394
397
this . model
395
398
. find ( conditions )
396
- . then ( docs =>
399
+ . then ( ( docs ) =>
397
400
Promise . all (
398
401
docs . map ( ( doc , i ) => {
399
402
doc . _original = this . _originals [ i ]
@@ -408,14 +411,14 @@ export default function(schema, opts) {
408
411
schema . pre ( 'updateMany' , preUpdateMany )
409
412
schema . post ( 'updateMany' , postUpdateMany )
410
413
411
- schema . pre ( 'update' , function ( next ) {
414
+ schema . pre ( 'update' , function ( next ) {
412
415
if ( this . options . multi ) {
413
416
preUpdateMany . call ( this , next )
414
417
} else {
415
418
preUpdateOne . call ( this , next )
416
419
}
417
420
} )
418
- schema . post ( 'update' , function ( result , next ) {
421
+ schema . post ( 'update' , function ( result , next ) {
419
422
if ( this . options . many ) {
420
423
postUpdateMany . call ( this , result , next )
421
424
} else {
0 commit comments