#rods
a micro SQL ORM using knex
npm install [pg, mysql, mariasql, sqlite3]
npm install knex
npm install rods
###Connecting via knex
see knex documentation for complete list of connection options
var knex = require('knex')({
client: 'pg',
connection: {
host: '',
username: '',
password: ''
}
})
var rods = require('rods')(knex)
###Mapping Tables
var db = {};
db.user = rods.table('users');
###Create Models
ORM does not check if object properties are valid. If invalid object properties do not match DB properties an error is thrown
var u = new db.user({
id: id,
username: 'admin',
password: 'ha$hedp@ssw0rd',
admin: false;
});
u.save(function(err, res) {
});
###Retreving Models
.get()
returns a single model, .fetch()
returns multiple models
db.user.get(1, function(err, u) {
u.admin = true;
u.save(function(err) {
});
});
db.user.get('id-if-it-is-a-string', function(err, u) {});
db.user.get({id: 'value', function(err, u) {});
db.user.fetch([1, 2, 3], function(err, users) {
});
db.user.fetch(['id-1', 'id-2', 'id-3'], function(err, users) {});
db.user.fetch({admin: true}, function(err, users) {});
###Bridge to knex
Use any features of knex with .first()
and .select()
. Just end the chain with .exec()
db.user
.first()
.join('user_groups', 'users.id', 'user_groups.user_id')
.join('groups', 'user_groups.group_id', 'groups.id')
.where('groups.name' '=', 'Administrators')
.exec(function(err, u) {
});
db.user
.select()
.join('user_groups', 'users.id', 'user_groups.user_id')
.join('groups', 'user_groups.group_id', 'groups.id')
.where('groups.name' '=', 'Administrators')
.exec(function(err, users) {
});
###Hooks
hooks run before and after save. useful for history tracking, or settings common properties like 'modified_by'
var rods = require('rods')(knex)
rods.pre('save', function(args, data) {
console.log('pre save with args ' + args);
data.modified_by = args.id;
data.modified_at = Date.now();
})
rods.post('save', function(args, original, updated) {
console.log('post hook');
var userWhoMadeTheChange = args;
trackTheseChangesSomewhere(userWhoMadeTheChange, original, updated)
})
db.user = rods.table('users');
db.user.pre('save', preSave);
db.user.post('save', postSave);
var u = new db.user();
u.save(args, function(err) {
});
###Options
- id: ORM requires each table have 1 single column primary key. It assumes that primary key is named 'id'. You can change this, globally or for a single table.
var rods = require('rods')(knex, {
id: 'ID'
});
db.user = rods.table('users', {
id: 'username'
});