I have the following array of objects which i want to add to and indexedDb:
const data = [
{ name: 'abc',
color: 'blue'
},
{ name: 'xyz',
color: 'red'
},
{ name: 'yui',
color: 'black'
}];
Now i create the database and insert this data into a store like this:
if (!db.objectStoreNames.contains("people")) {
peopleStore = db.createObjectStore("people", { keyPath: "name" });
peopleStore.createIndex("color", "color", { unique: false });
peopleStore.transaction.oncomplete = function(event) {
data.forEach(function(data) {
operations.add(data);
});
};
}
I have defined a function expression with an add()
(inside a const operations
) method like shown below:
add: function(data) {
let request = db
.transaction(["people"], "readwrite")
.objectStore("people")
.add(data);
}
So here are my questions:
Is this way creating a new transaction every time the add() method is being called or is it inserting all data in a single transaction?
If it is creating a new transaction every time, how can i make only 1 transaction to improve performance and perform all operations in it only. I am guessing i have to create a global variable with the transaction and perform operations on it (there are other methods too, like
edit()
,delete()
, etc., with each one of them having a "request
" variable inside them defining a transaction, similar to what i have shown above). should i make a global variable something like this:
const globalTrans = db.transaction(["people"], "readwrite").objectStore("people");
- Is creating a new transaction a costly operation?
Thanks in advance to everyone who takes their time to reply! :)