This is a simple Postgres ORM that uses promises.
npm install pg-orm --save
Note that this will automatically use your database.json file to build the pg link, a la:
{
"dev": {
"driver": "pg",
"user": "skoobi",
"password": "something",
"host": "localhost",
"database": "myDB",
"schema": "myDBSchema"
}
}
The ORM essentially acts like a factory that pops out objects based on a simple DSL. Here's an example definition:
'use strict';
var orm = require('pg-orm');
var person = {
'tableName': 'people',
'tableProperties': {
'id': {
'type': 'key'
},
'name': {
'type': 'string',
'required': true
},
'email': {
'type': 'string',
'required': true
}
}
};
module.exports = orm.build(person);
Once you have a factory set up, you can now create people:
'use strict';
var person = require('./person.js');
person.create({ 'name': 'Jimmy Johnson', 'email': 'test@example.com' }).then(function(newPerson) {
assert(newPerson.name == 'Jimmy Johnson', 'Jimmy should have a name');
newPerson.name = 'Jack White';
newPerson.update().then(function(result) {
assert(newPerson.name == 'Jack White', 'Database should be updated');
});
});
I like db-migrate to manage the database changes and keep the domain in sync.
Create an object with the properties in the hash. This will return a new pg-orm object.
Update the database with the properties of the object that matches tableProperties. This will return the raw pg result.
Update the database with the properties of the hash. This will return the raw pg result.
Find an object by ID. This will return a new pg-orm object.
Delete the object by ID. This will return the raw pg result.
Euphamisn for deleteById with the current ID.
Clone the project locally then install node modules:
npm install
Create a database.json file in the root directory like the example (or just rename it).
Install the test table:
db-migrate up
Run tests:
grunt mochacli