2.4.1 • Published 8 years ago

relax-model v2.4.1

Weekly downloads
5
License
Apache-2.0
Repository
github
Last release
8 years ago

npm version Build Status Coverage Status Dependency Status

relax-model

Model

A CouchDB data model with declarative validation

Kind: global class

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.

ParamTypeDescription
namestringThe name of the model.
constraintsObjectThe constraints this model is subject to.
validatorsObjectCustom 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.

ParamTypeDescription
docObjectThe 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.

ParamTypeDescription
docObjectThe 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
ParamTypeDescription
newDocObjectThe new version of the document.
oldDocObjectThe 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.

ParamTypeDescription
docObjectThe document being prepared.

ERROR, Cannot find class.

2.4.1

8 years ago

2.4.0

8 years ago

2.3.3

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago