Class Model

Defined in: src/model.coffee

Overview

Public: the model class

Subclasses

CozyBackedModel, PouchdbBackedModel

Class Method Summary

Instance Method Summary

Class Method Details

.all(params, callback) Source

Public: List all instance of a model assume the model has an "all" request params - optional Object requests params (see couchdb doc) callback - Function(Error err, [Model] results) Returns null

Parameters:

  • (Object) paramsoptional Object requests params (see couchdb doc)
  • (Error) callbackFunction(Error err, [Model] results)

Returns:

  • Returns null

.attachBinary(id, path, data, callback) Source

Public: attach a file to the object id - String, id of the model to update path - String, path or [Buffer](http://nodejs.org/api/buffer.html) or [Stream](http://nodejs.org/api/stream.html) data - Obejct options linked with upload :filename - String Name of the file callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, path or Buffer or Stream
  • dataObejct options linked with upload
    • (String) filenameString Name of the file
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.attachFile(id, path, data, callback) Source

Public: attach a file to the object id - String, id of the model to update path - String path or [Buffer](http://nodejs.org/api/buffer.html) or [Stream](http://nodejs.org/api/stream.html) data - Object options linked with upload :filename - String Name of the file callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString path or Buffer or Stream
  • dataObject options linked with upload
    • (String) filenameString Name of the file
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

Object .cast(attributes, target = {}) Source

Public: cast a POJO using this model schema attributes - Object to cast target - optional Object that will be filled with cast properties Returns Object target

Parameters:

  • (Object) attributesObject to cast
  • (Object) targetoptional Object that will be filled with cast properties

Returns:

  • (Object) — Returns Object target

.create(data, callback) Source

Public: create a new instance of this model (POST) data - Object, arguments for the new model callback - Function(Error err, Model created) Returns null

Parameters:

  • dataObject, arguments for the new model
  • (Error) callbackFunction(Error err, Model created)

Returns:

  • Returns null

.defineRequest(name, request, callback) Source

Public: Define a map/reduce request for this model name - String, name of the request request - a single Function (map only) *OR* an object with properties :map - Function :reduce - Function callback - Function(Error err) Returns null

Parameters:

  • (String) nameString, name of the request
  • (Function) requesta single Function (map only) OR an object with properties
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.destroy(id, callback) Source

Public: delete a model by its Id id - String, id of the model to delete callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to delete
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.exists(id, callback) Source

Public: check if a model with given Id exists. id - String, id of the model we are looking for callback - Function(Error err, Boolean exists) Returns null

Returns:

  • Returns null

.find(id, callback) Source

Public: find a model by its Id (GET) id - String, id of the model we are looking for callback - Function(Error err, Model result) Returns null

Parameters:

  • (String) idString, id of the model we are looking for
  • (Error) callbackFunction(Error err, Model result)

Returns:

  • Returns null

.first(callback) Source

Public: Find the first item of this kind useful for singleton type Model callback - Function(Error err, Model found or null) Returns null

Parameters:

  • (Error) callbackFunction(Error err, Model found or null)

Returns:

  • Returns null

LaterStream .getBinary(id, path, callback) Source

Public: get an attached file as a stream id - String, id of the model to update path - String, path or [Buffer](http://nodejs.org/api/buffer.html) or [Stream](http://nodejs.org/api/stream.html) data - Obejct options linked with upload :filename - String Name of the file callback - Function(Error err, [Stream](http://nodejs.org/api/stream.html) file) Returns a LaterStream for the file

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, path or Buffer or Stream
  • dataObejct options linked with upload
    • (String) filenameString Name of the file
  • (Error) callbackFunction(Error err, Stream file)

Returns:

.getFile(id, path, callback) Source

Public: get an attached file as a stream id - String, id of the model to update path - String, path or [Buffer](http://nodejs.org/api/buffer.html) or [Stream](http://nodejs.org/api/stream.html) data - Obejct options linked with upload :filename - String Name of the file callback - Function(Error err, [Stream](http://nodejs.org/api/stream.html) stream}) Returns a [Stream](http://nodejs.org/api/stream.html) for the model

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, path or Buffer or Stream
  • dataObejct options linked with upload
    • (String) filenameString Name of the file
  • (Error) callbackFunction(Error err, Stream stream})

Returns:

  • Returns a Stream for the model

.rawRequest(name, params, callback) Source

Public: Get results for defined request for this model pass the results as an array of object with id, key, value properties name - String, name of the request request - Couchdb query params callback - Function(Error err, ) Returns null

Parameters:

  • (String) nameString, name of the request
  • requestCouchdb query params
  • (Error) callbackFunction(Error err, )

Returns:

  • Returns null

.removeBinary(id, path, callback) Source

Public: remove an attached file id - String, id of the model to update path - String, Name of the attachment callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, Name of the attachment
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.removeFile(id, path, callback) Source

Public: remove an attached file id - String, id of the model to update path - String, Name of the attachment callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, Name of the attachment
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.removeRequest(name, callback) Source

Public: remove a Request name - String, name of the request callback - Function(Error err) Returns null

Parameters:

  • (String) nameString, name of the request
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.request(name, params, callback) Source

Public: Get results for defined request for this model name - String, name of the request request - Couchdb query params callback - Function(Error err, [Model] results) Returns null

Parameters:

  • (String) nameString, name of the request
  • requestCouchdb query params
  • (Error) callbackFunction(Error err, [Model] results)

Returns:

  • Returns null

.requestDestroy(name, params, callback) Source

Public: Destroy results for defined request for this model name - String, name of the request request - Couchdb query params callback - Function(Error err) Returns null

Parameters:

  • (String) nameString, name of the request
  • requestCouchdb query params
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.save(id, data, callback) Source

Public: save (create or update) a model whole state (PUT) id - String, id of the model to update data - Object, new attributes for the model callback - Function(Error err, Model updated) Returns null

Parameters:

  • (String) idString, id of the model to update
  • dataObject, new attributes for the model
  • (Error) callbackFunction(Error err, Model updated)

Returns:

  • Returns null

.saveBinary(id, path, filePath, callback) Source

Public: [DEPRECATED] save an attached file to disk id - String, id of the model to update path - String, Name of the attachment filePath - String, path to save to callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, Name of the attachment
  • (String) filePathString, path to save to
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.saveFile(id, path, filePath, callback) Source

Public: [DEPRECATED] save an attached file to disk id - String, id of the model to update path - String, Name of the attachment filePath - String, path to save to callback - Function(Error err) Returns null

Parameters:

  • (String) idString, id of the model to update
  • (String) pathString, Name of the attachment
  • (String) filePathString, path to save to
  • (Error) callbackFunction(Error err)

Returns:

  • Returns null

.search(query, callback) Source

Public: find docs by FTS query - String, string to search callback - Function(Error err, [Model] results) Returns null

Parameters:

  • (String) queryString, string to search
  • (Error) callbackFunction(Error err, [Model] results)

Returns:

  • Returns null

.updateAttributes(id, data, callback) Source

Public: change some attributes of the model (PATCH) id - String, id of the model to update data - Object, changed attributes for the model callback - Function(Error err, Model updated) Returns null

Parameters:

  • (String) idString, id of the model to update
  • dataObject, changed attributes for the model
  • (Error) callbackFunction(Error err, Model updated)

Returns:

  • Returns null

Constructor Details

Model ::constructor(attributes) Source

Public: constructor attributes - Object, attributes of the model Returns a Model instance

Parameters:

  • attributesObject, attributes of the model

Returns:

Instance Method Details

::attachBinary(path, data, callback) Source

Public: attach a file to the object Instance version of .attachBinary

::attachFile(path, data, callback) Source

Public: attach a file to the object Instance version of .attachFile

::destroy(callback) Source

Public: desttroy Remove the model from the DB callback - Function (Error err) Returns null

Parameters:

  • (Error) callbackFunction (Error err)

Returns:

  • Returns null

::getAttributes() Source

Public: getAttributes Returns this model attributes as a POJO Returns Object

Returns:

  • Returns Object

::getBinary(path, callback) Source

Public: get an attached file as a stream Instance version of .getBinary

::getFile(path, callback) Source

Public: get an attached file as a stream Instance version of .getFile

::index(fields, callback) Source

Public: index Index some fields on this model fields - [String] fields to index callback - Function (Error err) Returns null

Parameters:

  • (String) fields[String] fields to index
  • (Error) callbackFunction (Error err)

Returns:

  • Returns null

::removeBinary(path, callback) Source

Public: remove an attached file Instance version of .removeBinary

::removeFile(path, callback) Source

Public: remove an attached file Instance version of .removeFile

::save(callback) Source

Public: [DEPRECATED] save update or create a model callback - Function(Error err, Model updated) Returns null

Parameters:

  • (Error) callbackFunction(Error err, Model updated)

Returns:

  • Returns null

::saveBinary(path, filePath, callback) Source

Public: [DEPRECATED] save an attached file to disk Instance version of .saveBinary

::saveFile(path, filePath, callback) Source

Public: [DEPRECATED] save an attached file to disk Instance version of .saveFile

::updateAttributes(attributes, callback) Source

Public: updateAttributes apply changes to model (dont change other fields) attributes - Object changes to apply callback - Function(Error err, Model updated) Returns null

Parameters:

  • attributesObject changes to apply
  • (Error) callbackFunction(Error err, Model updated)

Returns:

  • Returns null