# rethink-odm

> Simple Object Document Mapper for RethinkDB.

Latest version **0.1.2** (published 2014-07-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install rethink-odm
pnpm add rethink-odm
yarn add rethink-odm
bun add rethink-odm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2014-07-01 |
| First published | 2014-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Greg Bergé |
| Maintainers | neoziro |
| Keywords | rethinkdb, odm, orm, thinky |

## Links

- npm: https://www.npmjs.com/package/rethink-odm
- Repository: https://github.com/neoziro/rethink-odm
- Issues: https://github.com/neoziro/rethink-odm/issues
- npm.io page: https://npm.io/package/rethink-odm

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^2.4.1
- [bluebird](https://npm.io/package/bluebird.md) ^2.1.2
- [rethinkdb](https://npm.io/package/rethinkdb.md) ^1.13.0-0
- [pipe-event](https://npm.io/package/pipe-event.md) ^0.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2014-07-01
- 0.1.1 — 2014-07-01
- 0.1.0 — 2014-06-18

## README

# rethink-odm
[![Build Status](https://travis-ci.org/neoziro/rethink-odm.svg?branch=master)](https://travis-ci.org/neoziro/rethink-odm)
[![Dependency Status](https://david-dm.org/neoziro/rethink-odm.svg?theme=shields.io)](https://david-dm.org/neoziro/rethink-odm)
[![devDependency Status](https://david-dm.org/neoziro/rethink-odm/dev-status.svg?theme=shields.io)](https://david-dm.org/neoziro/rethink-odm#info=devDependencies)

Simple Object Document Mapper for RethinkDB.

## Install

```
npm install rethink-odm
```

## Usage

```js
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](http://rethinkdb.com/api/javascript/connect/).

```js
var ro = rethinkOdm({host: 'localhost'});
```

#### Events

##### error

Emitted when an error occurs in the connection.

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

##### connect

Emitted when the client is connected.

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

##### close

Emmited when the connection is closed.

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

### ro.r

Expose the rethinkdb module.

```js
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.

```js
ro.run(ro.r.now()).then(function (now) {
  // ... 
});
```

### ro.createModel(options)

Create a new model.

- tableName: Name of the table
- hooks: Hooks

```js
var User = ro.createModel({tableName: 'users'});
```

#### Hooks

It's possible to add some hooks, hook are some listeners automatically applied at initialization.

```js
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.

```js
ro.run(User.table().get('1a487dc0-f6ec-11e3-a3ac-0800200c9a66'));
```

### new Model([data])

Create a new instance of the model.

```js
var user = new User({name: 'Johnny'});
```

### model.insert([cb])

Insert the model.

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

#### Events

##### insert

Emitted before the insert.

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

##### inserted

Emitted after the insert.

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

### model.update([data], [cb])

Update the model.

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

#### Events

##### update

Emitted before the update.

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

##### updated

Emitted after the update.

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

### model.delete([cb])

Delete the model.

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

#### Events

##### delete

Emitted before the deletion.

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

##### deleted

Emitted after the deletion.

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

## License

MIT

---
_Source: https://npm.io/package/rethink-odm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
