Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop deprecation of pre-connected DB object #194

Open
ctrlsam opened this issue Dec 7, 2021 · 9 comments
Open

Stop deprecation of pre-connected DB object #194

ctrlsam opened this issue Dec 7, 2021 · 9 comments
Assignees
Labels

Comments

@ctrlsam
Copy link

ctrlsam commented Dec 7, 2021

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.

@waynehaffenden
Copy link

+1

@wbt
Copy link

wbt commented Jan 27, 2022

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!

@ctrlsam
Copy link
Author

ctrlsam commented Jan 27, 2022

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

@ContraHacker
Copy link

ContraHacker commented Feb 25, 2022

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.

@hampusolsen
Copy link

hampusolsen commented Sep 8, 2022

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.

winston-mongodb.js#L134

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.

@ctrlsam
Copy link
Author

ctrlsam commented Sep 8, 2022

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.

@zhifeng-heng
Copy link

zhifeng-heng commented Oct 26, 2022

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:

  • Node 18.7
  • Mongodb 4.9.0
  • Winston 3.8.1
  • Winston-Mongodb 5.0.7

Thanks!

@zhifeng-heng
Copy link

zhifeng-heng commented Oct 27, 2022

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');

@Cristian48
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants