-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsir-transformalot.js
158 lines (145 loc) · 5.25 KB
/
sir-transformalot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const async = require('async');
const through2 = require('through2');
module.exports = function(transforms) {
function parseVersion(versionString) {
return parseInt(versionString.substring(1));
}
function _transformReadyData(data, fromVersion, toVersion, preparedData, options) {
let version, transformationCode;
if (fromVersion < toVersion) {
for (version = fromVersion; version < toVersion; version++) {
transformationCode = 'V' + version + 'toV' + (version + 1);
data = transforms['v' + (version + 1)][transformationCode].transform(data, preparedData[transformationCode], options);
}
} else if (fromVersion > toVersion) {
for (version = fromVersion; version > toVersion; version--) {
transformationCode = 'V' + version + 'toV' + (version - 1);
data = transforms['v' + version][transformationCode].transform(data, preparedData[transformationCode], options);
}
}
return data;
}
function transformObject(id, data, fromVersion, toVersion, mongo, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
if (!fromVersion) {
return callback(new Error('fromVersion is missing in transformObject'));
}
if (!toVersion) {
return callback(new Error('toVersion is missing in transformObject'));
}
fromVersion = parseVersion(fromVersion);
toVersion = parseVersion(toVersion);
_prepareTransform([id], fromVersion, toVersion, mongo, options, function(err, preparedData) {
if (err) {
return callback(err);
}
const transformedData = _transformReadyData(data, fromVersion, toVersion, preparedData, options);
if (transformedData instanceof Error) {
return callback(transformedData);
}
return callback(null, transformedData);
});
}
function getTransformStream(fromVersion, toVersion, mongo, options) {
let parsedFromVersion, parsedToVersion;
let preparedDataSets = null;
return through2.obj(function(obj, encoding, callback){
if (!parsedFromVersion) {
if (!fromVersion) {
return callback(new Error('fromVersion is missing in getTransformStream'));
}
parsedFromVersion = parseVersion(fromVersion);
}
if (!parsedToVersion) {
if (!toVersion) {
return callback(new Error('toVersion is missing in getTransformStream'));
}
parsedToVersion = parseVersion(toVersion);
}
const through = this;
if(!preparedDataSets) {
_prepareTransform(null, parsedFromVersion, parsedToVersion, mongo, options, function(err, _preparedDataSets){
preparedDataSets = _preparedDataSets;
through.push(_transformReadyData(obj, parsedFromVersion, parsedToVersion, preparedDataSets, options));
callback();
});
} else {
through.push(_transformReadyData(obj, parsedFromVersion, parsedToVersion, preparedDataSets, options));
callback();
}
});
}
function _prepareTransform(id, fromVersion, toVersion, mongo, options, callback) {
const tasks = _createTasksForVersions(
fromVersion,
toVersion,
(version, transformationCode) => createPrepareTransformTask(id, version, transformationCode, mongo, options));
return async.parallel(tasks, callback);
}
function _createTasksForVersions(fromVersion, toVersion, createTaskForVersion) {
let version, transformationCode;
const tasks = {};
if (fromVersion < toVersion) {
for (version = fromVersion; version < toVersion; version++) {
transformationCode = 'V' + version + 'toV' + (version + 1);
tasks[transformationCode] = createTaskForVersion(version + 1, transformationCode);
}
} else if (fromVersion > toVersion) {
for (version = fromVersion; version > toVersion; version--) {
transformationCode = 'V' + version + 'toV' + (version - 1);
tasks[transformationCode] = createTaskForVersion(version, transformationCode);
}
}
return tasks;
}
function createPrepareTransformTask(id, _version, _transformationCode, mongo, options) {
const prepareTransform = transforms['v' + _version][_transformationCode].prepareTransform;
if (!prepareTransform) {
return noopTask;
}
if (options) {
return prepareTransform.bind(null, id, mongo, options);
}
return prepareTransform.bind(null, id, mongo);
}
function checkCompatibility(id, fromVersion, toVersion, mongo, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
if (!fromVersion) {
return callback(new Error('fromVersion is missing in checkCompatibility'));
}
if (!toVersion) {
return callback(new Error('toVersion is missing in checkCompatibility'));
}
fromVersion = parseVersion(fromVersion);
toVersion = parseVersion(toVersion);
const tasks = _createTasksForVersions(
fromVersion,
toVersion,
(version, transformationCode) => createCheckCompatibilityTask(id, version, transformationCode, mongo, options));
return async.parallel(tasks, callback);
}
function createCheckCompatibilityTask(id, _version, _transformationCode, mongo, options) {
const checkCompatibility = transforms['v' + _version][_transformationCode].checkCompatibility;
if (!checkCompatibility) {
return noopTask;
}
if (options) {
return checkCompatibility.bind(null, id, mongo, options);
}
return checkCompatibility.bind(null, id, mongo);
}
function noopTask(cb) {
cb();
}
return {
transformObject,
getTransformStream,
checkCompatibility
};
};