大网数据广告活动
Web 标准与前端开发

IDBFactory: databases() method

· 52CE 我爱测

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.

IndexedDB

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:

  • name
    • : A database name.
  • version
    • : The database version.

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.

<pre id="log"></pre>
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.

Create and list databases

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。