npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-postgresql-pool-connector

v1.0.3

Published

it's wrapper ready to use for making query to a postgresql database very easy and simple.

Downloads

12

Readme

koa-postgresql-pool-connector

Build Status codecov.io

The MIT License (MIT)

koa-postgresql-pool-connector is a wrapper on top of koa-pg ready to use.

Table of Contents

Practical information
Files tree
Installation
API documentation
Example
Acknowledgement
Other project

Practical information

You must be familiar with :

Files tree

    ./
    ├── examples                // Contain one working example.
    │   └── index.js
    ├── .gitignore
    ├── index.js                // Current npm module sources
    ├── LICENSE
    ├── package.json
    ├── README.md
    └── __tests__               // Jest Unit Tests
    │   └── index-test.js
    └── .travis.yml

Installation

    $ npm install koa-postgresql-pool-connector

API documentation

This is a simple code overview of how to use koa-postgresql-pool-connector. After you added the module to your nodeJs app, this.sqlQuery will be available from every koa generator scope functions.

this.sqlQuery

Param name | Description | Mandatory ------------ | ------------- | ------------- databaseUrl | string which should contain the database connection info | YES query | string which should contain the query to execute on PG db | YES Return | promise which you could use as you want for callback | always present

    /**
     * module for db pool connection
     * @param databaseUrl key
     * @param query to execute by client
     * @return promise
     */

code simplification

const app = require( 'koa' )(),
    router = require( 'koa-joi-router' )(),
    dbCo = require( 'koa-postgresql-pool-connector' );

app.use( dbCo );
app.use( router.middleware() );

router.post('/example',function* (next){
const query = 'postegresql query';
    this.sqlQuery("urlDb",query)
    .then(function( respData ){
        //do your stuf after query execution
    })
});

Example

Following example could be found inside /examples directory.

Live test it by doing npm run devExample, which will start nodejs instance with koa/koaPostgres api with 3 working path. Don't forget to set a postgresSql Db on your local computer to make the example server work.

*current db url connection : postgres://postgres:postgres@localhost:5432/postgres

Example overview

  • set every const we will need.
// const dbCo is our koa-postgresql-pool-connector
const koa = require( 'koa' ),
    dbCo = require( '../index.js' ),
    dbUrl = '',
    router = require( 'koa-joi-router' )(),
    bodyParser = require( 'koa-bodyparser' ),
    app = koa();
  • add middlewares to Nodejs app.
// add our koa-postgresql-pool-connector module to nodeJs app as middleware.
app.use( dbCo );
// use a koa bodyparser to get form data as middleware.
app.use( bodyParser() );
// declare a koa joi router as middleware.
app.use( router.middleware() );
  • create specifiques routes via koi-joi-router. We see that i passed callbacks to every routes
router
    .post( '/createFilmTable', function* ( next ) {
        'use strict';
// just call this.sqlQuery and you will get you function to request database with a query and a callback to execute at the end
        this.sqlQuery( 'postgres://postgres:postgres@localhost:5432/postgres',
            "CREATE TABLE films (" +
            "id serial NOT NULL,  CONSTRAINT films_pkey PRIMARY KEY (id)," +
            "title       varchar(40) NOT NULL," +
            "kind        varchar(10)," +
            ");"
        )
    } )
    .get( '/films/', function* ( next ) {
        'use strict';

        let that = this;

        yield this.sqlQuery( 'postgres://postgres:postgres@localhost:5432/postgres',
            "SELECT * FROM films;"
        ).then( function ( data ) {
            if ( data.name === "error" ) {
                that.status = 500;
                that.body = data;
            }
            if ( data.rowCount !== 0 ) {
                that.status = 200;
                that.body = { data: data.rows }
            }
            else {
                that.status = 204
            }
        } );
    } )
    .post( '/films/', function* ( next ) {
        'use strict';
        let data = this.request.body,
            that = this;

        if ( data.title && data.kind ) {
            yield this.sqlQuery( 'postgres://postgres:postgres@localhost:5432/postgres',
                          'INSERT INTO films (title,kind) VALUES (\'' + data.title + '\',\'' + data.kind + '\')' )
                      .then( function ( data ) {
                          console.log( arguments );
                          if ( data.name === "error" ) {
                              that.status = 500;
                              that.body = data;
                          }
                          if ( data && data.rowCount !== 0 ) {
                              that.status = 200;
                              that.body = { data: data.rows };
                          }
                          else {
                              that.status = 204;
                          }
                      } );
        }
        else {
            this.status = 400;
            this.body = { error: 'missing title or kind key with values' };
        }
    } );
  • register start nodejs server on port 3000
app.listen( 3000 );

Acknowledgement

Thanks to @Companeo for let me post this module I developped for work under open source license. Thanks to @MathRobin for his help and introduction to Koa.

Other project

You may also like this project : git-hooks-versionned