# relax-model

> CouchDB data models with declarative validation

Latest version **2.4.1** (published 2016-03-16) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install relax-model
pnpm add relax-model
yarn add relax-model
bun add relax-model
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2016-03-16 |
| First published | 2016-03-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Infinite Automata Community |
| Maintainers | tokyo-jesus |
| Keywords | model, db, validation, declarative |

## Links

- npm: https://www.npmjs.com/package/relax-model
- Repository: https://github.com/heropunch/relax-model
- Homepage: https://github.com/heropunch/relax-model#readme
- Issues: https://github.com/heropunch/relax-model/issues
- npm.io page: https://npm.io/package/relax-model

## Dependencies (4)

- [q](https://npm.io/package/q.md) ^1.4.1
- [node-uuid](https://npm.io/package/node-uuid.md) ^1.4.7
- [relax-schema](https://npm.io/package/relax-schema.md) ^1.0.0
- [pouchdb-collate](https://npm.io/package/pouchdb-collate.md) ^1.2.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.4.1 (latest) — 2016-03-16
- 2.4.0 — 2016-03-16
- 2.3.3 — 2016-03-15
- 2.3.2 — 2016-03-14
- 2.3.1 — 2016-03-14
- 2.3.0 — 2016-03-09
- 2.2.0 — 2016-03-07
- 2.1.1 — 2016-03-07
- 2.1.0 — 2016-03-07
- 2.0.0 — 2016-03-07

## README

[![npm version](https://badge.fury.io/js/relax-model.svg)](https://badge.fury.io/js/relax-model)
[![Build Status](https://travis-ci.org/heropunch/relax-model.svg?branch=master)](https://travis-ci.org/heropunch/relax-model)
[![Coverage Status](https://coveralls.io/repos/github/heropunch/relax-model/badge.svg?branch=master)](https://coveralls.io/github/heropunch/relax-model?branch=master)
[![Dependency Status](https://david-dm.org/heropunch/relax-model.svg)](https://david-dm.org/heropunch/relax-model)

# relax-model

<a name="Model"></a>

## Model
A CouchDB data model with declarative validation

**Kind**: global class  

* [Model](#Model)
    * [new Model(name, constraints, validators)](#new_Model_new)
    * [.generatePrimaryIndex(doc)](#Model+generatePrimaryIndex) ⇒ <code>Promise.&lt;String&gt;</code>
    * [.parsePrimaryIndex(doc)](#Model+parsePrimaryIndex) ⇒ <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code>
    * [.validateDocUpdate(newDoc, oldDoc)](#Model+validateDocUpdate) ⇒ <code>Promise.&lt;Object&gt;</code>
    * [.prepare(doc)](#Model+prepare) ⇒ <code>Promise.&lt;Object&gt;</code>

<a name="new_Model_new"></a>

### new Model(name, constraints, validators)
Create a model. Uses [`validate.js`][validate.js] to provide declarative
validation for your documents. Additional validation functions may be
specified by passing an object to the constructor.

[validate.js]: http://validatejs.org/


| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The name of the model. |
| constraints | <code>Object</code> | The constraints this model is subject to. |
| validators | <code>Object</code> | Custom validation functions. |

<a name="Model+generatePrimaryIndex"></a>

### model.generatePrimaryIndex(doc) ⇒ <code>Promise.&lt;String&gt;</code>
Generate the primary index and use it as the document id. The primary
index always includes the name of the model and a unique, sequential
identifier. Fields can be added to the primary index by including
`primaryKey: true` in the constraints object.

Note: Fields in the primary index cannot be modified.

**Kind**: instance method of <code>[Model](#Model)</code>  
**Returns**: <code>Promise.&lt;String&gt;</code> - A promise for the primary index.  

| Param | Type | Description |
| --- | --- | --- |
| doc | <code>Object</code> | The document being indexed. |

<a name="Model+parsePrimaryIndex"></a>

### model.parsePrimaryIndex(doc) ⇒ <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code>
The primary index is designed to be easily rehydrated
into native data types.

**Kind**: instance method of <code>[Model](#Model)</code>  
**Returns**: <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code> - A promise for the rehydrated primary
index object.  

| Param | Type | Description |
| --- | --- | --- |
| doc | <code>Object</code> | The document whose primary index is being rehydrated. |

<a name="Model+validateDocUpdate"></a>

### model.validateDocUpdate(newDoc, oldDoc) ⇒ <code>Promise.&lt;Object&gt;</code>
Validate changes between two versions of a document.

**Kind**: instance method of <code>[Model](#Model)</code>  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise for the validated document.  
**Throws**:

- <code>ValidationError</code> 


| Param | Type | Description |
| --- | --- | --- |
| newDoc | <code>Object</code> | The new version of the document. |
| oldDoc | <code>Object</code> | The current version of the document. |

<a name="Model+prepare"></a>

### model.prepare(doc) ⇒ <code>Promise.&lt;Object&gt;</code>
Prepare models with a simple promise-based API. Objects passed to
`prepare()` are automatically extended with an `_id` property that
includes the name of the model, a unique ID for the document, and
other fields as specified by the primaryKey validator. If `_id`
already exists, this step is skipped.

The `_id` property provides the primary index for the document you are
creating. This value is generated using [`pouchdb-collate`]
[pouchdb-collate] and allows you full access to the query API without
creating additional views. However, this comes with an important
limitation: primary index fields cannot be modified without creating a
new document.

[pouchdb-collate]: https://github.com/pouchdb/collate

**Kind**: instance method of <code>[Model](#Model)</code>  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise for the prepared document.  

| Param | Type | Description |
| --- | --- | --- |
| doc | <code>Object</code> | The document being prepared. |


ERROR, Cannot find class.

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