一个简单的 IndexedDB 数据库入门示例
|
freeflydom
2025年9月10日 9:6
本文热度 202
|
写一个浏览器端的数据库 IndexedDB
简单的入门示例。
页面大概长这样:

源码:
以下代码包含了一个数据库所有的 CRUD
(增删改查)操作。
<div>
<button id="js_add_btn">添加书籍</button>
</div>
<div>
<input type="text" name="" id="js_book_id" placeholder="输入书籍ID"/>
<button id="js_get_by_id_btn">查询书籍</button>
<button id="js_update_by_id_btn">更新书籍</button>
<button id="js_delete_by_id_btn">删除书籍</button>
</div>
<div>
<input type="text" name="" id="js_book_author" placeholder="输入书籍作者查询"/><button id="js_get_by_author_btn">查询书籍</button>
</div>
<div>
<button id="js_get_all_btn">查询所有书籍</button>
</div>
<div id="output"></div>
<script>
(() => {
const dbName = 'MyDB';
const storeName = 'books';
const output = document.getElementById('output')
let db = null;
function setOutputContent(html) {
output.innerText = html;
}
const initDB = () => {
const request = indexedDB.open(dbName);
request.onupgradeneeded = (event) => {
db = event.target.result;
const store = db.createObjectStore(storeName, {
keyPath: 'id',
autoIncrement: true,
});
store.createIndex('author_idx', 'author', { unique: false });
console.log('数据库已创建/升级');
};
request.onsuccess = (event) => {
db = event.target.result;
console.log('数据库已打开');
};
request.onerror = (event) => {
console.error('数据库错误:', event.target.error);
setOutputContent('数据库错误');
};
};
function wrapper(func) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([storeName], 'readwrite');
const store = transaction.objectStore(storeName);
const res = func(store)
res.onsuccess = () => {
resolve(res.result)
};
res.onerror = (event) => {
reject(event.target.error)
};
});
}
const addBook = async (book) => {
try {
const result = await wrapper((store) => store.add(book))
console.log(result);
setOutputContent(`添加成功!书籍ID: ${result}`);
} catch (error) {
console.error(error);
setOutputContent('添加失败');
}
};
const getBook = async (id) => {
try {
const book = await wrapper((store) => store.get(id))
if (book) {
console.log('查询结果:', book);
setOutputContent(`书名: ${book.title}\n作者: ${book.author}\n价格: $${book.price}`);
} else {
console.log('未找到书籍');
setOutputContent('未找到该书籍');
}
} catch (error) {
console.error(error);
setOutputContent('查询失败');
}
};
const getAllBook = async () => {
try {
const book = await wrapper((store) => store.getAll())
if (book) {
console.log('查询结果:', book);
setOutputContent(`总数:${book.length}`);
} else {
console.log('未找到书籍');
setOutputContent('未找到该书籍');
}
} catch (error) {
console.error(error);
setOutputContent('查询失败');
}
};
const updateBook = async (book) => {
try {
const result = await wrapper((store) => store.put(book))
console.log(result);
setOutputContent(`更新成功!结果: ${result}`);
} catch (error) {
console.error(error);
setOutputContent('更新失败');
}
};
const deleteBook = async (id) => {
try {
const result = await wrapper((store) => store.delete(id))
console.log(result);
setOutputContent(`删除成功!结果: ${result}`);
} catch (error) {
console.error(error);
setOutputContent('删除失败');
}
};
const getByAuthor = async (author) => {
try {
const result = await wrapper((store) => {
const index = store.index("author_idx");
const request = index.getAll(author);
return request;
})
console.log(result);
setOutputContent(`查询成功!结果: ${JSON.stringify(result)}`);
} catch (error) {
console.error(error);
setOutputContent('查询失败');
}
};
document.getElementById('js_add_btn').onclick = () => {
addBook({
title: 'Web前端入门',
author: '前端路引',
price: (0.99 * Math.random() + 10) .toFixed(2),
});
};
document.getElementById('js_get_by_id_btn').onclick = () => {
const bookId = document.getElementById('js_book_id').value;
getBook(Number(bookId));
};
document.getElementById('js_delete_by_id_btn').onclick = () => {
const bookId = document.getElementById('js_book_id').value;
deleteBook(Number(bookId));
};
document.getElementById('js_update_by_id_btn').onclick = () => {
const bookId = document.getElementById('js_book_id').value;
updateBook({
id: Number(bookId),
title: 'Web前端入门',
author: '公众号:前端路引',
price: (0.99 * Math.random() + 10) .toFixed(2),
});
};
document.getElementById('js_get_by_author_btn').onclick = () => {
const author = document.getElementById('js_book_author').value
getByAuthor(author);
};
document.getElementById('js_get_all_btn').onclick = () => {
getAllBook();
};
initDB();
})();
</script>
IndexedDB 大小限制
以下内容为 AI 回答:

获取可用的存储空间大小
navigator.storage.estimate().then((estimate) => {
console.log(
`已使用:`,
(
(estimate.usage / estimate.quota) * 100
).toFixed(2)
);
console.log(`可使用:`, (estimate.quota / 1024 / 1024).toFixed(2) + "MB");
});
相关文档:https://developer.mozilla.org/en-US/docs/Web/API/Storage_API
写在最后
由于项目中很少使用,所以这 API 给不了太多建议~~
此 API 的应用场景还是有的,可以想想下那些超大文件在线处理应用,比如 ZIP、PSD、PPT 之类的文件,可以将文件解析后存在 IndexedDB 中,在需要的时候查询指定数据即可,这样可以节约很多的解析时间。
只是需要注意,所有存在浏览器端的数据,用户清空缓存之后都将不复存在,所以在使用时需做好容错处理~~
转自https://www.cnblogs.com/linx/p/19062137
该文章在 2025/9/10 9:08:51 编辑过