UNPKG

great-scott

Version:

A Marty-like data source for postgres.

84 lines (62 loc) 1.66 kB
# Great Scott [![NPM](https://nodei.co/npm/great-scott.png)](https://npmjs.org/package/great-scott) 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 ```javascript 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); }); ```