IndexedDB API - Web APIs 编辑

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 Storage is useful for storing smaller amounts of data, it is less useful for storing larger amounts of structured data. IndexedDB provides a solution. This is the main landing page for MDN's IndexedDB coverage — here we provide links to the full API reference and usage guides, browser support details, and some explanation of key concepts.

Note:

This feature is available in Web Workers.

Note: IndexedDB API is powerful, but may seem too complicated for simple cases. If you'd prefer a simple API, try libraries such as localForage, dexie.js, ZangoDB, PouchDB, idb, idb-keyval, JsStore and lovefield that make IndexedDB more programmer-friendly.

Key concepts and usage

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.

Note: 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.

Synchronous and asynchronous

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 Workers 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.

Storage limits and eviction criteria

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. Browser storage limits and eviction criteria attempts to explain how this works, at least in the case of Firefox.

Interfaces

To get access to a database, call open() on the indexedDB attribute of a window object. This method returns an IDBRequest object; asynchronous operations communicate to the calling application by firing events on IDBRequest objects.

Connecting to a database

IDBEnvironment
Provides access to IndexedDB functionality. It is implemented by the window and worker objects. This interface isn't part of the 2.0 specification.
IDBFactory
Provides access to a database. This is the interface implemented by the global object indexedDB and is therefore the entry point for the API.
IDBOpenDBRequest
Represents a request to open a database.
IDBDatabase
Represents a connection to a database. It's the only way to get a transaction on the database.

Retrieving and modifying data

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
Also allows access to a subset of data in an IndexedDB database, but uses an index to retrieve the record(s) rather than the primary key. This is sometimes faster than using IDBObjectStore.
IDBCursor
Iterates over object stores and indexes.
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 This API has not been standardized.
Defines a key range that can be used to retrieve data from a database in a certain range, sorted according to the rules of the locale specified for a certain index (see createIndex()'s optionalParameters.). This interface isn't part of the 2.0 specification.

Custom event interfaces

This specification fires events with the following custom interface:

IDBVersionChangeEvent
The IDBVersionChangeEvent interface indicates that the version of the database has changed, as the result of an IDBOpenDBRequest.onupgradeneeded event handler function.

Obsolete interfaces

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 This is an obsolete API and is no longer guaranteed to work.
Represents a request to change the version of a database. The way to change the version of the database has since changed (by calling IDBFactory.open without also calling IDBDatabase.setVersion), and the interface IDBOpenDBRequest now has the functionality of the removed IDBVersionChangeRequest.
IDBDatabaseException  This is an obsolete API and is no longer guaranteed to work.
Represents exception conditions that can be encountered while performing database operations.
IDBTransactionSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBTransaction.
IDBObjectStoreSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBObjectStore.
IDBIndexSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBIndex.
IDBFactorySync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBFactory.
IDBEnvironmentSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBEnvironment.
IDBDatabaseSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBDatabase.
IDBCursorSync This is an obsolete API and is no longer guaranteed to work.
Sync version of IDBCursor.

Examples

Specifications

SpecificationStatusComment
Indexed Database API 2.0Recommendation
Indexed Database API 2.0RecommendationInitial definition

See also

  • localForage: A Polyfill providing a simple name:value syntax for client-side data storage, which uses IndexedDB in the background, but falls back to WebSQL and then localStorage in browsers that don't support IndexedDB.
  • Dexie.js: A wrapper for IndexedDB that allows much faster code development via nice, simple syntax.
  • ZangoDB: A MongoDB-like interface for IndexedDB that supports most of the familiar filtering, projection, sorting, updating and aggregation features of MongoDB.
  • JsStore: An IndexedDB wrapper with SQL like syntax.
  • MiniMongo: A client-side in-memory mongodb backed by localstorage with server sync over http. MiniMongo is used by MeteorJS.
  • PouchDB: A client-side implementation of CouchDB in the browser using IndexedDB
  • idb: A tiny (~1.15k) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.
  • idb-keyval: A super-simple-small (~600B) promise-based keyval store implemented with IndexedDB
  • sifrr-storage: A small (~2kB) promise based library for client side key-value storage. Works with IndexedDB, localStorage, WebSQL, Cookies. Can automatically use supported storage available based on priority.
  • lovefield: Lovefield is a relational database for web apps. Written in JavaScript, works cross-browser. Provides SQL-like APIs that are fast, safe, and easy to use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:227 次

字数:24406

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文