Skip to content

Table.hook('creating')

David Fahlander edited this page Apr 20, 2014 · 19 revisions

Syntax

db.[tableName].hook('creating', function (primKey, obj, transaction) {
    // You may do additional database operations using given transaction object.
    // You may also modify given obj
    // If returning any value other than undefined, the returned value will be used as primary key
});

Parameters

primKey The primary key of the object being added, or undefined in case primary key will be created by the system.
obj Object that is about to be created. Modification of obj will affect what will be added to the database.
transaction Transaction instance.

Return Value

If return value of given subscriber is other than undefined, the return value will be used as the primary key. Implentors may use this to provide extended methods of auto-generation primary keys other than the built-in autoIncrement (++) method. Return value is only handled in case given primKey was undefined. If primKey was set, any return value will be ignored since it is not allowed.

Description

This event is called whenever an object is being added into the database no matter which method is used. When calling WriteableTable.add(), it will always be called. But when calling WriteableTable.put() it will only be called if the operation results in an object creation. If it would result in replacing an existing object, hook('updating') will be triggered.

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. If any exception or database error event occur, the entire transaction will abort.

Error Handling

If subscriber throws an exception, the create operation will fail and the caller of the create operation will get the failure as a Promise rejection that may be catched/handled or not. If the caller of the create 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, but then the transaction has already aborted. 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('reading')

Table.hook('updating')

Table.hook('deleting')

Clone this wiki locally