javascriptindexeddbpersistent-object-store

Storing values in a newly created objectStore


In this example, the author uses the following line:

var customerObjectStore = 
  db.transaction("customers", "readwrite").objectStore("customers");

I don't understand why "customers" is used twice. I've tried renaming either one of them to see where it might make a difference and the example breaks, so it's clearly necessary that they retain the same name.


Solution

  • The transaction method gets two arguments. The first is an array of tables (Object stores) that you want to work with, and the second is the type of access. In your example, you only want to work with one table, so you have used a string instead of array. But if you want to work on large scale projects, you should work with multiple tables like this:

    var trans = db.transaction(["customers", "payments"], "readwrite");
    var customerObjectStore = trans.objectStore("customers");
    var paymentObjectStore = trans.objectStore("payments");
    

    Hope this example fixes your confusion.