IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs. This API uses indexes to enable high-performance searches of this data. While Web 存储 可用于存储少量数据,但对于存储大量结构化数据却不那么有用。IndexedDB 提供解决方案。这是 MDN 的 IndexedDB 覆盖范围的主登录页面 — 这里提供完整 API 参考和用法指南,浏览器支持细节及一些关键概念解释的链接。
注意: 此特征可用于 Web 工作者 .注意 :IndexedDB API 功能强大,但对于简单情况而言似乎太复杂。若您偏好简单 API,可尝试库如 localForage , dexie.js , ZangoDB , PouchDB , idb , idb-keyval , JsStore and lovefield that make IndexedDB more programmer-friendly.
IndexedDB is a transactional database system, like an SQL-based RDBMS. However, unlike SQL-based RDBMSes, which use fixed-column tables, IndexedDB is a JavaScript-based object-oriented database. IndexedDB lets you store and retrieve objects that are indexed with a key ; any objects supported by the structured clone algorithm can be stored. You need to specify the database schema, open a connection to your database, and then retrieve and update data within a series of transactions .
注意 : Like most web storage solutions, IndexedDB follows a same-origin policy . So while you can access stored data within a domain, you cannot access data across different domains.
Operations performed using IndexedDB are done asynchronously, so as not to block applications. IndexedDB originally included both synchronous and asynchronous APIs. The synchronous API was intended for use only with Web 工作者 but was removed from the spec because it was unclear whether it was needed. However, the synchronous API may be reintroduced if there is enough demand from web developers.
There are a number of web technologies that store data of one kind or another on the client side (i.e. on your local disk). IndexedDB is most commonly talked about. The process by which the browser works out how much space to allocate to web data storage and what to delete when that limit is reached is not simple, and differs between browsers. 浏览器存储限制和驱逐准则 attempts to explain how this works, at least in the case of Firefox.
要访问数据库,调用
open()
在
indexedDB
attribute of a
window
object. This method returns an
IDBRequest
object; asynchronous operations communicate to the calling application by firing events on
IDBRequest
对象。
IDBEnvironment
window
and
worker
objects. This interface isn't part of the 2.0 specification.
IDBFactory
indexedDB
and is therefore the entry point for the API.
IDBOpenDBRequest
表示打开数据库的请求。
IDBDatabase
Represents a connection to a database. It's the only way to get a transaction on the database.
IDBTransaction
Represents a transaction. You create a transaction on a database, specify the scope (such as which object stores you want to access), and determine the kind of access (read only or readwrite) that you want.
IDBRequest
Generic interface that handles database requests and provides access to results.
IDBObjectStore
Represents an object store that allows access to a set of data in an IndexedDB database, looked up via primary key.
IDBIndex
IDBObjectStore
.
IDBCursor
遍历对象存储和索引。
IDBCursorWithValue
Iterates over object stores and indexes and returns the cursor's current value.
IDBKeyRange
Defines a key range that can be used to retrieve data from a database in a certain range.
IDBLocaleAwareKeyRange
createIndex()
's optionalParameters
.). This interface isn't part of the 2.0 specification.
This specification fires events with the following custom interface:
IDBVersionChangeEvent
IDBVersionChangeEvent
interface indicates that the version of the database has changed, as the result of an
IDBOpenDBRequest.onupgradeneeded
event handler function.
An early version of the specification also defined the following, now removed, interfaces. They are still documented in case you need to update previously written code:
IDBVersionChangeRequest
IDBFactory.open
without also calling
IDBDatabase.setVersion
), and the interface
IDBOpenDBRequest
now has the functionality of the removed
IDBVersionChangeRequest
.
IDBDatabaseException
Represents exception conditions that can be encountered while performing database operations.
IDBTransactionSync
IDBTransaction
.
IDBObjectStoreSync
IDBObjectStore
.
IDBIndexSync
IDBIndex
.
IDBFactorySync
IDBFactory
.
IDBEnvironmentSync
IDBEnvironment
.
IDBDatabaseSync
IDBDatabase
.
IDBCursorSync
IDBCursor
.
| 规范 | 状态 | 注释 |
|---|---|---|
| 索引数据库 API 草案 | 推荐 | |
| 索引数据库 API 2.0 | 推荐 | 初始定义 |
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabase
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBKeyRange
IDBObjectStore
IDBObjectStoreSync
IDBOpenDBRequest
IDBRequest
IDBTransaction
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest