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

Proposal to allow updating client connection #485

Open
seanwarmanshine opened this issue Nov 13, 2024 · 0 comments
Open

Proposal to allow updating client connection #485

seanwarmanshine opened this issue Nov 13, 2024 · 0 comments

Comments

@seanwarmanshine
Copy link

seanwarmanshine commented Nov 13, 2024

  • I'm submitting a ...
    [ ] bug report
    [ X ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    Client connections are currently a one-time operation that has it's set up code inside the MongoStore constructor. I like to have a method that allows developers to swap out the client connection after MongoStore has been instantiated.

This feature would address issues where a MongoDB connection is closed, which currently crashes connect-mongo as it's not able to swap out the original disconnected client. It could also be useful for other circumstances.

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

I've built a fix for this that basically just moves most of the constructor logic into a method called connectClient and calls that method from the constructor instead:

class MongoStore extends session.Store {
    constructor({ collectionName = 'sessions', ttl = 1209600, mongoOptions = {}, autoRemove = 'native', autoRemoveInterval = 10, touchAfter = 0, stringify = true, crypto, ...required }) {
        super();
        this.crypto = null;
        debug('create MongoStore instance');
        this.connectClient({ collectionName, ttl, mongoOptions, autoRemove, autoRemoveInterval, touchAfter, stringify, crypto, ...required });
    }
    connectClient({ collectionName = 'sessions', ttl = 1209600, mongoOptions = {}, autoRemove = 'native', autoRemoveInterval = 10, touchAfter = 0, stringify = true, crypto, ...required }) {
        const options = {
            collectionName,
            ttl,
            mongoOptions,
            autoRemove,
            autoRemoveInterval,
            // Connection logic etc...
    }
    // Rest of class logic...
}

This allows the developer to swap out a client whenever they want:

const store = MongoStore.create({
  client: mongoose.connection.getClient(),
});

// Later at any time...
store.connectClient({ client: connection.getClient() });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant