A ridiculously simple Object Mapper for Node running on top of CouchDB.
MIT License
Endtable is an experimental ORM built on top of Node.js and CouchDB.
The concept? long-lived, self-monitoring, objects that persist only periodically as modifications are made to them.
This paradigm reduces the frequency with which writes are made to the database, and works well for domains such as games.
A single engine object is instantiated and passed as a dependency to an Endtable Class:
var engine = new endtable.Engine({
database: 'people_example',
host: 'localhost',
user: '',
password: '',
errorCallback: function(error) {
// When views aren't found they raise a warning.
sys.puts(JSON.stringify(error));
}
});
Errors that occur in Endtable are propagated up to the errorCallback passed to the Engine's constructor.
Error objects provide the following information:
Endtable Classes describe the ORM-backed objects.
var Dog = endtable.Object.extend(
{
bark: function() {
sys.puts('Woof!!!');
}
},
{
engine: engine,
type: 'dog',
customViews: [
function lowerName(doc) {
if(doc.type=='dog')
emit(doc.name.toLowerCase(),doc);
},
function otherNamedView(doc) { ... }
]
}
);
Once you've created some Endtable Classes simply instantiate them to create auto-persisting CouchDB-backed objects.
var dog = new Dog({
name: 'Spike',
owner: 'Benjamin Coe',
coat: 'soft'
})
var dog2 = new Dog({
name: 'Fluffy',
owner: 'Eric Brown',
coat: 'rough'
}, function(err, message) {
// Called after the first save.
});
The first parameter passed to the constructor provides the instance variables for the object.
An optional callback can be provided for the second parameter and will be executed the first time the object persists to CouchDB.
You can lookup objects based on any of their keys.
Simply call the load method on an Endtable Class:
Dog.load({
keys: ['owner', 'coat'],
key: ['Benjamin Coe', 'soft']
}, function(error, obj) {
if (!error) {
obj[0].bark();
}
})
You can also load objects based on a range of values.
Person.load({
keys: 'age',
startkey: 28,
endkey: 50
}, function(error, obj) {
if (!error) {
for (var i = 0; i < obj.length; i++) {
obj[i].sayName();
}
}
})
This will load individuals with an age ranging from 28 to 50.
You can also load an object with a custom view function applied to it.
Dog.load({
keys: 'name',
customView: 'lowerName'
}, function(error, obj) {
if (!error) {
for (var i = 0; i < obj.length; i++) {
obj[i].sayName();
}
}
})
Run node examples/person.js to get an idea of Endtable in action.
Copyright (c) 2011 Benjamin Coe. See LICENSE.txt for further details.