great-scott

0.7.0 • Public • Published

Great Scott

NPM

Postgres data source library

API

Optional properties

  • model - A Immutable Data Record (see https://www.npmjs.com/package/immutable).
  • connectionString - A Postgres connection string.
  • tableName - (required for insert/update helpers) the name of the table to extract data from
  • idAttribute - (optional) the primary key attribute

Example Usage

 
import { DataSource } from 'great-scott';
import Avery from 'avery';
 
var UserModel = Avery.Model({
 
  name : 'User',
 
  defaults : {
    id : null,
    name : null,
  }
 
});
 
class UserDataSource extends DataSource {
  constructor() {
    super({
      tableName : 'users',
      model : UserModel,
      connectionString : process.env.POSTGRES_URL,
    });
  }
 
  // object to model with camel case keys
  parse(row) {
    return new this.model(_.transform(row, function(memo, val, key) {
      memo[ _.camelCase(key) ] = val;
      return memo;
    }));
  }
 
  // model to object with snake case keys
  format(model) {
    var row = model.toObject();
    return _.transform(row, function(memo, val, key) {
      memo[ _.snakeCase(key) ] = val;
      return memo;
    });
  }
 
  findAll : function() {
    var query = this.builder
      .select()
      .from('users');
 
    return this.execute(query);
  }
 
  createUser(userModel) {
    return this.insert(userMode);
  }
}
 
UserDataSource.createUser(myUser)
.then(function(results) {
  console.log('success', results);
});
 
UserDataSource.findAllUsers()
.then(function(results) {
  console.log('success', results);
});
 

Package Sidebar

Install

npm i great-scott

Weekly Downloads

1

Version

0.7.0

License

MIT

Last publish

Collaborators

  • bjy
  • isaiahdw