IDBTransaction.mode

This article is in need of a technical review.

« IDBTransaction

The IDBTransaction.mode property returns the mode for isolating access to data in the object stores that are in the scope of the transaction. The default value is readonly.

Syntax

readonly  attribute  IDBTransactionMode mode;

Example

// Let us open our database
var request = window.indexedDB.open("toDoList", 4);

// This event handles the event whereby a new version of the database needs to be created
// Either one has not been created before, or a new version number has been submitted via the
// window.indexedDB.open line above
//it is only implemented in recent browsers
request.onupgradeneeded = function(event) {
  var db = event.target.result;
 
  db.onerror = function(event) {
    note.innerHTML += '<li>Error loading database.</li>';
  };

  // Create an objectStore for this database
 
  var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });
 
  // define what data items the objectStore will contain
 
  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });

  objectStore.createIndex("notified", "notified", { unique: false });
 
  note.innerHTML += '<li>Object store created.</li>';
};
    
// store the result of opening the database in the db variable.
db = request.result;

// Create a new object ready for being inserted into the IDB
var newItem = [
  { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" }
];

// open a read/write db transaction, ready for adding the data
var transaction = db.transaction(["toDoList"], "readwrite");

// report on the success of opening the transaction
transaction.oncomplete = function(event) {
  note.innerHTML += '<li>Transaction opened for task addition.</li>';
};

transaction.onerror = function(event) {
  note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};

// call an object store that's already been added to the database
var objectStore = transaction.objectStore("toDoList");

// add our newItem object to the object store
var request = objectStore.add(newItem[0]);  

request.onsuccess = function(event) {
  // report the success of our new item going into the database
  note.innerHTML += '<li>New item added to database.</li>';
};

// Return the mode this transaction has been opened in (should be "readwrite" in this case)
transaction.mode;

Value

An IDBTransactionMode object defining the mode for isolating access to data in the current object stores:

Value Explanation
readonly Allows data to be read but not changed.
readwrite Allows reading and writing of data in existing data stores to be changed.
versionchange Allows any operation to be performed, including ones that delete and create object stores and indexes. This mode is for updating the version number of transactions that were started using IDBDatabase.setVersion. Transactions of this mode cannot run concurrently with other transactions.

Specifications

Specification Status Comment
Indexed Database API Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12-webkit
21
 
4.0 (2.0) 10 17 Not supported
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 6.0 (6.0) 1.0.1 10 17 Not supported

Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

  • Using IndexedDB
  • The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.

Document Tags and Contributors

Contributors to this page: chrisdavidmills
Last updated by: chrisdavidmills,