Baseline 2024
Newly available
Note: This feature is available in Web Workers.
The databases
method of the IDBFactory
interface returns a Promise
that fulfills with an array of objects containing the name and version of all the available databases.
This is a snapshot of the databases, intended primarily to allow web applications to check what databases have been created â in order to, for example, clean up databases created by earlier versions of application code.
Syntax ParametersNone.
Return valueA Promise
that fulfills with an array of objects representing a snapshot of the available databases (or rejects with the error/exceptions below).
Each array object has the following properties:
name
A database name.
version
The database version.
Note that the sequence on the returned objects is undefined.
ExceptionsSecurityError
DOMException
Thrown if the method is called from an opaque origin or the user has disabled storage.
UnknownError
DOMException
Thrown if the set of available databases cannot be determined for any reason.
This example creates/opens a number of databases. On successful initialization of each database it lists all the available databases.
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
#log {
height: 240px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
JavaScript
First we define the function that is used to get and log the available databases. This awaits on the promise returned by indexedDB.databases()
and then iterates the array and lists the values of each element:
async function getDb() {
const databases = await indexedDB.databases();
log("List databases:");
databases.forEach((element) => {
log(`name: ${element.name}, version: ${element.version}`);
});
}
To demonstrate how the above function is used, below we create two databases. For each database, we log just before the database is opened. We also log on successful initialization (or error) and then also log the available databases.
// Create a database named toDoList with default version (1)
const dbName1 = "toDoList";
log(`Opening: ${dbName1}`);
let DBOpenRequest = window.indexedDB.open(dbName1);
DBOpenRequest.addEventListener("error", (event) => {
log(`Error opening: ${dbName1}`);
getDb();
});
DBOpenRequest.addEventListener("success", (event) => {
log(`Initialized: ${dbName1}`);
getDb();
});
// Create database "AnotherDb"
const dbName2 = "AnotherDb";
log(`Opening ${dbName2}`);
DBOpenRequest = window.indexedDB.open(dbName2, 2);
DBOpenRequest.addEventListener("error", (event) => {
log(`Error opening: ${dbName2}`);
getDb();
});
DBOpenRequest.addEventListener("success", (event) => {
log(`Initialized: ${dbName2}`);
getDb();
});
Result
The result is shown below. Note that the time taken to get the databases and their order is undefined.
Specifications Browser compatibility See alsoIDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4