-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
139 lines (120 loc) · 3.09 KB
/
index.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
var Db = require('mongodb').Db
, Server = require('mongodb').Server
, sh = require('shelljs')
, o = 0
, Resource = require('deployd/lib/resource')
, util = require('util')
, path = require('path')
, waiter
, log = function() {}
function Importer() {
Resource.apply(this, arguments);
}
module.exports = Importer;
util.inherits(Importer, Resource);
Importer.dashboard = {
path: path.join(__dirname, 'dashboard')
}
Importer.prototype.handle = function (ctx) {
if(ctx.method === 'POST' && ctx.req.isRoot) {
waiter = function (err) {
ctx.done(err);
}
log = function (data) {
ctx.req.session.socket.emit('importer log', data);
}
start(ctx.body);
} else {
ctx.done();
}
}
var db;
function start(options) {
db = new Db(options.name, new Server(options.host, options.port));
db.open(getCollections);
}
function getCollections() {
log('getting collections');
db.collections(function(err, collections) {
collections.forEach(createCollection)
});
}
function createCollection(col) {
log('creating collection ' + col);
if(~col.collectionName.indexOf('.')) {
console.log('skipping', col.collectionName);
} else {
console.log('creating', col.collectionName);
sh.mkdir('-p', 'resources/' + col.collectionName);
var config = {
type: 'Collection',
typeLabel: 'Collection',
order: o++,
properties: {},
path: '/' + col.collectionName
};
var stream = col.find().stream();
var store = process.server.createStore(col.collectionName);
stream.on('data', migrate(config, col, store));
stream.on('error', stop);
stream.on('close', end(config));
}
}
function migrate(config, col, store) {
return function (obj) {
Object.keys(obj).forEach(function (key) {
switch(typeof obj[key]) {
case 'string':
proprify(obj[key], key, 'string');
break;
case 'object':
if(Array.isArray(obj[key])) {
proprify(obj[key], key, 'string');
} else if(obj[key] instanceof Date) {
proprify(obj[key], key, 'number');
} else {
proprify(obj[key], key, 'object');
}
break;
case 'number':
proprify(obj[key], key, 'number');
break;
case 'boolean':
proprify(obj[key], key, 'boolean');
break;
}
});
function proprify(val, key, type) {
if(key === '_id') {
return;
}
config.properties[key] = {
type: type,
typeLabel: type,
id: key,
name: key
};
}
store.insert(obj, function (err) {});
}
}
var remaining = 0;
var fs = require('fs');
function end(config) {
remaining++;
return function () {
var json = JSON.stringify(config, null, ' ');
fs.writeFile('resources' + config.path + '/config.json', json, function (err) {
remaining--;
if(!remaining) {
console.log('done');
if(waiter) waiter();
}
});
}
}
function stop(err) {
console.log('migration failed...');
console.error(err);
if(waiter) waiter(err);
}