db-query-assistant
Summary
Provides the following features on top of supported node.js database connection modules:
- Configurable connection pooling.
- Issue multiple simultaneous queries, and get all results back in a callback when the last query completes.
- Issue queries in series, getting the results for each previous query back before executing the next one.
- Transaction support with auto-rollback on query error.
Drivers exist for the following modules:
Writing new drivers is fairly straightforward, have a look at one of the existing drivers, and feel free to submit a pull request to add one!
Installation
npm install db-query-assistant
Dependencies
- underscore
- async
- generic-pool
- One or more of the supported database connection modules listed above.
Usage
var assistant = ; assistant; // Parameters for connecting to the database. These are driver specific, see // the driver documentation. This example is for the db-mysql/db-drizzle // driver. var db_config = hostname: 'localhost' user: 'root' password: 'root' database: 'test' ; // Any values for creating a new node-pool Pool object can be passed. var pool_config = max: 10 ; // First argument is the driver to use, see lib/driver/. var db = assistant; /** * Single query example. * * Query functions are passed the following arguments: * query: * A query object that can be used to construct the query to execute. * When using the query object, do not return a value from the query * function! * db: * The node-db database object. * * Note that not all drivers may support query objects. If a driver does not * support it, then no query object argument is passed to the query function. */ var { query; console; } var { console; console; console; } db; /** * Muliple simultaneous queries example. * * The query object doesn't have to be used. Any of the following forms can * be returned by the query function. * * Query results will be returned to the callback function in the order the * queries are passed to the assistant, one data argument per query. * * For efficiency, each query is run on its own database connection. * * To skip executing a query, return false from the query function, the data * result for the skipped query in the callback will also be set to false. * * The exact format returned is driver-specific. This example shows the * db-mysql/db-drizzle format. * * Note that due to the async handling of multiple queries, the query * execution of all queries in the case of a query failure can not be * guaranteed consistent. This method is best used for performing multiple * simultaneous SELECTs. */ var { return "SELECT * FROM test_table WHERE id = 1 LIMIT 1"; } var { return "SELECT * FROM test_table WHERE id = 2 LIMIT 1"; } var { return false; } var { return "SELECT * FROM test_table WHERE id = ? LIMIT 1" 3; } var { console; console; console; console; console; } db; /** * Query series example. * * The result data from the previous query function (if there was one) will be * the first argument to the next query function. * * Since queries are being run in sequence, the same database connection is * reused for efficiency. */ var { return "SELECT COUNT(id) AS count FROM test_table"; } var { console; query; } var { console; } db; /** * Query transaction example. * * Works the same as querySeries, but wraps the query set in a transaction. * Any query errors trigger an automatic rollback of the transaction. */ var { return "INSERT INTO test_table (name) VALUES (?)" 'foo'; } var { console; return "INSERT INTO test_table_detail (id, value) VALUES (?, ?)" insert_datarowsid 'bar'; } var { console; } db;
Support
The issue tracker for this project is provided to file bug reports, feature requests, and project tasks -- support requests are not accepted via the issue tracker. For all support-related issues, including configuration, usage, and training, consider hiring a competent consultant.