introduce
IndexedDB is a low-level API for storing large amounts of structured data on the client side. At present, all browsers have been supported, and the compatibility is very good.
features
- IndexedDB is a JavaScript-based object-oriented database, IndexedDB allows you to store and retrieve objects indexed with keys ;
- Any object supported by the structured cloning algorithm can be stored;
- Use indexes to enable high-performance searching of data;
- Most of the IndexedDB API is asynchronous. The data will not be provided through the return value, but a callback function will be passed to get the return value;
- IndexedDB obeys the same-origin policy . When you operate and store data under a certain domain name, you cannot operate data under other domain names;
- Can store files/binary large objects (blobs);
- Available in Web Workers;
- IndexedDB is a transactional database system.
use
To gain access to the database, call the open() method on the indexedDB (en-US) property of the window object. This method returns an IDBRequest object; asynchronous operations communicate with the calling program by firing events on the IDBRequest object.
The specific use can see the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<title>Title</title>
<link href="./favicon.png">
</head>
<body>
</body>
<script>
let dbName = 'helloIndexDB', version = 1, storeName = 'helloStore'
let indexedDB = window.indexedDB
let db
const request = indexedDB.open(dbName, version)
request.onsuccess = function(event) {
db = event.target.result // 数据库对象
console.log('数据库打开成功')
}
request.onerror = function(event) {
console.log('数据库打开报错')
}
request.onupgradeneeded = function(event) {
// 数据库创建或升级的时候会触发
console.log('onupgradeneeded')
db = event.target.result // 数据库对象
let objectStore
if (!db.objectStoreNames.contains(storeName)) {
objectStore = db.createObjectStore(storeName, { keyPath: 'id' }) // 创建表
// objectStore.createIndex('name', 'name', { unique: true }) // 创建索引 可以让你搜索任意字段
}
}
// 添加数据
function addData(db, storeName, data) {
let request = db.transaction([storeName], 'readwrite') // 事务对象 指定表格名称和操作模式("只读"或"读写")
.objectStore(storeName) // 仓库对象
.add(data)
request.onsuccess = function(event) {
console.log('数据写入成功')
}
request.onerror = function(event) {
console.log('数据写入失败')
throw new Error(event.target.error)
}
}
// 根据id获取数据
function getDataByKey(db, storeName, key) {
let transaction = db.transaction([storeName]) // 事务
let objectStore = transaction.objectStore(storeName) // 仓库对象
let request = objectStore.get(key)
request.onerror = function(event) {
console.log('事务失败')
}
request.onsuccess = function(event) {
console.log('主键查询结果: ', request.result)
}
}
// 根据id修改数据
function updateDB(db, storeName, data) {
let request = db.transaction([storeName], 'readwrite') // 事务对象
.objectStore(storeName) // 仓库对象
.put(data)
request.onsuccess = function() {
console.log('数据更新成功')
}
request.onerror = function() {
console.log('数据更新失败')
}
}
// 根据id删除数据
function deleteDB(db, storeName, id) {
let request = db.transaction([storeName], 'readwrite').objectStore(storeName).delete(id)
request.onsuccess = function() {
console.log('数据删除成功')
}
request.onerror = function() {
console.log('数据删除失败')
}
}
// 由于打开indexDB是异步的加个定时器避免 db对象还没获取到值导致 报错
setTimeout(() => {
addData(db, storeName, {
id: new Date().getTime(), // 必须且值唯一
name: '张三',
age: 18,
desc: 'helloWord'
})
getDataByKey(db, storeName, 1638160036008)
updateDB(db, storeName, {id: 1638164880484, desc: '修改的内容'})
deleteDB(db, storeName, 1638164870439)
}, 1000)
</script>
</html>
quick access
At present, there are many packaged third-party libraries, such as the following, you can choose according to your needs:
- localForage : A simple polyfill that provides simple value syntax for client-side data storage. It uses IndexedDB behind the scenes and falls back to WebSQL or localStorage in browsers that don't support IndexedDB.
- Dexie.js : A wrapper for IndexedDB, with a simple syntax for faster code development.
- ZangoDB : An IndexedDB-like interface to MongoDB that supports most of MongoDB's familiar filtering, projection, sorting, updating, and aggregation functions.
- JsStore : An IndexedDB wrapper with SQL syntax.