IDBFactory: databases() method
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
databases()
Parameters
None.
Return value
A 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:
Note that the sequence on the returned objects is undefined.
Exceptions
SecurityError
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.
Examples
Create and list databases
This example creates/opens a number of databases. On successful initialization of each database it lists all the available databases.
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
Specification |
---|
Indexed Database API 3.0 # ref-for-dom-idbfactory-databases① |
Browser compatibility
BCD tables only load in the browser
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (View the example live).