首页 > web前端 > js教程 > 正文

IndexedDB 解释

碧海醫心
发布: 2024-10-30 16:24:02
转载
398人浏览过

在上一篇文章中,我们讨论了 dexie,indexeddb 的包装器。在本文中,我们讨论 indexeddb。您必须熟悉这个 localstorage api,通常用于在浏览器中存储信息。类似地,indexeddb 用于客户端存储。

什么是 indexeddb?

mdn文档说明:

indexeddb 是一个低级 api,用于客户端存储大量结构化数据(包括文件/blob)。此 api 使用索引来实现对此数据的高性能搜索。虽然 web 存储对于存储少量数据很有用,但对于存储大量结构化数据则不太有用。

IndexedDB 解释

indexeddb 提供了一个解决方案。这是 mdn indexeddb 报道的主要登陆页面 - 在这里我们提供完整 api 参考和使用指南、浏览器支持详细信息以及关键概念的一些解释的链接。

示例存储库:

mdn 提供了一个示例 github 存储库,并有 script/todo.js。

使用 window.onload 初始化脚本

window.onload = () => {
}
登录后复制

打开数据库请求:

// let us open our database
const dbopenrequest = window.indexeddb.open('todolist', 4);
登录后复制

连接错误:

// register two event handlers to act on the database being opened successfully, or not
dbopenrequest.onerror = (event) => {
 note.appendchild(createlistitem('error loading database.'));
};
登录后复制

数据库连接成功:

dbopenrequest.onsuccess = (event) => {
 note.appendchild(createlistitem('database initialised.'));
// store the result of opening the database in the db variable. this is used a lot below
 db = dbopenrequest.result;
// run the displaydata() function to populate the task list with all the to-do list data already in the indexeddb
 displaydata();
};
登录后复制

添加数据

// open a read/write db transaction, ready for adding the data
const transaction = db.transaction(['todolist'], 'readwrite');
// call an object store that's already been added to the database
const objectstore = transaction.objectstore('todolist');
// make a request to add our newitem object to the object store
const objectstorerequest = objectstore.add(newitem[0]);
objectstorerequest.onsuccess = (event) => {
 // process data on success.
}
// report on the success of the transaction completing, when everything is done
transaction.oncomplete = () => {
 note.appendchild(createlistitem('transaction completed: database modification finished.'));
// update the display of data to show the newly added item, by running displaydata() again.
 displaydata();
};
// handler for any unexpected error
transaction.onerror = () => {
 note.appendchild(createlistitem(`transaction not opened due to error: ${transaction.error}`));
};
登录后复制

观察:localstorage 与 indexeddb

您现在可能已经意识到,仅添加一条记录就需要大量代码,您有异步回调,例如 onerror 和 onsuccess。这个堆栈交换答案中指出了这一点。

为了简化处理此 indexeddb,可以使用 dexie。

使用 dexie 添加数据:

export function AddFriendForm({ defaultAge } = { defaultAge: 21 }) {
 const [name, setName] = useState('');
 const [age, setAge] = useState(defaultAge);
 const [status, setStatus] = useState('');
async function addFriend() {
 try {
 // Add the new friend!
 const id = await db.friends.add({
 name,
 age
 });
setStatus(`Friend ${name} successfully added. Got id ${id}`);
 setName('');
 setAge(defaultAge);
 } catch (error) {
 setStatus(`Failed to add ${name}: ${error}`);
 }
 }
return (
 <>
 <p>{status}</p>
 Name:
 <input
 type="text"
 value={name}
 onChange={(ev) => setName(ev.target.value)}
 />
 Age:
 <input
 type="number"
 value={age}
 onChange={(ev) => setAge(Number(ev.target.value))}
 />
 <button onClick={addFriend}>Add</button>
 </>
 );
}
登录后复制

这个包装 api 让我想起了 prisma 和 drizzle 等 orm。

关于我们:

在 thinkthroo,我们研究大型开源项目并提供架构指南。我们开发了使用 tailwind 构建的 resubale 组件,您可以在您的项目中使用它们。我们提供 next.js、react 和 node 开发服务。

与我们预约会面讨论您的项目。

IndexedDB 解释

IndexedDB 解释

参考资料:

  1. https://www.reddit.com/r/sveltejs/comments/15rj12h/any_downsides_to_using_indexeddb_vs_localstorage/

  2. https://developer.mozilla.org/en-us/docs/web/api/indexeddb_api

  3. https://github.com/mdn/dom-examples/tree/main/to-do-notifications

  4. https://softwareengineering.stackexchange.com/questions/219953/how-is-localstorage- different-from-indexeddb

以上就是IndexedDB 解释的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号