npm.io
0.1.2 • Published 12 years ago

rethink-odm

Licence
MIT
Version
0.1.2
Deps
4
Vulns
5
Weekly
0
Stars
3

rethink-odm

Build Status Dependency Status devDependency Status

Simple Object Document Mapper for RethinkDB.

Install

npm install rethink-odm

Usage

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) {
  // ...
});
rethinkOdm(options) / rethinkOdm.createClient(options)

Create a new rethinkOdm client. To know avalaible options, please refer to rethinkdb documentation.

var ro = rethinkOdm({host: 'localhost'});
Events
error

Emitted when an error occurs in the connection.

ro.on('error', function (err) {});
connect

Emitted when the client is connected.

ro.on('connect', function () {});
close

Emmited when the connection is closed.

ro.on('close', function () {});
ro.r

Expose the rethinkdb module.

ro.r.now();
ro.run(command, [cb])

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) {
  // ... 
});
ro.createModel(options)

Create a new model.

  • tableName: Name of the table
  • hooks: Hooks
var User = ro.createModel({tableName: 'users'});
Hooks

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');
    }
  }
});
Model.table()

Return the table linked to the model.

ro.run(User.table().get('1a487dc0-f6ec-11e3-a3ac-0800200c9a66'));
new Model([data])

Create a new instance of the model.

var user = new User({name: 'Johnny'});
model.insert([cb])

Insert the model.

var user = new User({name: 'Johnny'});
user.insert().then(function (user) {
  // ...
});
Events
insert

Emitted before the insert.

model.on('insert', function (model) {});
inserted

Emitted after the insert.

model.on('inserted', function (model) {});
model.update([data], [cb])

Update the model.

var user = new User({
  id: '1a487dc0-f6ec-11e3-a3ac-0800200c9a66',
  name: 'Johnny'
});
user.update().then(function (user) {
  // ...
});
Events
update

Emitted before the update.

model.on('update', function (model, data) {});
updated

Emitted after the update.

model.on('updated', function (model, data) {});
model.delete([cb])

Delete the model.

var user = new User({id: '1a487dc0-f6ec-11e3-a3ac-0800200c9a66'});
user.delete().then(function () {
  // ...
});
Events
delete

Emitted before the deletion.

model.on('delete', function (model) {});
deleted

Emitted after the deletion.

model.on('deleted', function (model) {});

License

MIT

Keywords