-
-
Notifications
You must be signed in to change notification settings - Fork 648
WriteableCollection.modify()
Modifies all objets in the collection
collection.modify(changes)
changes | Object containing changes to apply on all objects in the collection. Caller can supply a Function instead of Object.
|
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 catched there, finally bubble to db.on("error").
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 funcation may also delete the object by doing delete this.value;
See samples below.
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.MultiModifyError error object containing the following properties:
failures | Array of Error objects of all errors that have occurred |
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.
db.transaction("rw", db.friends, function (friend) {
// Mark bigfoots:
friends.where("shoeSize").aboveOrEqual(47).modify({isBigfoot: true});
// Log all bigfoots.
// Since in transaction, and prev operation is a write-operation, the below operation will be
// stalled until above operation completes, ensuring we get the result after the modification.
friends.where("isBigfoot").equals(true).each(function(friend) {
console.log(friend.name);
});
}).catch (function (e) {
console.error(e);
});
// Convert all shoeSize from european to american size:
db.friends.modify(function(friend) {
friend.shoeSize *= 0.31; // (very approximate formula...., but anyway...)
});
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:
// Replace friend with another friend:
db.friends.where("isKindToMe").equals(false).modify(function(value) {
this.value = new Friend({name: "Another Friend"});
});
// Delete all old none close friends:
db.friends.where("isCloseFriend").equals(false).modify(function(value) {
delete this.value;
});
The sample above is equivalent to:
db.friends.where("isCloseFriend").equals(false).delete();
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.
var db = new Dexie("FriendsDB");
db.version(1).stores({ friends: "++id,name,props.shoeSize,props.bigfoot"});
db.on("populate", function (trans) {
trans.friends.add({
name: "Zlatan",
props: {
shoeSize: 47,
bigfoot: false // Will be changed later ;)
}
});
});
db.open();
db.transaction("rw", db.friends, function (friend) {
// Mark bigfoots:
friends.where("props.shoeSize").aboveOrEqual(47).modify({"props.bigfoot": true});
// Log all bigfoots.
// Since in transaction, and prev operation is a write-operation, the below operation will be
// stalled until above operation completes, ensuring we get the result after the modification.
friends.where("props.bigfoot").equals(true).each(function(friend) {
console.log("Found bigfoot: " + friend.name);
});
}).catch (function (e) {
console.error(e);
});
Dexie.js - minimalistic and bullet proof indexedDB library