@@ -63,7 +63,7 @@ var MongoDB = exports.MongoDB = function(options) {
63
63
}
64
64
} ;
65
65
}
66
- this . collection = ( options . collection || 'logs ' ) ;
66
+ this . collection = ( options . collection || 'log ' ) ;
67
67
this . level = ( options . level || 'info' ) ;
68
68
this . silent = options . silent ;
69
69
this . username = options . username ;
@@ -77,47 +77,55 @@ var MongoDB = exports.MongoDB = function(options) {
77
77
this . hostname = os . hostname ( ) ;
78
78
}
79
79
80
-
81
- this . _opQuery = [ ] ;
80
+ this . _opQueue = [ ] ;
82
81
83
82
var self = this ;
84
83
85
- function processOpQuery ( ) {
86
- self . _opQuery . forEach ( function ( operation ) {
84
+ function setupDatabaseAndEmptyQueue ( db ) {
85
+ authorizeDb ( db , function ( err , db ) {
86
+ createCollection ( db , function ( err , db ) {
87
+ self . logDb = db ;
88
+ processOpQueue ( ) ;
89
+ } ) ;
90
+ } ) ;
91
+ }
92
+
93
+ function processOpQueue ( ) {
94
+ self . _opQueue . forEach ( function ( operation ) {
87
95
self [ operation . method ] . apply ( self , operation . args ) ;
88
96
} ) ;
89
- delete self . _opQuery ;
97
+ delete self . _opQueue ;
90
98
}
91
99
92
- function createCollection ( cb ) {
100
+ function createCollection ( db , cb ) {
93
101
var opts = { } ;
94
102
if ( self . capped ) {
95
103
opts = { capped : true , size : self . cappedSize } ;
96
104
}
97
- self . mainDb . createCollection ( self . collection , opts , function ( ) {
98
- cb ( null ) ;
105
+ db . createCollection ( self . collection , opts , function ( ) {
106
+ cb ( null , db ) ;
99
107
} ) ;
100
108
}
101
109
102
- function authorizeDb ( cb ) {
110
+ function authorizeDb ( db , cb ) {
103
111
if ( self . username && self . password ) {
104
- self . mainDb . authenticate ( self . username , self . password ,
112
+ db . authenticate ( self . username , self . password ,
105
113
function ( err , result ) {
106
114
if ( err ) {
107
115
console . error ( 'winston-mongodb: error initialising logger' , err ) ;
108
- self . mainDb . close ( ) ;
116
+ db . close ( ) ;
109
117
return ;
110
118
}
111
119
if ( ! result ) {
112
120
console . error ( 'winston-mongodb: invalid username or password' ) ;
113
- self . mainDb . close ( ) ;
121
+ db . close ( ) ;
114
122
return ;
115
123
}
116
- cb ( null ) ;
124
+ cb ( null , db ) ;
117
125
}
118
126
) ;
119
127
}
120
- cb ( null ) ;
128
+ cb ( null , db ) ;
121
129
}
122
130
123
131
if ( 'string' === typeof this . db ) {
@@ -126,16 +134,17 @@ var MongoDB = exports.MongoDB = function(options) {
126
134
console . error ( 'winston-mongodb: error initialising logger' , err ) ;
127
135
return ;
128
136
}
129
- self . mainDb = db ;
130
- authorizeDb ( function ( ) {
131
- createCollection ( processOpQuery ) ;
132
- } ) ;
137
+ setupDatabaseAndEmptyQueue ( db ) ;
133
138
} ) ;
134
- } else {
135
- this . mainDb = this . db ;
136
- authorizeDb ( function ( ) {
137
- createCollection ( processOpQuery ) ;
139
+ } else if ( 'function' === typeof this . db . then ) {
140
+ this . db . then ( function ( db ) {
141
+ setupDatabaseAndEmptyQueue ( db ) ;
142
+ } , function ( err ) {
143
+ console . error (
144
+ 'winston-mongodb: error initialising logger from promise' , err ) ;
138
145
} ) ;
146
+ } else {
147
+ setupDatabaseAndEmptyQueue ( this . db ) ;
139
148
}
140
149
} ;
141
150
@@ -161,8 +170,8 @@ winston.transports.MongoDB = MongoDB;
161
170
* @param {Function } callback Continuation to respond to when complete.
162
171
*/
163
172
MongoDB . prototype . log = function ( level , msg , opt_meta , callback ) {
164
- if ( ! this . mainDb ) {
165
- this . _opQuery . push ( {
173
+ if ( ! this . logDb ) {
174
+ this . _opQueue . push ( {
166
175
method : 'log' ,
167
176
args : arguments
168
177
} ) ;
@@ -186,7 +195,7 @@ MongoDB.prototype.log = function(level, msg, opt_meta, callback) {
186
195
callback ( err , null ) ;
187
196
}
188
197
189
- self . mainDb . collection ( self . collection , function ( err , col ) {
198
+ self . logDb . collection ( self . collection , function ( err , col ) {
190
199
if ( err ) {
191
200
onError ( err ) ;
192
201
return ;
@@ -225,8 +234,8 @@ MongoDB.prototype.log = function(level, msg, opt_meta, callback) {
225
234
* @return {* }
226
235
*/
227
236
MongoDB . prototype . query = function ( opt_options , callback ) {
228
- if ( ! this . mainDb ) {
229
- this . _opQuery . push ( {
237
+ if ( ! this . logDb ) {
238
+ this . _opQueue . push ( {
230
239
method : 'query' ,
231
240
args : arguments
232
241
} ) ;
@@ -257,7 +266,7 @@ MongoDB.prototype.query = function(opt_options, callback) {
257
266
opt . fields = options . fields ;
258
267
}
259
268
260
- this . mainDb . collection ( this . collection , function ( err , col ) {
269
+ this . logDb . collection ( this . collection , function ( err , col ) {
261
270
if ( err ) {
262
271
callback ( err ) ;
263
272
return ;
@@ -293,8 +302,8 @@ MongoDB.prototype.stream = function(options, stream) {
293
302
var self = this ;
294
303
var start = options . start ;
295
304
296
- if ( ! this . mainDb ) {
297
- this . _opQuery . push ( {
305
+ if ( ! this . logDb ) {
306
+ this . _opQueue . push ( {
298
307
method : 'stream' ,
299
308
args : [ options , stream ]
300
309
} ) ;
@@ -310,7 +319,7 @@ MongoDB.prototype.stream = function(options, stream) {
310
319
}
311
320
312
321
if ( start != null ) {
313
- this . mainDb . collection ( this . collection , function ( err , col ) {
322
+ this . logDb . collection ( this . collection , function ( err , col ) {
314
323
if ( err ) {
315
324
stream . emit ( 'error' , err ) ;
316
325
return ;
@@ -334,7 +343,7 @@ MongoDB.prototype.stream = function(options, stream) {
334
343
return stream ;
335
344
}
336
345
337
- this . mainDb . collection ( this . collection , function ( err , col ) {
346
+ this . logDb . collection ( this . collection , function ( err , col ) {
338
347
if ( err ) {
339
348
stream . emit ( 'error' , err ) ;
340
349
return ;
@@ -387,8 +396,8 @@ MongoDB.prototype.streamPoll = function(options, stream) {
387
396
var start = options . start ;
388
397
var last ;
389
398
390
- if ( ! this . mainDb ) {
391
- this . _opQuery . push ( {
399
+ if ( ! this . logDb ) {
400
+ this . _opQueue . push ( {
392
401
method : 'streamPoll' ,
393
402
args : [ options , stream ]
394
403
} ) ;
@@ -408,7 +417,7 @@ MongoDB.prototype.streamPoll = function(options, stream) {
408
417
} ;
409
418
410
419
( function check ( ) {
411
- self . mainDb . collection ( self . collection , function ( err , col ) {
420
+ self . logDb . collection ( self . collection , function ( err , col ) {
412
421
if ( err ) {
413
422
stream . emit ( 'error' , err ) ;
414
423
return ;
0 commit comments