forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopbackMigration.js
231 lines (212 loc) · 5.53 KB
/
loopbackMigration.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* eslint-disable no-process-exit */
require('dotenv').load();
var Rx = require('rx'),
uuid = require('uuid'),
assign = require('lodash/object/assign'),
mongodb = require('mongodb'),
secrets = require('../config/secrets');
const batchSize = 20;
var MongoClient = mongodb.MongoClient;
Rx.config.longStackSupport = true;
var providers = [
'facebook',
'twitter',
'google',
'github',
'linkedin'
];
// create async console.logs
function debug() {
var args = [].slice.call(arguments);
process.nextTick(function() {
console.log.apply(console, args);
});
}
function createConnection(URI) {
return Rx.Observable.create(function(observer) {
debug('connecting to db');
MongoClient.connect(URI, function(err, database) {
if (err) {
return observer.onError(err);
}
debug('db connected');
observer.onNext(database);
observer.onCompleted();
});
});
}
function createQuery(db, collection, options, batchSize) {
return Rx.Observable.create(function(observer) {
var cursor = db.collection(collection).find({}, options);
cursor.batchSize(batchSize || 20);
// Cursor.each will yield all doc from a batch in the same tick,
// or schedule getting next batch on nextTick
debug('opening cursor for %s', collection);
cursor.each(function(err, doc) {
if (err) {
return observer.onError(err);
}
if (!doc) {
console.log('onCompleted');
return observer.onCompleted();
}
observer.onNext(doc);
});
return Rx.Disposable.create(function() {
debug('closing cursor for %s', collection);
cursor.close();
});
});
}
function getUserCount(db) {
return Rx.Observable.create(function(observer) {
var cursor = db.collection('users').count(function(err, count) {
if (err) {
return observer.onError(err);
}
observer.onNext(count);
observer.onCompleted();
return Rx.Disposable.create(function() {
debug('closing user count');
cursor.close();
});
});
});
}
function insertMany(db, collection, users, options) {
return Rx.Observable.create(function(observer) {
db.collection(collection).insertMany(users, options, function(err) {
if (err) {
return observer.onError(err);
}
observer.onNext();
observer.onCompleted();
});
});
}
var count = 0;
// will supply our db object
var dbObservable = createConnection(secrets.db).replay();
var totalUser = dbObservable
.flatMap(function(db) {
return getUserCount(db);
})
.shareReplay();
var users = dbObservable
.flatMap(function(db) {
// returns user document, n users per loop where n is the batchsize.
return createQuery(db, 'users', {}, batchSize);
})
.map(function(user) {
// flatten user
assign(user, user.portfolio, user.profile);
if (!user.username) {
user.username = 'fcc' + uuid.v4().slice(0, 8);
}
if (user.github) {
user.isGithubCool = true;
} else {
user.isMigrationGrandfathered = true;
}
providers.forEach(function(provider) {
user[provider + 'id'] = user[provider];
user[provider] = null;
});
user.rand = Math.random();
return user;
})
.shareReplay();
// batch them into arrays of twenty documents
var userSavesCount = users
.bufferWithCount(batchSize)
// get bd object ready for insert
.withLatestFrom(dbObservable, function(users, db) {
return {
users: users,
db: db
};
})
.flatMap(function(dats) {
// bulk insert into new collection for loopback
return insertMany(dats.db, 'user', dats.users, { w: 1 });
})
.flatMap(function() {
return totalUser;
})
.doOnNext(function(totalUsers) {
count = count + batchSize;
debug('user progress %s', count / totalUsers * 100);
})
// count how many times insert completes
.count();
// create User Identities
var userIdentityCount = users
.flatMap(function(user) {
var ids = providers
.map(function(provider) {
return {
provider: provider,
externalId: user[provider + 'id'],
userId: user._id || user.id
};
})
.filter(function(ident) {
return !!ident.externalId;
});
return Rx.Observable.from(ids);
})
.bufferWithCount(batchSize)
.withLatestFrom(dbObservable, function(identities, db) {
return {
identities: identities,
db: db
};
})
.flatMap(function(dats) {
// bulk insert into new collection for loopback
return insertMany(dats.db, 'userIdentity', dats.identities, { w: 1 });
})
// count how many times insert completes
.count();
/*
var storyCount = dbObservable
.flatMap(function(db) {
return createQuery(db, 'stories', {}, batchSize);
})
.bufferWithCount(batchSize)
.withLatestFrom(dbObservable, function(stories, db) {
return {
stories: stories,
db: db
};
})
.flatMap(function(dats) {
return insertMany(dats.db, 'story', dats.stories, { w: 1 });
})
.count();
*/
Rx.Observable.combineLatest(
userIdentityCount,
userSavesCount,
// storyCount,
function(userIdentCount, userCount) {
return {
userIdentCount: userIdentCount * batchSize,
userCount: userCount * batchSize
// storyCount: storyCount * batchSize
};
})
.subscribe(
function(countObj) {
console.log('next');
count = countObj;
},
function(err) {
console.error('an error occured', err, err.stack);
},
function() {
console.log('finished with ', count);
process.exit(0);
}
);
dbObservable.connect();