1
- var async = require ( 'async' ) ;
2
- var through2 = require ( 'through2' ) ;
1
+ const async = require ( 'async' ) ;
2
+ const through2 = require ( 'through2' ) ;
3
3
4
4
module . exports = function ( transforms ) {
5
5
@@ -8,7 +8,7 @@ module.exports = function(transforms) {
8
8
}
9
9
10
10
function _transformReadyData ( data , fromVersion , toVersion , preparedData , options ) {
11
- var version , transformationCode ;
11
+ let version , transformationCode ;
12
12
if ( fromVersion < toVersion ) {
13
13
for ( version = fromVersion ; version < toVersion ; version ++ ) {
14
14
transformationCode = 'V' + version + 'toV' + ( version + 1 ) ;
@@ -34,7 +34,7 @@ module.exports = function(transforms) {
34
34
if ( err ) {
35
35
return callback ( err ) ;
36
36
}
37
- var transformedData = _transformReadyData ( data , fromVersion , toVersion , preparedData , options ) ;
37
+ const transformedData = _transformReadyData ( data , fromVersion , toVersion , preparedData , options ) ;
38
38
if ( transformedData instanceof Error ) {
39
39
return callback ( transformedData ) ;
40
40
}
@@ -45,9 +45,9 @@ module.exports = function(transforms) {
45
45
function getTransformStream ( fromVersion , toVersion , mongo , options ) {
46
46
fromVersion = parseVersion ( fromVersion ) ;
47
47
toVersion = parseVersion ( toVersion ) ;
48
- var preparedDataSets = null ;
48
+ let preparedDataSets = null ;
49
49
return through2 . obj ( function ( obj , encoding , callback ) {
50
- var through = this ;
50
+ const through = this ;
51
51
if ( ! preparedDataSets ) {
52
52
_prepareTransform ( null , fromVersion , toVersion , mongo , options , function ( err , _preparedDataSets ) {
53
53
preparedDataSets = _preparedDataSets ;
@@ -62,37 +62,73 @@ module.exports = function(transforms) {
62
62
}
63
63
64
64
function _prepareTransform ( id , fromVersion , toVersion , mongo , options , callback ) {
65
- var version , transformationCode ;
66
- var tasks = { } ;
65
+ const tasks = _createTasksForVersions (
66
+ fromVersion ,
67
+ toVersion ,
68
+ ( version , transformationCode ) => createPrepareTransformTask ( id , version , transformationCode , mongo , options ) ) ;
69
+ return async . parallel ( tasks , callback ) ;
70
+ }
71
+
72
+ function _createTasksForVersions ( fromVersion , toVersion , createTaskForVersion ) {
73
+ let version , transformationCode ;
74
+ const tasks = { } ;
67
75
if ( fromVersion < toVersion ) {
68
76
for ( version = fromVersion ; version < toVersion ; version ++ ) {
69
77
transformationCode = 'V' + version + 'toV' + ( version + 1 ) ;
70
- tasks [ transformationCode ] = createPrepareTransformTask ( id , version + 1 , transformationCode , mongo , options ) ;
78
+ tasks [ transformationCode ] = createTaskForVersion ( version + 1 , transformationCode ) ;
71
79
}
72
- async . parallel ( tasks , callback ) ;
73
80
} else if ( fromVersion > toVersion ) {
74
81
for ( version = fromVersion ; version > toVersion ; version -- ) {
75
82
transformationCode = 'V' + version + 'toV' + ( version - 1 ) ;
76
- tasks [ transformationCode ] = createPrepareTransformTask ( id , version , transformationCode , mongo , options ) ;
83
+ tasks [ transformationCode ] = createTaskForVersion ( version , transformationCode ) ;
77
84
}
78
- async . parallel ( tasks , callback ) ;
79
- } else {
80
- callback ( ) ;
81
85
}
86
+ return tasks ;
82
87
}
83
88
84
89
function createPrepareTransformTask ( id , _version , _transformationCode , mongo , options ) {
85
- var prepareTransform = transforms [ 'v' + _version ] [ _transformationCode ] . prepareTransform ;
90
+ const prepareTransform = transforms [ 'v' + _version ] [ _transformationCode ] . prepareTransform ;
91
+ if ( ! prepareTransform ) {
92
+ return noopTask ;
93
+ }
86
94
if ( options ) {
87
- prepareTransform = prepareTransform || function ( id , mongo , options , cb ) { cb ( ) ; } ;
88
95
return prepareTransform . bind ( null , id , mongo , options ) ;
89
96
}
90
- prepareTransform = prepareTransform || function ( id , mongo , cb ) { cb ( ) ; } ;
91
97
return prepareTransform . bind ( null , id , mongo ) ;
92
98
}
93
99
100
+ function checkCompatibility ( id , fromVersion , toVersion , mongo , options , callback ) {
101
+ if ( typeof options === 'function' ) {
102
+ callback = options ;
103
+ options = null ;
104
+ }
105
+ fromVersion = parseVersion ( fromVersion ) ;
106
+ toVersion = parseVersion ( toVersion ) ;
107
+ const tasks = _createTasksForVersions (
108
+ fromVersion ,
109
+ toVersion ,
110
+ ( version , transformationCode ) => createCheckCompatibilityTask ( id , version , transformationCode , mongo , options ) ) ;
111
+ return async . parallel ( tasks , callback ) ;
112
+ }
113
+
114
+ function createCheckCompatibilityTask ( id , _version , _transformationCode , mongo , options ) {
115
+ const checkCompatibility = transforms [ 'v' + _version ] [ _transformationCode ] . checkCompatibility ;
116
+ if ( ! checkCompatibility ) {
117
+ return noopTask ;
118
+ }
119
+ if ( options ) {
120
+ return checkCompatibility . bind ( null , id , mongo , options ) ;
121
+ }
122
+ return checkCompatibility . bind ( null , id , mongo ) ;
123
+ }
124
+
125
+ function noopTask ( cb ) {
126
+ cb ( ) ;
127
+ }
128
+
94
129
return {
95
- transformObject : transformObject ,
96
- getTransformStream : getTransformStream
130
+ transformObject,
131
+ getTransformStream,
132
+ checkCompatibility
97
133
} ;
98
134
} ;
0 commit comments