Collection.modify()
Modifies all objects in the table subset represented by this Collection instance.
Syntax
Parameters
changes
Object containing changes to apply on all objects in the collection. Caller can supply a Function instead of Object.
If an Object is supplied, each key is the path to a property to change and each value is the new value to set
If a Function is supplied, the given function will be called for each matched object in Collection. Function retrieves the object as first argument. Function may then modify, add or delete properties of the object. When function has returned, the framework will update the given object to database with the changes made on it.
Return Value
Promise where resolved value is the number of modified objects. If any object fails to be updated, the entire operation will fail and Promise will reject. To catch the error, call Promise.catch() on the returned Promise, else the error will bubble to the aborted transaction and if not caught there, finally bubble to db.on("error").
Remarks
Applies given changes to all objects in the collection.
If given changes is an Object, each key in the object represents a keyPath and each value the new value to set. A key path can be the name of a property or if containing periods (.) act as a path to a property in a nested object.
If the value is a function, the function will be called for each object so that the function may modify or delete any properties on the object. A function may also replace the object with another object by using this.value = otherObject. Finally, the function may also delete the object by doing delete this.value;
See samples below.
Error Handling
If any object fails to be modified or an exception occur in a callback function, entire operation will fail and transaction will be aborted.
If you catch the returned Promise, transaction will not abort, and you recieve a Dexie.ModifyError error object containing the following properties:
failedKeys
Array of the keys of the failed modifications. This array will have the same order as failures so that failures[i] will always represent the failure of failedKeys[i]
successCount
Number of successful modifications made.
If you do NOT catch the returned Promise, and an error occur, the transaction will be aborted.
If you want to log the error but still abort the transaction, you must encapsulate the operation in a transaction() block and catch the transaction instead. It is also possible to catch the operation and call transaction.abort() in the catch() clause.
Sample
Sample using Function
Sample Replacing Object
The this pointer points to an object containing the property value. This is the same value as being sent as the first argument. To replace the object entirely you cannot use the value from the argument since "by ref" is not possible with javascript. Instead, use this.value = otherObj
to change the reference, as sample code below shows:
With arrow function (Supported since Dexie v1.3.4):
Sample Deleting Object
With arrow function (Supported since Dexie v1.3.4):
The sample above is equivalent to:
Sample using Nested Key
In this sample, we modify a property that is a nested key (a key in a nested object). We include the schema and an example object in this example just to clarify how nested objects are used.