This plugin is no longer actively maintained, you can still use it but issues will not be resolved. If you want the npm name, you can contact me by email.
Simple Object Document Mapper for RethinkDB.
npm install rethink-odm
var ro = require('rethink-odm')();
// Run command without waiting connection to be ready.
ro.run(ro.r.now()).then(function (now) {
// ...
});
// Create the model "User".
var User = ro.createModel({
tableName: 'users'
});
// Create a new User.
var user = new User({
name: 'Johnny'
});
// Create model.
user.create().then(function (user) {
// ...
});
Create a new rethinkOdm client. To know avalaible options, please refer to rethinkdb documentation.
var ro = rethinkOdm({host: 'localhost'});
Emitted when an error occurs in the connection.
ro.on('error', function (err) {});
Emitted when the client is connected.
ro.on('connect', function () {});
Emmited when the connection is closed.
ro.on('close', function () {});
Expose the rethinkdb module.
ro.r.now();
Run a command using the internal rethink odm connection. The advantage is that you don't have to wait connection to be ready.
ro.run(ro.r.now()).then(function (now) {
// ...
});
Create a new model.
- tableName: Name of the table
- hooks: Hooks
var User = ro.createModel({tableName: 'users'});
It's possible to add some hooks, hook are some listeners automatically applied at initialization.
var User = ro.createModel({
tableName: 'users',
hooks: {
insert: function () {
if (! this.email) throw new Error('Email is required');
}
}
});
Return the table linked to the model.
ro.run(User.table().get('1a487dc0-f6ec-11e3-a3ac-0800200c9a66'));
Create a new instance of the model.
var user = new User({name: 'Johnny'});
Insert the model.
var user = new User({name: 'Johnny'});
user.insert().then(function (user) {
// ...
});
Emitted before the insert.
model.on('insert', function (model) {});
Emitted after the insert.
model.on('inserted', function (model) {});
Update the model.
var user = new User({
id: '1a487dc0-f6ec-11e3-a3ac-0800200c9a66',
name: 'Johnny'
});
user.update().then(function (user) {
// ...
});
Emitted before the update.
model.on('update', function (model, data) {});
Emitted after the update.
model.on('updated', function (model, data) {});
Delete the model.
var user = new User({id: '1a487dc0-f6ec-11e3-a3ac-0800200c9a66'});
user.delete().then(function () {
// ...
});
Emitted before the deletion.
model.on('delete', function (model) {});
Emitted after the deletion.
model.on('deleted', function (model) {});
MIT