-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportTreeMaster.js
More file actions
76 lines (64 loc) · 1.99 KB
/
importTreeMaster.js
File metadata and controls
76 lines (64 loc) · 1.99 KB
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
var fs = require('fs');
var Converter = require('csvtojson').Converter;
var converter = new Converter({});
// Cloudant用アクセス・モジュール「cradle」設定
var Cloudant = require('cloudant');
// Cloudant DB接続情報取得
var services = {};
if (typeof process.env.VCAP_SERVICES === 'undefined') {
services = require('./config/VCAP_SERVICES.json');
} else {
services = JSON.parse(process.env.VCAP_SERVICES)
};
var credentials = services['cloudantNoSQLDB'][0].credentials;
var host = credentials.host;
var port = credentials.port;
var options = {
cache: true,
raw: false,
secure: true,
auth: {
username: credentials.username,
password: credentials.password
}
};
// データベース接続
var cloudant = Cloudant({
account: credentials.username,
password: credentials.password
});
var db = cloudant.db;
converter.on("end_parsed", function(jsonArray) {
var groupCount = 1;
var arr = jsonArray.filter(function(elem, index, array) {
if (elem.parentId == 0) {
array[index].tree_group = groupCount;
groupCount++;
}
return elem.parentId == 0;
})
for (var i = 0; i < arr.length; i++) {
setTreeGroup(arr[i].id, jsonArray, arr[i].tree_group);
}
db.use('remixtree').bulk({
docs: jsonArray
}, function(er) {
if (er) {
throw er;
}
console.log('Inserted all documents');
});
fs.writeFile('./files/sample.json', JSON.stringify(jsonArray, null, ' '));
});
var setTreeGroup = function(id, rowArray, groupId) {
var targetElement = rowArray.filter(function(elem, index, array) {
if (elem.id == id) {
array[index].tree_group = groupId;
for (var i = 0; i < array[index].childrenIds.length; i++) {
setTreeGroup(array[index].childrenIds[i], array, groupId);
}
}
return true;
});
}
require("fs").createReadStream("./files/tree_master.csv").pipe(converter);