Skip to content

Feature: Option to disable creation of initial connection #12965

Closed

Description

@titanism

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

If you manage multiple custom connections, a typical pattern is to simply create new connections with createConnection(uri, options). Doing so, it adds by default to the mongoose singleton of mongoose.connections, so you can iterate over mongoose.connections to see properties such as readyState.

The default connection however is always present. The only way to delete it (regardless of custom Mongoose instances) is to manually call mongoose.connection.destroy();. This isn't such a good pattern, as a better option might be new Mongoose({ createInitialConnection: false }); or something. Adding this feature would require some refactoring though.

Activity

  1. added
    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature
    new featureThis change adds new functionality, like a new method or class
    on Jan 31, 2023
  2. titanism commented on Jan 31, 2023

    @titanism
    Author

    This might not actually be too hard, but we could be wrong:

      // default global options
      this.options = Object.assign({
        pluralization: true,
        autoIndex: true,
    -    autoCreate: true
    +    autoCreate: true,
    +    createInitialConnection: false
      }, options);
    +
    +  if (this.options.createInitialConnection) {
    +    const conn = this.createConnection(); // default connection
    +    conn.models = this.models;
    +  }
    
    -  const conn = this.createConnection(); // default connection
    -  conn.models = this.models;
  3. titanism commented on Jan 31, 2023

    @titanism
    Author

    There might also be another bug here we discovered, whereas id is not unique.

    if (typeof base === 'undefined' || !base.connections.length) {
    this.id = 0;
    } else {
    this.id = base.connections.length;
    }

    If you create multiple connections, then call .destroy() on them, and then create more connections, multiple of the connections will have the same id property because .length does not check for uniqueness.

    cc @vkarpov15

  4. titanism commented on Jan 31, 2023

    @titanism
    Author

    Confirmed this is a bug!

    const mongoose = require('mongoose');
    mongoose.createConnection()
    mongoose.createConnection()
    mongoose.connections[0].destroy()
    mongoose.createConnection()
    mongoose.createConnection()
    mongoose.createConnection()
    mongoose.connections.map(c => c.id) // outputs: [ 1, 2, 3, 3, 4 ]

    You can see 3 appears twice.

  5. added this to the 6.9.2 milestone on Feb 10, 2023
  6. added a commit that references this issue on Feb 11, 2023
    6e5176a
  7. added a commit that references this issue on Feb 13, 2023
  8. modified the milestones: 6.9.2, 6.9.4, 6.10.0 on Feb 16, 2023
  9. added a commit that references this issue on Feb 22, 2023
    c3c133b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featurenew featureThis change adds new functionality, like a new method or class

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions