neumino / rethinkdbdash

An advanced Node.js driver for RethinkDB with a connection pool, support for streams etc.
MIT License
848 stars 109 forks source link

Question about good practices #360

Closed atif089 closed 6 years ago

atif089 commented 6 years ago

Is it a good practice to require a DB driver directly on any module that I want to use or should I create my own module with a singleton connection and keep passing it around.

For example, let's say I have express routes defined in 5 different files. If I run

var r = require('rethinkdbdash')(config.db);

in every file does it mean it will be creating a new pool of connections in each file or does it internally reuse the same connections.

aleclarson commented 6 years ago

You should only execute the exported function once per cluster. The rethinkdbdash instance creates a connection pool for each server in the cluster.

https://github.com/neumino/rethinkdbdash/blob/master/lib/index.js#L556 https://github.com/neumino/rethinkdbdash/blob/master/lib/index.js#L88

If you're only using a single cluster, feel free to set global.r instead of importing a singleton module every time you need to do a query.

neumino commented 6 years ago

It's better to share the same instance. You want all your modules to share the same pool - it's a bit more stable