node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

db options does not fire select db on createClient when url (with specified db number) is provided

Open darkash opened this issue 5 years ago • 3 comments

  • Version: 2.8.0
  • Platform: Node JS 12.8.0 on MacOS Mojave 10.14.5
  • Description: as the title said, I expect when the db option is provided, it will fire select command on connect using the db option provided regardless whether url provides db number or not.

How to reproduce:

const redis = require('redis')
const url = 'redis://127.0.0.1:6379/0'
const client = redis.createClient({db: 3, url: url})
client.selected_db // this will return 0, instead of 3 which I expect
client.set('foo', 'bar') // upon checking on redis-cli this is written to db 0, not on db 3

darkash avatar Nov 28 '19 09:11 darkash

Good catch. In reviewing createClient, the url takes precedence over any other options that can be specified inside the URL. The options object will be evaluated first, then the url inside that object - so specifying db of 3 would be accepted, then immediately overwritten by what is in the URL. It makes sense and would be breaking change to do otherwise.

However, I see your point in how this is confusing. We should document this behaviour and possibly throw a warning (maybe an actual error) for any option that is being overwritten by the URL.

stockholmux avatar Nov 28 '19 14:11 stockholmux

It is indeed a good idea to throw an error in such case / to at least log something for now.

The main issue is that it applies to multiple options and not only the db option.

BridgeAR avatar Feb 05 '20 09:02 BridgeAR

Have there been any updates to this issue?

PopBot avatar Aug 06 '22 21:08 PopBot