-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbCredentials.js
65 lines (58 loc) · 2.62 KB
/
dbCredentials.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
var fs = require('fs');
var cradle = require(__dirname+'/node_modules/cradle'); // Driver for node.js Couchdb/Cloudantdb
// >>>>> Für running on Bluemix noch nicht überprüft <<<<<
var dbCredentials = {
dbName : 'bikertreffs'
};
// Prüfen, ob running on Bluemix
if(process.env.VCAP_SERVICES) {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
if(vcapServices.cloudantNoSQLDB) {
dbCredentials.host = vcapServices.cloudantNoSQLDB[0].credentials.host;
dbCredentials.port = vcapServices.cloudantNoSQLDB[0].credentials.port;
dbCredentials.user = vcapServices.cloudantNoSQLDB[0].credentials.username;
dbCredentials.password = vcapServices.cloudantNoSQLDB[0].credentials.password;
dbCredentials.url = vcapServices.cloudantNoSQLDB[0].credentials.url;
} // End if(vcapServices.cloudantNoSQLDB)
//console.log('VCAP Services: '+JSON.stringify(process.env.VCAP_SERVICES));
console.log('>>>>> Running on Bluemix <<<<<');
} else {
// Wenn die Datei config/local.on vorhanden ist, wird eine lokale CouchDB verwendet.
// Wenn diese Datei fehlt, wird auch bei lokalem Test die Bluemix-CloudantDB benutzt
if (fs.existsSync(__dirname+'/config/local.on')) {
dbCredentials.host = 'http://localhost';
dbCredentials.port = '5984';
dbCredentials.user = '';
dbCredentials.password = '';
//console.log('Local login information: ' + JSON.stringify(dbCredentials));
console.log('>>>>> Lokal mit lokaler CouchDB <<<<<');
// Herstellen der DB-Verbindung
var connection = new(cradle.Connection)(dbCredentials.host, dbCredentials.port, {});
var dbConnection = connection.database(dbCredentials.dbName);
} else {
var CloudantCredential = require(__dirname+'/config/cloudant_Credentials');
dbCredentials.host = CloudantCredential.host;
dbCredentials.port = CloudantCredential.port;
dbCredentials.user = CloudantCredential.user;
dbCredentials.password = CloudantCredential.password;
dbCredentials.url = CloudantCredential.url;
console.log('>>>>> Lokal mit Bluemix CloudantDB <<<<<');
// Herstellen der DB-Verbindung
var dbConnection = new (cradle.Connection)(dbCredentials.host,
{auth:{username: dbCredentials.user,
password: dbCredentials.password}}).database(dbCredentials.dbName);
}
}
// Checken, ob man die Datenbank erreichen kann
/*dbConnection.exists(function (err, exists) {
if (err) {
console.log('error', err);
} else if (exists) {
console.log('the force is with you.');
} else {
console.log('database does not exist.');
throw (err);
}
});
*/
module.exports = dbConnection;