-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycloudant.js
122 lines (109 loc) · 2.55 KB
/
mycloudant.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
var cradle = require('cradle');
var connection;
var db;
function initConnection(host, port, user, password, cb){
//connection = new(cradle.Connection)(url, port, {
// auth: { username: user, password: password }
//});
connection = new(cradle.Connection)('https://' + host, port, {
cache: true,
secure: true,
raw: false,
auth: { username: user, password: password }
});
db = connection.database(dbName);
db.exists(function(err, exists){
if(err){
console.log('Error accessing db' + err );
cb(err,null);
} else if (exists) {
console.log('db ' + dbName + ' exists');
cb(null, db);
} else {
console.log('db ' + dbName + ' will be created');
db.create();
cb(null,'ok');
}
});
}
function createExampleMutants(mutantName, cb){
var inserted = 0;
for(var i = 0; i < 10; i++) {
db.save({
name: mutantName + i
}, function (err, res) {
if (err) {
cb(err,null);
}
if (++inserted == 10) {
cb(null, 'Mutants created');
}
});
}
}
function createViews(cb){
db.save('_design/bikertreffs', {
all: {
map: function (doc) {
if (doc.name) emit(doc.name, doc);
}
},
allBikertreffs: {
map: function (doc) {
if (doc.properties.name) emit({name: doc.properties.name, coordinates: doc.geometry.coordinates});
//if (doc.properties.name) emit(doc.properties.name, doc);
}
},
},function (err, res) {
if(err){
cb(err,null);
}
else {
cb(null,'Views created');
}
});
}
function findAll(cb){
db.view('mutants/all', {
},function (err, res) {
if(err){
cb(err,null);
} else {
var docs = [];
res.forEach(function (row) {
docs.push( " " + row.name);
//console.log(row.name);
});
cb(null, docs.length + ' Mutants read: ' + docs);
}
});
}
function findAllBikertreffs2(callback){
db.view('bikertreffs/allBikertreffs', {
},function (err, res) {
if(err){
callback(err, null);
}
else{
//console.log(res);
var docs = [];
res.forEach(function (row){
//console.log(row.key.name);
//console.log(row.key.coordinates);
docs.push('\n' + '{"loc":[' + row.key.coordinates + '], '
+ '"title": ' + row.key.name + '}'
).toString();
});
console.log(docs);
callback(null, docs);
}
});
}
module.exports = {
initConnection: initConnection,
createExampleMutants: createExampleMutants,
createViews: createViews,
findAll: findAll,
// findAllBikertreffs: findAllBikertreffs,
findAllBikertreffs2: findAllBikertreffs2
};