Skip to content

Table.hook('deleting')

David Fahlander edited this page Apr 17, 2014 · 11 revisions

Syntax

db.[tableName].hook('deleting', function (primKey, obj, transaction) {
    // You may do additional database operations using given transaction object.
    // Any modification to obj is ignored.
    // Any return value is ignored.
    // throwing exception will make the db operation fail.
});

Parameters

primKey The primary key of the object being deleted
obj Object that is about to be deleted. Modification of obj will NOT affect the operation.
transaction Transaction instance.

Return Value

Any return value of given subscriber is ignored.

Description

This event is called whenever an object is about to be deleted from the database no matter which method is used. Methods that may delete objects are WriteableTable.delete(), WriteableTable.clear(), WriteableCollection.delete() but also WriteableTable.put() and WriteableCollection.modify() since they might replace existing objects resulting in a ´deleting´ event followed by a ´creating´ event.

A subscriber may use the given transaction object to do additional operations on the database. The chain of operations can be considered atomic since the subscriber can work on the same transaction as being used for creating the object.

Error Handling

If subscriber throws an exception, the delete operation will fail and the caller of the delete operation will get the failure as a Promise rejection that may be catched/handled or not. If the caller of the delete operation does not catch the excetion using Promise.catch(), the transaction will be aborted.

If a database operation initiated by the subscriber, results in a failure the transaction will be aborted no matter if the origin caller of the delete operation calls catch() or not. However, the origin caller will recieve your error if catching transaction failures. If you as the implementor of the subscriber want to ignore errors resulting from your operations, you may catch() your database operations to prohibit transaction from being aborted. However, it is normally better to let the transaction abort in case a failure of your database operation would impinge database consistancy.

Use Cases of the CRUD events

Dexie CRUD events can be used to implement several addons to Dexie such as:

  • Server Synchronization
  • Automatic primary key generation
  • Views
  • Full-text search or other custom indexes
  • etc.

Internally the hook('reading') event is used by the methods Table.defineClass() and Table.mapToClass() in order to make all objects retrieved from database inherit a given class using prototypal inheritance.

See Also

Table.hook('creating')

Table.hook('reading')

Table.hook('updating')

Clone this wiki locally