海哥 发表于 3 天前

用于操纵浏览器中的 IndexedDB 数据库的javascript库Dexie.js详解

Dexie.js 详解

Dexie.js 是一个高性能、功能丰富的 JavaScript 库,用于操纵浏览器中的 IndexedDB 数据库。它提供了一个简单而强大的 API,克服了原生 IndexedDB 的复杂性和限定,使开发者可以更轻松地举行前端持久化数据存储。
1. 什么是 Dexie.js?

Dexie.js 是一个基于 Promise 的库,用于管理 IndexedDB 数据库。它提供了一个简单、易用且强大的 API,得当存储大量结构化数据(如缓存、用户数据、离线数据等),支持事件管理和复杂查询。
2. Dexie 的特点



[*]简化 IndexedDB 操纵:提供了直观的 API,消除了原生 IndexedDB 的回调嵌套问题。
[*]Promise 和 Async/Await 支持:允许以现代 JavaScript 的异步编程方式处理数据库操纵。
[*]强大的查询本事:支持复杂的查询操纵,如范围查询、索引查询等。
[*]事件支持:内置事件机制,确保多个数据库操纵的原子性。
[*]跨浏览器兼容性:兼容大多数现代浏览器,包括 Chrome、Firefox、Safari 和 Edge。
[*]轻量级:Dexie.js 的核心库非常小,不会显著增加项目体积。
3. 安装 Dexie

可以通过 npm 或 CDN 安装 Dexie.js:
npm 安装

npm install dexie
CDN 引入

<script src="https://cdn.jsdelivr.net/npm/dexie/dist/dexie.min.js"></script>
4. Dexie 的基本用法

1)创建数据库

import Dexie from "dexie";

// 创建数据库实例
const db = new Dexie("MyDatabase");

// 定义数据库表和索引
db.version(1).stores({
    friends: "++id, name, age, email", // 主键 id,自增,索引 name, age 和 email
});
2)插入数据

async function addFriend() {
    await db.friends.add({
      name: "John Doe",
      age: 25,
      email: "johndoe@example.com",
    });
    console.log("Friend added!");
}
3)查询数据

async function getFriends() {
    const friends = await db.friends.toArray();
    console.log("All friends:", friends);
}
4)更新数据

async function updateFriend(id) {
    await db.friends.update(id, { age: 26 });
    console.log("Friend updated!");
}
5)删除数据

async function deleteFriend(id) {
    await db.friends.delete(id);
    console.log("Friend deleted!");
}
5. 复杂查询

Dexie 支持丰富的查询操纵:
范围查询

async function getYoungFriends() {
    const youngFriends = await db.friends.where("age").below(30).toArray();
    console.log("Young friends:", youngFriends);
}
多条件查询

async function getSpecificFriend() {
    const friend = await db.friends
      .where({ name: "John Doe", age: 25 })
      .first();
    console.log("Specific friend:", friend);
}
使用索引查询

async function findByEmail(email) {
    const friend = await db.friends.where("email").equals(email).first();
    console.log("Friend with email:", friend);
}
6. 事件支持

Dexie 提供事件支持,可以确保一组操纵的原子性:
async function transactionExample() {
    await db.transaction("rw", db.friends, async () => {
      await db.friends.add({ name: "Alice", age: 30 });
      await db.friends.add({ name: "Bob", age: 28 });
    });
    console.log("Transaction completed!");
}
7. 版本升级

假如必要在已有数据库中增加或修改表,可以通过版本升级实现:
db.version(2).stores({
    friends: "++id, name, age, email, city", // 新增 city 字段
});
8. 数据库事件

Dexie 提供事件机制,允许监听数据库变化:
db.friends.hook("creating", (primKey, obj, transaction) => {
    console.log("Creating new friend:", obj);
});
9. 高级功能



[*]动态表名:可以动态定义表名,得当多租户应用。
[*]加密存储:结合其他库(如 Crypto.js),可以实现加密存储。
[*]离线支持:Dexie.js 非常得当 PWA(渐进式 Web 应用)的离线存储需求。
[*]插件扩展:支持通过插件添加额外功能,如同步到远程数据库。
10. 使用场景



[*]前端缓存:存储 API 数据以淘汰网络哀求。
[*]离线存储:保存用户数据供离线时使用。
[*]用户设置:保存用户设置、偏好或状态。
[*]大型数据存储:得当存储大型结构化数据集,如电子表格或项目管理数据。
11. 与其他库的对比

特性Dexie.js原生 IndexedDBLocalStorageWebSQL易用性★★★★★★★★★★★★★性能★★★★★★★★★★★★★★★查询本事★★★★★★★★★★★★★★数据量支持★★★★★★★★★★★★★★★★异步支持★★★★★★★★★★★★★★★ 总结

Dexie.js 是操纵 IndexedDB 的强大工具,非常得当必要高性能、结构化、离线存储的 Web 应用。通过其简单的 API 和丰富的功能,开发者可以快速实现复杂的持久化存储逻辑,同时保持代码的可读性和维护性。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 用于操纵浏览器中的 IndexedDB 数据库的javascript库Dexie.js详解