ravel-mysql-provider
Ravel DatabaseProvider for MySQL
ravel-mysql-provider
is a DatabaseProvider
for Ravel, wrapping the powerful node mysql library. It supports connection pooling as well as Ravel's transaction system (including rollbacks).
Example usage:
Step 1: Import and instantiate the MySQLProvider
app.js
const app = 'ravel';const MySQLProvider = ;app;// ... other providers and parametersapp;app;// ... the rest of your Ravel appappstart;
@transaction
Step 2: Access connections via resources/posts_resource.js
const Ravel = ;const autoinject = Ravelautoinject;const Resource = RavelResource;const transaction = Resourcetransaction; @@ /** * Retrieve a single post */ @ { // Best practice is to pass the transaction object through to a Module, where you handle the actual business logic. return thisposts ; }
Step 3: Use connections to perform queries
modules/posts.js
const Ravel = ;const Module = RavelModule; @ { return { const mysql = transaction'mysql'; // for more information about the mysql connection's capabilities, visit the docs: https://github.com/mysqljs/mysql mysql; }; }
Step 4: Configuration
Requiring the ravel-mysql-provider
module will register a configuration parameter with Ravel which must be supplied via .ravelrc
or app.set()
:
.ravelrc
All options for a mysql
connection are supported, and are documented here.
Additional Notes
Multiple Simultaneous Providers
ravel-mysql-provider
also supports multiple simultaneous pools for different mysql databases, as long as you name them:
app.js
const app = 'ravel';const MySQLProvider = ;app;app;// ... other providers and parametersappstart;
.ravelrc
resources/posts_resource.js
const Ravel = ;const Resource = RavelResource;const transaction = Resourcetransaction; @ // ... @ { // can use ctx.transaction['first mysql'] // and ctx.transaction['second mysql'] }
Named Parameter Syntax
ravel-mysql-provider
bakes-in the named parameter syntax described here.