Front-end IndexedDB database package

IndexedDB

introduce

Browser database IndexedDB IndexedDB is the local database provided by the browser, which can be created and operated by web scripts. IndexedDB allows storing large amounts of data, providing a lookup interface, and building indexes. These are not available in LocalStorage. As far as the database type is concerned, IndexedDB is not a relational database (does not support SQL query statements), but is closer to a NoSQL database. IndexedDB is a relatively complex API involving many concepts. It abstracts different entities into object interfaces. To learn this API is to learn its various object interfaces.

Database: IDBDatabase Object Object Warehouse: IDBObjectStore Object Index: IDBIndex Object Transaction: IDBTransaction Object Operation Request: IDBRequest Object Pointer: IDBCursor Object Primary Key Set: IDBKeyRange Object The following are some main concepts.

(1) database

A database is a collection of related data containers. Each domain name (strictly speaking, protocol + domain name + port) can create any number of databases.

The IndexedDB database has a concept of versions. At the same time, only one version of the database exists. If you want to modify the database structure (add or delete tables, indexes or primary keys), it can only be done by upgrading the database version.

(2) Object warehouse

Each database contains several object stores (object store). It is similar to a table in a relational database.

(3) Data record

The object store holds data records. Each record is similar to a row in a relational database, but only has two parts: the primary key and the data body. The primary key is used to create the default index and must be different, otherwise an error will be reported. The primary key can be an attribute in the data record, or it can be specified as an incrementing integer number.

In the above objects, the id attribute can be used as the primary key.

A data body can be of any data type, not limited to objects.

(4) Index

In order to speed up data retrieval, indexes can be created for different attributes in the object warehouse.

(5) Affairs

The reading, writing and deletion of data records must be done through transactions. The transaction object provides three events of error, abort and complete, which are used to monitor the operation results.

Code cloud address: IndexedDB

npm install:

npm install yiu-indexeddb --save

yarn installation:

yarn add yiu-indexeddb

preview

I hope that friends who have finished watching can click Star/Follow, your support is my greatest encouragement.

If you like this article, welcome to pay attention to my subscription account, the long technical road, and look forward to learning and growing together.

Guess you like

Origin blog.csdn.net/Jensen_Yao/article/details/120269413