Model

Extends

name
Type:
string
options

_private

Type:
Object
schema

_private

Type:
Object
storage
new Model(name, schema, options)
Parameters:
  • name
    • Type: string
    • name of the Model

  • schema
    • Type: Object
    • structure definition of stored data

  • options optional
    • Type: Object
      • key optional
        • Type: Key
        • Default: UUID4Key
        • The strategy used for document's key generation. Must inherit from base Key class

      • refDocKey optional
        • Type: RefDocKey
        • Default: RefDocKey
        • The strategy used for reference document key generation. Must inherit from base RefDocKey class

      • timestamps optional
        • Type: boolean
        • Default: true
        • Adds automatically handled timestamp schema properties: created_at, updated_at and if paranoid option is true, deleted_at property

      • paranoid optional
        • Type: boolean
        • Default: false
        • if true is set, a document is not actually removed from bucket but rather deleted_at flag on the document is set. aka. soft delete

      • camelCase optional
        • Type: boolean
        • Default: false
        • if true is set, camel case notation is used for document's internal properties.

      • schemaSettings optional
        • Type: Object
        • allows to modify default values used for document's key generation and document's property names handled by ODM

          • key optional
            • Type: Object
              • prefix optional
                • Type: string
                • defaults to Model's name

              • postfix optional
                • Type: string
                • Default: ""
              • delimiter optional
                • Type: string
                • Default: "_"
          • doc optional
            • Type: Object
              • idPropertyName optional
                • Type: string
                • Default: "_id"
                • _id contains generated id of document (not whole document's key)

              • typePropertyName optional
                • Type: string
                • Default: "_type"
                • _type contains the name of a Model

      • hooks optional
        • Type: Object
        • allows to add one or more hooks of a hook type (eg. afterValidate)

      • classMethods optional
        • Type: Object
        • custom method definitions which are bound to a Model.

      • instanceMethods optional
        • Type: Object
        • custom method definitions which are bound to a Instance.

      • bucket optional
        • Type: Bucket
        • must be instance of Couchbase.Bucket (from official nodejs couchbase sdk)

      • indexes optional
        • Type: Object
          • refDocs optional
            • Type: Object
            • reference documents definitions aka. custom application layer index support. ref. doc. is a document which reference parent document with its string value=key

Throws:
build(data, options)Instance

builds a new Model instance object

Parameters:
  • data
    • Type: mixed
    • In case an Object or an Array is provided, the data value is NOT cloned!

  • options optional
    • Type: Object
      • isNewRecord optional
        • Type: boolean
        • Default: true
      • sanitize optional
        • Type: boolean
        • Default: false
        • if true, data values are validated, an attempt is made to convert property values to correct type

      • cas optional
        • Type: CAS
      • key optional
        • Type: Key or string
        • instance of Key or valid string id (-> dynamic part of Key) should be provided if the isNewRecord=false that meens the document is persisted in storage

Throws:
Returns: Instance
buildKey(id, options)Key

return a new instance of document's primary key

Parameters:
  • id optional
    • Type: string
  • options optional
    • Type: Object
      • parse optional
        • Type: boolean
        • Default: false
        • if true, id argument will be treated as whole document's key string. By default id is presumed to be only "dynamic part of document's key"

Returns: Key
buildRefDocKey(id, options)RefDocKey

return a new instance of RefDocKey = primary key of a document referencing parent document with it's value

Parameters:
  • id optional
    • Type: string or Array
  • options
    • Type: Object
      • index
        • Type: String
        • index name - one of the keys from options.indexes.refDocs object of Model options

      • parse optional
        • Type: Boolean
        • Default: false
Returns: RefDocKey
create(data, options)
Parameters:
  • data
    • Type: Object
  • options optional
    • Type: Object
    • see StorageAdaper#insert options

      • key optional
        • Type: string or Key
        • the key under which a document should be saved. Can be instance of Key or valid string id (-> dynamic part of Key)

Returns: Promise
exists(id)Promise<Object>
Parameters:
  • id
    • Type: string or Key
Returns: Promise<Object>
getById()

if a key is not found, it returns resolved promise with null value

See: Model#getByIdOrFail arguments
Returns: Promise<Instance|null>
getByIdOrFail(id, options)

if a key is not found, it returns rejected promise with StorageError which has appropriate error code

Parameters:
  • id
    • Type: string or Key
  • options optional
    • Type: object
      • plain optional
        • Type: boolean
        • if true, raw data are returned

      • lockTime optional
        • Type: integer
        • time in seconds (max=30)

      • expiry optional
        • Type: integer
        • time in seconds. if provided, the method performs touch operation on document, returning the most recent document data

      • hooks optional
        • Type: boolean
        • Default: true
        • if false, hooks are not run

      • paranoid optional
        • Type: boolean
        • Default: true
        • if false, both deleted and non-deleted documents will be returned. Only applies if options.paranoid is true for the model.

Returns: Promise
getMulti(ids, options)Promise<Object>

if a operation fails to get a id from ids array, a StorageError is returned in place of resulting value in returned collection/map.

Parameters:
  • ids
    • Type: [(string | Key), ...]
  • options optional
    • Type: Object
      • plain optional
        • Type: boolean
        • Default: false
        • if true, Instances are not built and raw results are collected instead

      • lockTime optional
        • Type: integer
        • time in seconds (max=30)

      • expiry optional
        • Type: integer
        • time in seconds

      • hooks optional
        • Type: boolean
        • Default: true
        • if false, hooks are not run

      • indexed optional
        • Type: boolean
        • Default: true
        • if true, a results are indexed by document's id value, otherwise an array of results is collected

      • individualHooks optional
        • Type: boolean
        • Default: false
        • if true, hooks are run for each get query. if set to false, hooks are only run once before and after getMulti operation

      • paranoid optional
        • Type: boolean
        • Default: true
        • if false, both deleted and non-deleted documents will be returned. Only applies if options.paranoid is true for the model.

Throws:
Returns: Promise<Object>
Example:
//PSEUDOCODE:

var ids = [
    '829f52b2-f168-4538-8884-b5ad9054e391',
    'nonexistent-id'
];

// Indexed results
Model.getMulti(ids, {indexed: true}).then((result) => {
    console.log(result);
});

// The above prints:
    {
        data: {
            '829f52b2-f168-4538-8884-b5ad9054e391': Document, // object
            'nonexistent-id': Error // object
        },
        failed: ['nonexistend-id'],
        resolved: [Document]
    }

// Non-indexed results
Model.getMulti(ids, {indexed: false}).then((result) => {
    console.log(result);
});

// The above prints:
    {
        data: [
            Document, //object
            Error //object
        ],
        failed: [1], // collection of indexes referencing data array items
        resolved: [Document]
    }
remove(id)Promise<Instance>
Parameters:
  • id
    • Type: string or Key
Returns: Promise<Instance>
touch(id, expiry)Promise<Object>
Parameters:
  • id
    • Type: string or Key
Returns: Promise<Object>
unlock(id, cas)Promise<Object>
Parameters:
  • id
    • Type: string or Key
  • cas
    • Type: CAS
Returns: Promise<Object>
addHook(`hookType`, fn, name)

addHook

Parameters:
  • `hookType`
    • Type: string
    • see HookType for available types

  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Throws:
Returns: this
afterCreate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterDestroy(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterFailedIndexRemoval(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterFailedRollback(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterGet(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterRollback(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterUpdate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
afterValidate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeCreate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeDestroy(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeGet(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeRollback(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeUpdate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
beforeValidate(fn, name)this

adds hook function to the stack

Parameters:
  • fn
    • Type: function
    • hook function

  • name
    • Type: string
    • the identificator

Returns: this
listenerCount(hookType)Integer

listenerCount

Parameters:
  • hookType
    • Type: String
    • see HookType for available types

Returns: Integer
removeHook(hookType, name)

removeHook

Parameters:
  • hookType
    • Type: string
    • see HookType for available types

  • name
    • Type: string
    • the identificator

Throws:
Returns: this
runHooks(hookType)

runHooks

additional arguments passed to the function will be passed to the hook function

Parameters:
  • hookType
    • Type: string or Object
Throws:
Returns: Promise
comments powered by Disqus