mcollina / mqemitter-mongodb

MongoDB based MQEmitter
MIT License
18 stars 10 forks source link

Database name bug on using clustered uri #13

Closed bro-jo closed 4 years ago

bro-jo commented 4 years ago

I am using mongodb cluster with mqemitter-mongodb. But, I notice that there is some bug about database names.

The error log is like this:

/??/??/??/??/??/node_modules/mongodb/lib/topologies/replset.js:346
          throw err;
          ^

MongoError: database names cannot contain the character '.'
    at Function.create (/??/??/??/??/??/node_modules/mongodb/lib/core/error.js:44:12)
    at validateDatabaseName (/??/??/??/??/??/node_modules/mongodb/lib/operations/db_ops.js:557:24)
    at new Db (/??/??/??/??/??/node_modules/mongodb/lib/db.js:190:3)
    at MongoClient.db (/??/??/??/??/??/node_modules/mongodb/lib/mongo_client.js:275:14)
    at /??/??/??/??/??/config/mqemitter-mongodb.js:46:25
    at result (/??/??/??/??/??/node_modules/mongodb/lib/operations/execute_operation.js:75:17)
    at executeCallback (/??/??/??/??/??/node_modules/mongodb/lib/operations/execute_operation.js:68:9)
    at err (/??/??/??/??/??/node_modules/mongodb/lib/operations/connect.js:193:7)
    at connectCallback (/??/??/??/??/??/node_modules/mongodb/lib/operations/connect.js:343:5)
    at topology.connect.err (/??/??/??/??/??/node_modules/mongodb/lib/operations/connect.js:475:7)

The config of mine is like this:

const mongoPersistence = aedesPersistenceMongoDB({
  url: mongoUri,
  database: 'aedes',
  ttl: {
    packets: 300,
    subscriptions: 300,
  },
});

But, the library doesn't have database option. The library get database name from uri by using url parser, and it produces inadequate results. When I put uri for mongodb cluster such as mongodb://<user>:<pw>@<server1>:27017,<server2>:27017,<server3>/aedes?ssl=true&replicaSet=<someReplicaSet>&retryWrites=true&w=majority, then the url parser which is urlModule.parse produces not aedes but :27017,<server2>:27017,<server3>/aedes.

I think that there is 2 solution. The first one is making database option for MQEmitterMongoDB fuction which let a developer set custom database name. The second solution is improve uri parsing logic.

bro-jo commented 4 years ago

I will send pull request soon.

mcollina commented 4 years ago

I think a better approach could be to just passing down to MongoDB all the options as is. A PR would be highly welcomed, thanks!