0.7.0 • Published 9 years ago

great-scott v0.7.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

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);
});
0.7.0

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.0

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago