great-scott
v0.7.0
Published
A Marty-like data source for postgres.
Downloads
11
Readme
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 fromidAttribute
- (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);
});