Giter Site home page Giter Site logo

Comments (8)

yurijmikhalevich avatar yurijmikhalevich commented on September 23, 2024

How do you initialise winston-mongodb with preconnected db object. Could you paste a code snippet?
I added a test for preconnected db object here https://github.com/winstonjs/winston-mongodb/blob/master/test/winston-mongodb-preconnected-test.js.

from winston-mongodb.

dankahle avatar dankahle commented on September 23, 2024

for node native driver it's MongoClient.connect:
http://mongodb.github.io/node-mongodb-native/2.0/api/MongoClient.html#.connect

and for mongoose it's mongoose.createConnection:
http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection

both are async, the former with a callback, the latter under the covers as mongoose uses the mongo native node driver as well. The mongoose connection returned has a "mongo" property that represents the native driver (what this lib expects for a "existing db connection"). The mongoose.Connection.mongo property initially has a readyState of 2 (connecting) which switches to 1 (connected).

Winston will cache it's requests if opts.db is a string, but not if it isn't. So, it appears to me that everyone using the native driver will have to cache their database (db) requests until the driver's ready. Seems like a hassle, when all you need is the clients to take a promise and simply wait for the database to be ready. Winston doesn't wait, so either I have to figure out how to cache the db requests or simply pass in the mongo conn string, in which case winston will wait, for its own db connection.

The solution is simple enough, if winston allowed opts.db to be a: string, db, AND promise to a db, then we'd use code like:
https://github.com/winstonjs/winston-mongodb/blob/master/lib/winston-mongodb.js#L123

if ('string' === typeof this.db) {
mongodb.MongoClient.connect(this.db, this.options, function(err, db) {
if (err) {
console.error('winston-mongodb: error initialising logger', err);
return;
}
self.mainDb = db;
authorizeDb(function() {
createCollection(processOpQuery);
});
});
} else {
Q(this.db).then(function(db) {
self.mainDb = db;
authorizeDb(function() {
createCollection(processOpQuery);
});
}
}
};

If they pass in a db, fine, the wrapped promise is instantly resolved then, but if they pass in a promise to a db, we wait and cache the log requests until the promise is resolved. Thing is: winston has already implemented the queing code, so just a couple lines of code and it's all done.

from winston-mongodb.

yurijmikhalevich avatar yurijmikhalevich commented on September 23, 2024

I see what you want. But, as you can see in the test sample, already connected db object should be passed to winston-mongodb. So, you should wait for callback (or promise) in your database initialization code and init winston-mongodb after the connection to database was established.

from winston-mongodb.

dankahle avatar dankahle commented on September 23, 2024

All you modules need a logger, so makes sense to require() it, it's that or make it global, in either case how can it be available to them if it's waiting on the db connection? Say in their init code, they want to log something? How could that be done if you were busy waiting on the database connection? If I'm missing something basic, please enlighten me.

from winston-mongodb.

yurijmikhalevich avatar yurijmikhalevich commented on September 23, 2024

You should init logger somewhere. Init it first (in the beginning in your initialisation code), than connect to db and init your winston-mongodb logger. After you can do anything else, init your application, load other modules and so on. Doesn't this seems legit?

from winston-mongodb.

dankahle avatar dankahle commented on September 23, 2024

Oh. I've only seen modules with a list of requires at the top. This had just come to me: the requires can be anywhere, so your main module requires and code could live inside some init callback. Interesting.

from winston-mongodb.

yurijmikhalevich avatar yurijmikhalevich commented on September 23, 2024

Yes. But this isn't required to achieve behavior I described. There are different approaches. You may have some modules init methods. You may actually launch your application in your init callback, so any logic will be performed strictly after init. If you use only one logger in your application, you may simply require('winston') anywhere you want, and update it with new transports in your initialisation code.

from winston-mongodb.

dankahle avatar dankahle commented on September 23, 2024

As long as winston isn't busy waiting on a database connection (modules simply requiring winston to log). I just did a test with the main app code inside a init callback, kinda ugly but then... didn't work. Turns out there's a bug in this module. I'll create another issue for that. I still think it would be terribly easy to allow db promises, as all the queueing code has already been done. So easy in fact, that I can just fork and do it in my own version, all of 6 lines of code or so. In my case I only wait for the mongoose or the logger, mongoose queues up db calls, and I can hack wintson-mongodb to do the same with the logging calls, them simply require both. Or... just pass in the connection string and be done with it.

from winston-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.