pg-dal

A simple postgresql data access layer for node.js

npm install pg-dal
4 downloads in the last week
8 downloads in the last month

pg-dal

Simple Postgres Data access layer for node.js

Examples

Creating Records

var Dal = require('pg-dal');
var pgConfig = {
  user: 'dbuser',
  password: 'password',
  database: 'mydb',
  host: 'localhost',
  port: 5432
};
var sanitizeColumns = ['id', 'name', 'size'];
var defaults = {
  size: 'Large'
};
var itemDal = new Dal('items', pgConfig, sanitizeColumns, defaults, function(){
  console.log('created dal!');
});
item = itemDal.build({name: 'wheel'});
item.save(function(err, item){
  console.log('wheel saved!');
});

Custom Validation

var myValidation = function(item, next){
  var err = null;
  if(item.name === 'wheel'){
    err = new Error('Don\'t add wheels');
  }
  next(err);
};
itemDal.validation(myValidation);
item = itemDal.build({name: 'wheel'});
item.save(function(err, item){
  if(err){
    console.log(err.message);
    // will log Don't add wheels
  }
});
npm loves you