mongoose-history-trace icon indicating copy to clipboard operation
mongoose-history-trace copied to clipboard

Library not work without connectionUri

Open jcpalacios opened this issue 3 years ago • 7 comments

I am trying to use this library with the connection that is already created in my project, but it doesn't work.

MongooseError: Operation `historyLogs.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (***/node_modules/mongoose-history-trace/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:197:23)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

If I use the connectionUri parameter, it generates more than 1600 connections against the database and ends up blocking it. Do you have any advice on how to solve this?

Thanks

jcpalacios avatar Sep 22 '21 19:09 jcpalacios

Could you give me more example of how your connection was started and how it's calling the lib so I can set up the scenarios for testing? I'm working on a new feature so that it accepts a connection passed via parameter.

WelingtonMonteiro avatar Oct 05 '21 20:10 WelingtonMonteiro

Start connection app.js

const mongoose = require('mongoose')

const oneMinuteInMilliSeconds = 1000 * 60
const halfAMinuteInMilliSeconds = oneMinuteInMilliSeconds / 2
const fiveMinutesInMilliSeconds = oneMinuteInMilliSeconds * 5

mongoose.connect(process.env.DB_URL, {
      useNewUrlParser: true,
      useFindAndModify: false,
      useUnifiedTopology: true,
      useCreateIndex: true,
      keepAlive: fiveMinutesInMilliSeconds,
      connectTimeoutMS: halfAMinuteInMilliSeconds,
      socketTimeoutMS: oneMinuteInMilliSeconds,
      poolSize: 5,
      maxTimeMS: halfAMinuteInMilliSeconds
    }, (err) => {
    // ...start application
    })

Set lib in ExampleModel.js:

const Schema = mongoose.Schema
const ExampleModel = new Shema({
    example: {type: String}
}, {toJSON: {virtuals: true}, toObject: {virtuals: true}})
const options = {
  userPaths: ['_id', 'email', 'date', 'trace']
}
ExampleModel.plugin(mongooseHistory, options)
module.exports = mongoose.model('Example', ExampleModel)

And example use:

  const example = await ExampleModel.findOne({_id: id})
  example.example = '2'
  ExampleModel.addLoggedUser({_id: id, email: 'exampleEmail', date: new Date(), trace: 'traceExample'})
  await example.save()

jcpalacios avatar Oct 06 '21 08:10 jcpalacios

Did you manage to find any fix for this? I can't make the library work for this issue.

rofe-dl avatar Oct 31 '22 11:10 rofe-dl

Same issue for me. This library seems to be more actively maintained than any other mongoose history so it would be aweomesauce if this issue could be resolved. I'm connecting in a similar way as @jcpalacios -- otherwise @jcpalacios did you end up finding another library / solution?

Peege151 avatar Nov 10 '22 08:11 Peege151

Finally i'm used mongoose-patch-history but they have not closed version for 2 years.

jcpalacios avatar Nov 10 '22 10:11 jcpalacios

I'm analyzing these bugs, because it happens in some cases, I have a version on my site, I haven't published it yet, apparently with the new mongoose update, there was a problem with the scope of variables, and today it's breaking in the new versions, but I'm fixing some punctual bugs. If anyone has a solution and wants to upload a Pull Request, I will analyze it.

WelingtonMonteiro avatar Nov 10 '22 17:11 WelingtonMonteiro

In the new versions every connection created kept a structure and connection of mongoose, in the new versions, for some reason, there is no longer the connection object, so I am no longer able to automatically get the connection, the solution I currently have is to pass the connection from explicitly, in the plugin, so I will always have the connection created by the user.

WelingtonMonteiro avatar Nov 10 '22 17:11 WelingtonMonteiro