项目作者: bimedia-fr

项目描述 :
architect postgresql connection pool
高级语言: JavaScript
项目地址: git://github.com/bimedia-fr/architect-pg-pool.git
创建时间: 2014-03-05T14:00:09Z
项目社区:https://github.com/bimedia-fr/architect-pg-pool

开源协议:Apache License 2.0

下载


architect-pg-pool build status

Expose a posgresql connection pool as architect plugin. Automaticaly returns connection to the pool after query.

Installation

  1. npm install --save architect-pg-pool

Config Format

  1. module.exports = [{
  2. packagePath: "architect-pg-pool",
  3. url: 'postgresql://postgresuser:postgrespwd@localhost:5435/dbname',
  4. checkOnStartUp : true
  5. }];
  • url : Defines the postgres url to use for connection
  • checkOnStartUp : Defines if we must check connection validity on startup default is false.

Usage

Boot Architect :

  1. var path = require('path');
  2. var architect = require("architect");
  3. var configPath = path.join(__dirname, "config.js");
  4. var config = architect.loadConfig(configPath);
  5. architect.createApp(config, function (err, app) {
  6. if (err) {
  7. throw err;
  8. }
  9. console.log("app ready");
  10. });

Configure Architect with config.js :

  1. module.exports = [{
  2. packagePath: "architect-pg-pool",
  3. url: 'postgresql://postgresuser:postgrespwd@localhost:5435/dbname'
  4. }, './routes'];

Consume db plugin in your ./routes/package.json :

  1. {
  2. "name": "routes",
  3. "version": "0.0.1",
  4. "main": "index.js",
  5. "private": true,
  6. "plugin": {
  7. "consumes": ["db"]
  8. }
  9. }

Eventually use pg connection in your routes ./routes/index.js :

  1. module.exports = function setup(options, imports, register) {
  2. var rest = imports.rest;
  3. var db = imports.db;
  4. // register routes
  5. rest.get('/hello/:name', function (req, res, next) {
  6. db.query('SELECT * FROM Users WHERE id=$1', [req.params.name], function(err, res){
  7. res.write("{'message':'hello," + res.rows[0].name + "'}");
  8. res.end();
  9. });
  10. });
  11. register();
  12. };

Multiple pool configuration

This module supports multiple pools.

Here is how to define 2 different pools :

  1. module.exports = [{
  2. packagePath: "architect-pg-pool",
  3. first : {
  4. url: 'postgresql://postgresuser:postgrespwd@localhost:5435/dbname'
  5. },
  6. second : {
  7. url: 'postgresql://postgresuser:postgrespwd@localhost:5432/otherdb'
  8. },
  9. checkOnStartUp : true
  10. }];

This will create 2 properties (first and second) in the db object.

  1. module.exports = function setup(options, imports, register) {
  2. var db = imports.db;
  3. db.first.connection(function (err, client) {
  4. client.query(/*...*/);
  5. });
  6. register();
  7. };

default pool

A pool can be marked as default and will be available in db.connection.
Here is how to define 2 different pools with the second as default :

  1. module.exports = [{
  2. packagePath: "architect-pg-pool",
  3. first : {
  4. url: 'postgresql://postgresuser:postgrespwd@localhost:5435/dbname'
  5. },
  6. second : {
  7. url: 'postgresql://postgresuser:postgrespwd@localhost:5432/otherdb',
  8. 'default' : true
  9. },
  10. checkOnStartUp : true
  11. }];

This will create 2 properties (first and second) in the db object.

  1. module.exports = function setup(options, imports, register) {
  2. var db = imports.db;
  3. // this will use second pool
  4. db.connection(function (err, client) {
  5. client.query(/*...*/);
  6. });
  7. // second pool is also available
  8. db.second.connection(function (err, client) {
  9. client.query(/*...*/);
  10. });
  11. register();
  12. };

Configuration

  • url either a connection url or an object :

    • host : serveur hostname or ip
    • port : serveur port
    • user : username to login,
    • password : password to login,
    • database: database name,
    • application_name: a name to identify client,
    • validationQuery: a query to run to validate a connection
  • checkOnStartup : boolean, Whether we should try to validate configuration at startup.

API

The pool object (db) has the following methods :

connection

Retreive a connection from the pool. The method takes a callback as parameter. Once the connection is avaliable the callback is called with an :

  • err object if an error occured or null;
  • client the pg client object;
  • done, the close method.

query

The query method let you directly query the database without worrying about the database connection. Behind the scene the method retreive a connection from the pool and close it afterward. The method signature is similar to node-pg query.

  • string text: the query text;
  • optional array parameters: the query parameters;
  • optional function callback : the function called when data is ready.

Once the data is ready the callback is fired with an :

  • err object if an error occured or null;
  • rows the pg result set.
  1. module.exports = function setup(options, imports, register) {
  2. var db = imports.db;
  3. db.query('SELECT * from USERS', function (err, res) {
  4. res.rows.forEach(console.log);
  5. });
  6. //...
  7. };

queryStream

The queryStream method let you directly query the database without worrying about the database connection. This method passes a stream to the callback instead of a resultset. Behind the scene the method retreive a connection from the pool and close it afterward. The method signature is similar to node-pg query-stream.

  • string text: the query text;
  • optional array parameters: the query parameters;
  • optional function callback : the function called when stream is ready.
  • returns: ReadableStream

Once the stream is ready the callback is fired with an :

  • err object if an error occured or null;
  • stream the pg result stream.
  1. var JSONSteam = require('JSONStream');
  2. module.exports = function setup(options, imports, register) {
  3. var db = imports.db;
  4. db.queryStream('SELECT * from USERS')
  5. .pipe(JSONSteam.stringify())
  6. .pipe(process.stdout);
  7. //...
  8. };