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

Add config option to not connect in the constructor #350

Closed
shaharmor opened this issue Jul 17, 2016 · 6 comments · Fixed by #352
Closed

Add config option to not connect in the constructor #350

shaharmor opened this issue Jul 17, 2016 · 6 comments · Fixed by #352
Labels

Comments

@shaharmor
Copy link
Collaborator

Hi,

Can we add a config option to the constructor so that it will not try to connect until manually calling .connect()?

@luin
Copy link
Collaborator

luin commented Jul 18, 2016

Seems lazyConnect option should do the trick.

@shaharmor
Copy link
Collaborator Author

Does it work in Cluster mode as well? I want to do something like this:

var redis = new Redis.Cluster();

// then sometime later
redis.connect().then(() => ...)

@luin
Copy link
Collaborator

luin commented Jul 18, 2016

hmm...It doesn't work in cluster mode. What's your use case?

@shaharmor
Copy link
Collaborator Author

I just want to have the redis instance created in the constructor of my own class, and then make sure it connects before using it.

We can just wrap this line: https://github.com/luin/ioredis/blob/master/lib/cluster/index.js#L83
in an if statement with lazyConnect no?

@luin
Copy link
Collaborator

luin commented Jul 18, 2016

Yeah, should be pretty similar to the standalone version: https://github.com/luin/ioredis/blob/master/lib/redis.js#L126-L131

@Jokero
Copy link

Jokero commented Jul 15, 2017

For me it also looks very strange that instantiation does some connections.
I like how it did in mongoose. You need to connect explicitly:

mongoose.connect('mongodb://localhost/myapp');
var MyModel = mongoose.model('Test', new Schema({ name: String }));
// Works
MyModel.findOne(function(error, result) { /* ... */ });

Yes, I can use lazyConnect option, but anyway it looks like antipattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants