Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including pg-cr-layer with all npm packages installed. Try it out:

var pgCrLayer = require("pg-cr-layer")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

pg-cr-layer v2.0.6

A postgres interface layer for common requests

pg-cr-layer NPM version Dependency Status CircleCI Coverage percentage

A postgres interface layer for common requests. It uses pg to connect and wraps it in a tiny layer using ES2015 promises with the goal to be simpler and compatible with mssql via mssql-cr-layer

Install

$ npm install --save pg-cr-layer

Usage

var pgCrLayer = require('pg-cr-layer');

var config = {
  user: 'me',
  password: 'my password',
  host: 'localhost',
  port: 5432,
  pool: {
    max: 25,
    idleTimeout: 30000
  }
};

var layer = new PgCrLayer(config)

layer.connect()
  .then(function() {
    return layer.execute('CREATE TABLE products ( ' +
      'product_no integer, ' +
      'name varchar(10), ' +
      'price numeric(12,2) )');
  })
  .then(function() {
    return layer.transaction(function(t) {
      return layer
          .execute('INSERT INTO products VALUES (1, \'Cheese\', 9.99)', null, {transaction: t})
          .then(function() {
            return layer.execute('INSERT INTO products VALUES (2, \'Chicken\', 19.99)', null, {transaction: t})
          })
          .then(function() {
        return layer
          .execute('INSERT INTO products VALUES ($1, $2, $3)', [3, 'Duck', 0.99], {transaction: t})
       });
     })
  })
  .then(function() {
    return layer.query('SELECT * FROM products WHERE product_no=@product_no',
      {product_no: {value: 1, type: 'integer'}}) // or just {product_no: 1}
    .then(function(recordset) {
      console.log(recordset[0]); // => { product_no: 1, name: 'Cheese', price: '9.99' }
    })
  })
  .then(function() {
    return layer.close();
  })
  .catch(function(error) {
      console.log(error);
  });

License

MIT © Andre Gloria

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free