29
loading...
This website collects cookies to deliver better user experience
add
, delete
, get
, put
) it is necessary to create a transaction, since it will specify which "tables" (Object Stores) will be part of the transaction, as well as the type of access that will be used to perform the transaction, for this the transaction(storeNames, mode)
method of the IDBDatabase object is used, so the connection created previously is used.// Database connection (IDBDatabase)
let db;
function createDatabase() {
//...
const request = window.indexedDB.open('MyDatabase', 1);
request.onsuccess = (e) => {
// Create the DB connection
db = request.result;
};
//...
}
// Create a transaction
const transaction = db.transaction('students', 'readonly');
//...
transaccion(storeNames, mode)
method has the following parameters:storeNames
: This parameter refers to the Object Stores (tables) that are part of the transaction, the value is an array with their names. However, if only one Object Store will be used, the value can be a string.
mode
: This parameter indicates the type of access that will be used to carry out the transaction, this can be readonly (default) or readwrite.
objectStore(storeName)
method which is used to access to each Object Store (table) and carry out the respective operations.// Database connection (IDBDatabase)
let db;
function createDatabase() {
//...
const request = window.indexedDB.open('MyDatabase', 1);
request.onsuccess = (e) => {
// Create the DB connection
db = request.result;
};
//...
}
// Create a transaction
const transaction = db.transaction('students', 'readonly');
transaction.oncomplete = function(event) {
// This event will be executed when
// the transaction has finished
};
transaction.onerror = function(event) {
// Handling Errors
};
// Access to the "students table"
const objectStore = transaction.objectStore('students');
//...