-
-
Notifications
You must be signed in to change notification settings - Fork 647
Table.hook('updating')
db.[tableName].hook('updating', function (modifications, primKey, obj, transaction) {
// You may use transaction to do additional database operations.
// You may not do any modifications on any of the given arguments.
// If you want to make additional modifications, return another modifications object
// containing the additional or overridden modifications to make. Any returned
// object will be merged to the given modifications object.
});
modifications | Object of keyPaths and values that is about to be applied as a patch to the existing object. Changes made to this object will not affect the update operation unless the object is also returned as return value. |
primKey | The primary key of the object being updated |
obj | Object that is about to be updated. This object MUST NOT be modified. Instead modify the _modifications_ object by setting your desired keyPath as key and your desired value as value and then return the modified modifications object. |
transaction | Transaction instance. |
If you do not want to change any modification, dont return any value (return undefined). If you want to add additional modifications or change any of the modifications in the given modifications argument, you should return another object containing the additional or overridden modifications to make.
This event is called whenever an existing database object is being updated (through any of the WriteableTable.update() or WriteableCollection.modify() methods.
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 updating the object. If any exception or database error event occur, the entire transaction will abort.
If subscriber throws an exception, the update operation will fail and the caller of the update operation will get the failure as a Promise rejection that may be catched/handled or not. If the caller of the update 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 update 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.
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.
The following sample shows how to implement support for document text search:
Dexie.js - minimalistic and bullet proof indexedDB library