WelingtonMonteiro / mongoose-history-trace

Plugin mongoose for history logs schemas
MIT License
20 stars 7 forks source link

Library not work without connectionUri #7

Open jcpalacios opened 3 years ago

jcpalacios commented 3 years ago

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

WelingtonMonteiro commented 3 years ago

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.

jcpalacios commented 3 years ago

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()
rofe-dl commented 2 years ago

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

Peege151 commented 1 year ago

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?

jcpalacios commented 1 year ago

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

WelingtonMonteiro commented 1 year ago

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 commented 1 year ago

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.