@@ -90,24 +90,23 @@ module.exports = function(RED) {
90
90
function CloudantNode ( n ) {
91
91
RED . nodes . createNode ( this , n ) ;
92
92
93
- this . name = n . name ;
94
- this . hostname = n . hostname ;
93
+ this . name = n . name ;
94
+ this . host = n . host ;
95
+
96
+ // remove unnecessary parts from host value
97
+ var parsedUrl = url . parse ( this . host ) ;
98
+ if ( parsedUrl . host ) {
99
+ this . host = parsedUrl . host ;
100
+ }
101
+
102
+ // extract only the account name
103
+ this . account = this . host . substring ( 0 , this . host . indexOf ( '.' ) ) ;
95
104
96
105
var credentials = RED . nodes . getCredentials ( n . id ) ;
97
106
if ( credentials ) {
98
107
this . username = credentials . username ;
99
108
this . password = credentials . password ;
100
109
}
101
-
102
- var parsedUrl = url . parse ( this . hostname ) ;
103
- var authUrl = parsedUrl . protocol + '//' ;
104
-
105
- if ( this . username && this . password ) {
106
- authUrl += this . username + ":" + encodeURIComponent ( this . password ) + "@" ;
107
- }
108
- authUrl += parsedUrl . hostname ;
109
-
110
- this . url = authUrl ;
111
110
}
112
111
RED . nodes . registerType ( "cloudant" , CloudantNode ) ;
113
112
@@ -121,12 +120,13 @@ module.exports = function(RED) {
121
120
122
121
var node = this ;
123
122
var credentials = {
124
- account : node . cloudantConfig . credentials . username ,
125
- password : node . cloudantConfig . credentials . password
123
+ account : node . cloudantConfig . account ,
124
+ key : node . cloudantConfig . username ,
125
+ password : node . cloudantConfig . password
126
126
} ;
127
127
128
128
Cloudant ( credentials , function ( err , cloudant ) {
129
- if ( err ) { node . error ( err ) ; }
129
+ if ( err ) { node . error ( err . description , err ) ; }
130
130
else {
131
131
// check if the database exists and create it if it doesn't
132
132
createDatabase ( cloudant , node ) ;
@@ -139,10 +139,24 @@ module.exports = function(RED) {
139
139
140
140
function createDatabase ( cloudant , node ) {
141
141
cloudant . db . list ( function ( err , all_dbs ) {
142
- if ( err ) { node . error ( err ) ; }
142
+ if ( err ) {
143
+ if ( err . error !== 'forbidden' ) {
144
+ // if err.error is 'forbidden' then we are using an api
145
+ // key, so we can assume the database already exists
146
+ return ;
147
+ }
148
+ node . error ( "Failed to list databases: " + err . description , err ) ;
149
+ }
143
150
else {
144
151
if ( all_dbs && all_dbs . indexOf ( node . database ) < 0 ) {
145
- cloudant . db . create ( node . database ) ;
152
+ cloudant . db . create ( node . database , function ( err , body ) {
153
+ if ( err ) {
154
+ node . error (
155
+ "Failed to create database: " + err . description ,
156
+ err
157
+ ) ;
158
+ }
159
+ } ) ;
146
160
}
147
161
}
148
162
} ) ;
@@ -155,7 +169,12 @@ module.exports = function(RED) {
155
169
var doc = parseMessage ( msg , root ) ;
156
170
157
171
insertDocument ( cloudant , node , doc , MAX_ATTEMPTS , function ( err , body ) {
158
- if ( err ) { node . error ( err ) ; }
172
+ if ( err ) {
173
+ node . error (
174
+ "Failed to insert document: " + err . description ,
175
+ err
176
+ ) ;
177
+ }
159
178
} ) ;
160
179
}
161
180
else if ( node . operation === "delete" ) {
@@ -164,10 +183,16 @@ module.exports = function(RED) {
164
183
if ( "_rev" in doc && "_id" in doc ) {
165
184
var db = cloudant . use ( node . database ) ;
166
185
db . destroy ( doc . _id , doc . _rev , function ( err , body ) {
167
- if ( err ) { node . error ( err ) ; }
186
+ if ( err ) {
187
+ node . error (
188
+ "Failed to delete document: " + err . description ,
189
+ err
190
+ ) ;
191
+ }
168
192
} ) ;
169
193
} else {
170
- node . error ( "_rev and _id are required to delete a document" ) ;
194
+ var err = new Error ( "_id and _rev are required to delete a document" ) ;
195
+ node . error ( err . message , err ) ;
171
196
}
172
197
}
173
198
}
@@ -249,12 +274,13 @@ module.exports = function(RED) {
249
274
250
275
var node = this ;
251
276
var credentials = {
252
- account : node . cloudantConfig . credentials . username ,
253
- password : node . cloudantConfig . credentials . password
277
+ account : node . cloudantConfig . account ,
278
+ key : node . cloudantConfig . username ,
279
+ password : node . cloudantConfig . password
254
280
} ;
255
281
256
282
Cloudant ( credentials , function ( err , cloudant ) {
257
- if ( err ) { node . error ( err ) ; }
283
+ if ( err ) { node . error ( err . description , err ) ; }
258
284
else {
259
285
node . on ( "input" , function ( msg ) {
260
286
var db = cloudant . use ( node . database ) ;
@@ -339,10 +365,13 @@ module.exports = function(RED) {
339
365
msg . payload = null ;
340
366
341
367
if ( err . description === "missing" ) {
342
- node . warn ( "Document '" + node . inputId + "' not found in database '" +
343
- node . database + "'." ) ;
368
+ node . warn (
369
+ "Document '" + node . inputId +
370
+ "' not found in database '" + node . database + "'." ,
371
+ err
372
+ ) ;
344
373
} else {
345
- node . error ( err . reason ) ;
374
+ node . error ( err . description , err ) ;
346
375
}
347
376
}
348
377
@@ -351,11 +380,23 @@ module.exports = function(RED) {
351
380
}
352
381
RED . nodes . registerType ( "cloudant in" , CloudantInNode ) ;
353
382
383
+ // must return an object with, at least, values for account, username and
384
+ // password for the Cloudant service at the top-level of the object
354
385
function _getCloudantConfig ( n ) {
355
386
if ( n . service === "_ext_" ) {
356
387
return RED . nodes . getNode ( n . cloudant ) ;
388
+
357
389
} else if ( n . service !== "" ) {
358
- return appEnv . getService ( n . service ) ;
390
+ var service = appEnv . getService ( n . service ) ;
391
+ var cloudantConfig = { } ;
392
+
393
+ var host = service . credentials . host ;
394
+
395
+ cloudantConfig . username = service . credentials . username ;
396
+ cloudantConfig . password = service . credentials . password ;
397
+ cloudantConfig . account = host . substring ( 0 , host . indexOf ( '.' ) ) ;
398
+
399
+ return cloudantConfig ;
359
400
}
360
401
}
361
402
0 commit comments