4

I'm writing a Node app that accepts TCP connections. I have a few separate Node modules (utilizing the exports functionality). Each needs a database connection to handle some of their functionality.

What's the best way to architect/handle database connections? Should each module create a separate connection or should my main JS file pass in a database connection handler to the modules when I require them?

2
  • 1
    The question is too hard to answer without more specific details, however feel free to visit the node.js chat room for more detailed discussion Commented Jul 4, 2011 at 20:33
  • I agree my question is very general, but this type of architecture is probably common in Node.js apps. Most will require() many installed and custom modules, I assume someone has tried either way before and has experience in the flexibility and robustness of each method. Commented Jul 5, 2011 at 19:30

1 Answer 1

6

imo best approach is to initialize connections pool and use it in modules passing as parameter. mysql-pool is quite generic and easily adaptable to any db client

upd: node-pool is a generic db-agnostic pool

Sign up to request clarification or add additional context in comments.

3 Comments

Actually I think using github.com/springmeyer/node-pool is better, since it's more general than just using a mysql pool (you can use it with all kind of dbs).
I agree, I've started rewriting my code this way and it required very few changes. Allows for the modules to ignore database connection configuration, which IMO is a good thing.
springmeyer/node-pool seems to 404, but I wrote a node.js pool library that is available from NPM and has gotten a fair amount of use: github.com/coopernurse/node-pool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.