Open ctrlsam opened 2 years ago
+1
The appearance of the warning indicates use of a deprecated function; is this request to just remove the warning as stated or to remove the deprecation?
For history, the warning was added in this commit by @yurijmikhalevich on Mar 13, 2019, nominally to fix an issue around incompatibility with an updated MongoDB driver.
I'm not an expert in this topic but am adding a response assignment to someone who knows more on this topic than I; please feel free to decline if need be!
I think a lot of people use the "depreciated" preconnected object. It would be good if this was considered to no longer be depreciated, and remove the warning
We're missing key info here. Like @ctrlsam asked, why is it being deprecated? Also, what is the now-recommended way of creating and connecting to a MongoDB transporter?
Edit: I found out that I can simply pass the mongo connection URI to the options and everything works. This is good enough for me.
Will using a connection string not needlessly establish a new connection?
EDIT: If I'm not mistaken it seems as if a new connection is always created no matter what you pass as the db
option.
function connectToDatabase(logger) {
return mongodb.MongoClient.connect(logger.db, logger.options
).then(client=>{
logger.mongoClient = client;
setupDatabaseAndEmptyQueue(client.db());
}, err=>{
console.error('winston-mongodb: error initialising logger', err);
if (options.tryReconnect) {
console.log('winston-mongodb: will try reconnecting in 10 seconds');
return new Promise(resolve=>setTimeout(resolve, 10000)
).then(()=>connectToDatabase(logger));
}
});
}
Though, passing a db object seems to work just fine with versions below.
mongodb 3.7.3
winston 3.3.5
winston-mongodb 5.0.7
Then I will opt for using a connection uri just to rid of the deprecation warning.
The benefit I can see with using a pre-connected object is that it prevents opening another connection to the DB if the pre-connected instance is already in use. It works fine, however there is a warning generated.
I am also facing an issue whereby too many mongodb connection is being created after upgrading to v5 and using the mongodb connection URI. Previously on older version i was using a pre-connected mongodb connection obj and this did not happen.
Can i get some advice on how should i do this instead? Or i probably will need to downgrade to use v4 instead.
I am using:
Thanks!
Just to provide an update.
I have created a brand new nodejs project from scratch using the latest versions (listed below) and i am still getting an error "unhandledRejection TypeError: client.db is not a function" when i use a pre-connected mongodb client obj.
Versions: NodeJS - 18.7 MongoDB - 4.11.0 Winston - 3.8.2 Winston-Mongodb - 5.1.0
This is my code snippets.
const winston = require('winston'); const { MongoClient } = require('mongodb'); require('winston-mongodb');
const MongoDbLogClient = async () => { const mongoClient = await MongoClient.connect('mongodb://localhost:27017/logs'); const mongodb = await mongoClient.db(); return mongodb; }; const mongodb = MongoDbLogClient();
const logger = winston.createLogger({ transports: [ // MongoDB transport new winston.transports.MongoDB({ level: 'info', db: mongodb, collection: 'server_logs' })] });
logger.info('Info log');
tengo el mismo problema el error que me sale es
[1] error: WORKER unhandledRejection [1] error: uncaughtException: db.close is not a function [1] TypeError: db.close is not a function [1] at /app/node_modules/winston-mongodb/lib/winston-mongodb.js:87:10 [1] at processTicksAndRejections (internal/process/task_queues.js:95:5)
winston-mongodb: preconnected object support is deprecated and will be removed in v5
Using a preconnected DB object is useful when you're already using MongoDB in your program. I've searched around and cannot find a reason for this feature to be deprecated,
nodejs
is now far past v5 too.If there is a reason to keep this deprecation, I'd like to know why, and otherwise the warning should be removed.