relax-model v2.4.1
relax-model
Model
A CouchDB data model with declarative validation
Kind: global class
- Model
- new Model(name, constraints, validators)
- .generatePrimaryIndex(doc) ⇒ Promise.<String>
- .parsePrimaryIndex(doc) ⇒ Promise.<Array.<Object>>
- .validateDocUpdate(newDoc, oldDoc) ⇒ Promise.<Object>
- .prepare(doc) ⇒ Promise.<Object>
new Model(name, constraints, validators)
Create a model. Uses validate.js
to provide declarative
validation for your documents. Additional validation functions may be
specified by passing an object to the constructor.
Param | Type | Description |
---|---|---|
name | string | The name of the model. |
constraints | Object | The constraints this model is subject to. |
validators | Object | Custom validation functions. |
model.generatePrimaryIndex(doc) ⇒ Promise.<String>
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 Model
Returns: Promise.<String> - A promise for the primary index.
Param | Type | Description |
---|---|---|
doc | Object | The document being indexed. |
model.parsePrimaryIndex(doc) ⇒ Promise.<Array.<Object>>
The primary index is designed to be easily rehydrated into native data types.
Kind: instance method of Model
Returns: Promise.<Array.<Object>> - A promise for the rehydrated primary
index object.
Param | Type | Description |
---|---|---|
doc | Object | The document whose primary index is being rehydrated. |
model.validateDocUpdate(newDoc, oldDoc) ⇒ Promise.<Object>
Validate changes between two versions of a document.
Kind: instance method of Model
Returns: Promise.<Object> - A promise for the validated document.
Throws:
- ValidationError
Param | Type | Description |
---|---|---|
newDoc | Object | The new version of the document. |
oldDoc | Object | The current version of the document. |
model.prepare(doc) ⇒ Promise.<Object>
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
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.
Kind: instance method of Model
Returns: Promise.<Object> - A promise for the prepared document.
Param | Type | Description |
---|---|---|
doc | Object | The document being prepared. |
ERROR, Cannot find class.