Instance

Extends

Model
Type:
Model
new Instance(data, options)

Model Instance constructor & prototype

Parameters:
  • data
    • Type: mixed
  • options optional
    • Type: Object
      • key optional
        • Type: Key
        • Key instance representing primary key of the document

      • isNewRecord optional
        • Type: boolean
        • Default: true
      • cas optional
        • Type: CAS
        • If the item on the server contains a different CAS value, the operation will fail. Note that if this option is undefined, no comparison will be performed.

Throws:
clone()Instance
Returns: Instance
destroy(options)Promise

deletes the document and all related reference documents from a bucket

Parameters:
  • options optional
    • Type: Object
    • See StorageAdapter.remove for available options

      • force optional
        • Type: Boolean
        • Default: false
        • performs destoy operation even with no cas value set

Returns: Promise
getRefDocs()Promise<[Document>, ...]

returns collection of reference Documents of the Model instance.

Returns: Promise<[Document>, ...]
getSerializedData()Promise<{mixed}>

converts object's associations to json objects with single property with value of key string returns object's serialized data

Returns: Promise<{mixed}>
insert(options)Promise<Instance>

saves NEW document (fails if the document already exists in a bucket) to storage with all its reference documents, if an error occurs, an attempt for rollback is made, if the rollback fails, the afterFailedRollback hook is triggered.

Parameters:
  • options optional
    • Type: Object
    • See StorageAdapter.insert for available options

Returns: Promise<Instance>
isDirty(path)Boolean

Returns true when property value is modified. (differs from what is stored in a bucket) When no property path is provided, it checks whether the whole dataset is equal to its settled (persisted) counterpart

Parameters:
  • path
    • Type: [String, ...] or String
    • property path

Returns: Boolean
Example:
instance.isDirty();
// or
instance.isDirty('path.to.nested.property');
// or
instance.isDirty(['path', 'to', 'nested', 'property']);
populate(include, options)Instance

populates model's referenced associations

Parameters:
  • include
    • Type: String or Array or Object
  • options optional
    • Type: Object
      • skipPopulated optional
        • Type: Boolean
        • Default: true
        • if false, associations will be reloaded even though they've been already loaded

      • getOrFail optional
        • Type: Boolean
        • Default: false
        • if true, when there is an association which can not be found, the populate method will return rejected promise with the error

Returns: Instance
Example:
user.populate('friends'); //single path

user.populate([
    'friends',
    'apps'
]); // multiple paths

user.populate({
    path: 'friends',
    populate: 'friends'
}); // Populates user's friends and friends of the friends

// You can combine these three styles to describe exactly what you want to populate
refresh()Promise<Instance>

Explicitly fetches itself from upstream. Works also with destroyed documents which have been soft-deleted due to paranoid=true option

Returns: Promise<Instance>
replace(options)Promise<Instance>

replaces (updates) current document (fails if the document with the key does not exists) in a bucket and synchronizes reference documents (indexes). If an error occur, an attempt for rollback is made, if the rollback fails, the afterFailedRollback hook is triggered for every document an atempt for rollback failed (includes failed refdocs operations)

Parameters:
  • options optional
    • Type: Object
    • See StorageAdapter.replace for available options

      • force optional
        • Type: Boolean
        • Default: false
        • performs replace operation even with no cas value set

Returns: Promise<Instance>
sanitize()undefined

validates & tries to parse data values according to defined schema

Throws:
Returns: undefined
save(options)Promise<Instance>

shortcut method for calling Instance#replace / Instance#insert. if fresh instance is initialized and the data are not persisted to bucket yet, insert is called. If the Instance is already persisted, data are updated with replace method

Parameters:
Returns: Promise<Instance>
setData(property, data)Instance

override Document.prototype.setData

Parameters:
  • property optional
    • Type: string
  • data
    • Type: mixed
Returns: Instance
Example:
instance.setData({some: 'data'}) // bulk set enumerable properties of provided data object
instance.setData('username', 'fogine') //writes to `username` property on data object
toJSON()Object
Throws:
  • InstanceError

Returns: Object
touch(expiry, options)Promise<Instance>

touches underlaying document and all its reference documents when an Error occurs, it tries to touch all remaining documents before failing

Parameters:
  • expiry
    • Type: integer
    • time in seconds

Throws:
  • StorageError

Returns: Promise<Instance>
update(data, options)Instance

This is similar to seting data on the instance and then calling Instance#save, (respectively Instance#replace) however in this case when the operation fails, the Model's instance data are restored to the previous state.

Parameters:
  • data
    • Type: Object
Returns: Instance
getCAS()CAS or null
Returns: CAS or null
getData(property)mixed
Parameters:
  • property optional
    • Type: string
Returns: mixed
getGeneratedKey()Promise<Key>
Returns: Promise<Key>
getKey()Key or string or null
Returns: Key or string or null
getStorageAdapter()StorageAdapter or null
Returns: StorageAdapter or null
hasCAS()boolean
Returns: boolean
remove(options)Promise<Object>

Removes the document identified by its key. This is an atomic operation which always operates on single document only.

Parameters:
  • options
    • Type: Object
    • See StorageAdapter.remove for available options

Returns: Promise<Object>
setCAS(cas)undefined
Parameters:
  • cas
    • Type: CAS
Returns: undefined
setKey()undefined
Throws:
  • DocumentError

Returns: undefined
toString()string
Returns: string
comments powered by Disqus