ravel-postgresql-provider
Ravel DatabaseProvider for postgresql
ravel-postgresql-provider
is a DatabaseProvider
for Ravel, wrapping the powerful node postgresql library. It supports connection pooling as well as Ravel's transaction system (including rollbacks).
Example usage:
Step 1: Import and instantiate the PostgreSQL provider
app.js
const app = 'ravel';const postgresqlProvider = ;app;// ... other providers and parametersapp;app;// ... the rest of your Ravel appapp;app;
@transaction
Step 2: Access connections via resources/posts_resource.js
const Ravel = ;const inject = Ravelinject;const Resource = RavelResource;const transaction = Resourcetransaction; @ { super'/posts'; thisposts = posts; } /** * 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 postgresql = transaction'postgresql'; // for more information about the postgresql connection's capabilities, visit the docs: https://github.com/postgresqljs/postgresql postgresql; }; }
Step 4: Configuration
Requiring the ravel-postgresql-provider
module will register a configuration parameter with Ravel which must be supplied via .ravelrc
or app.set()
. This configuration parameter matches the format defined by pg
for Pools:
.ravelrc
All options for a node-postres
connection are supported, and are documented here.
Additional Notes
Multiple Simultaneous Providers
ravel-postgresql-provider
also supports multiple simultaneous pools for different postgresql databases, as long as you name them:
app.js
const app = 'ravel';const postgresqlProvider = ;app 'first postgresql';app 'second postgresql';// ... other providers and parametersapp;// ... the rest of your app
.ravelrc
resources/posts_resource.js
const Ravel = ;const Resource = RavelResource;const transaction = Resourcetransaction; // ... @ { // can use ctx.transaction['first postgresql'] // and ctx.transaction['second postgresql'] }