插件介绍

Isar 是一款专为 Flutter 打造的高性能 NoSQL 数据库,以其卓越的性能和丰富的功能而闻名。这款自定义修改版本的 Isar 数据库已适配鸿蒙平台,为鸿蒙开发者提供了强大的数据存储解决方案。

主要特性

  • 💙 专为 Flutter 打造:简化设置,易于使用,几行代码即可开始使用,无样板代码
  • 🚀 高可扩展性:单个数据库实例即可支持存入数十万的数据并能保证高速的异步查询
  • 🍭 多功能性:集成了复合索引和多条目索引、查询修改器、支持 JSON 等丰富功能
  • 🔎 全文检索:内部支持全文检索,创建多条目索引后查询变得十分简单
  • 🧪 ACID 语义:兼容 ACID 语义并能自动处理事务,遇到错误会自动回滚
  • 💃 类型静态:查询都是静态的,即在编译时就已确定变量,完全无需担心运行时错误
  • 📱 多平台支持:完美适配鸿蒙平台,同时支持 iOS、Android、桌面端以及 Web 端
  • 异步多线程:并行查询 & 多线程支持开箱即用
  • 🦄 开源免费:所有一切都是开源并永久免费

如何使用插件

1. 包的引入

由于这个三方库为自定义修改版本,需要以 git 形式引入。在引用的项目中,pubspec.yaml 中 dependencies 新增配置:

dependencies:
  isar:
    git:
      url: "https://atomgit.com/"
      path: "packages/isar/isar"
  isar_flutter_libs:
    git:
      url: "https://atomgit.com/"
      path: "packages/isar/isar_flutter_libs"

dev_dependencies:
  isar_generator:
    git:
      url: "https://atomgit.com/"
      path: "packages/isar/isar_generator"
  build_runner: any

2. 定义数据模型

使用 Isar 注解定义数据模型:

part 'email.g.dart';


class Email {
  Id id = Isar.autoIncrement; // 自动递增

  (type: IndexType.value)
  String? title;

  List<Recipient>? recipients;

  
  Status status = Status.pending;
}


class Recipient {
  String? name;

  String? address;
}

enum Status {
  draft,
  pending,
  sent,
}

3. 生成代码

运行 build_runner 生成 Isar 所需的代码:

flutter pub run build_runner build

4. 打开数据库

import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

final dir = await getApplicationDocumentsDirectory();
final isar = await Isar.open(
  [EmailSchema],
  directory: dir.path,
);

5. CRUD 操作

插入数据
final newEmail = Email()..title = '鸿蒙平台 Isar 数据库使用指南';

await isar.writeTxn(() {
  await isar.emails.put(newEmail); // 插入 & 更新
});
查询数据
final existingEmail = await isar.emails.get(newEmail.id!); // 根据 ID 查询
查询所有数据
final allEmails = await isar.emails.findAll();
删除数据
await isar.writeTxn(() {
  await isar.emails.delete(existingEmail.id!); // 根据 ID 删除
});

6. 高级查询

Isar 提供了强大的查询语言:

// 查询标题包含 '鸿蒙' 的邮件
final emails = await isar.emails.filter()
  .titleContains('鸿蒙', caseSensitive: false)
  .sortByStatusDesc()
  .limit(10)
  .findAll();

// 查询标题以 '重要' 开头的邮件
final importantEmails = await isar.emails
  .where()
  .titleStartsWith('重要')
  .limit(10)
  .findAll();

// 查询收件人姓名为 '张三' 的邮件
final specificEmails = await isar.emails
  .filter()
  .recipient((q) => q.nameEqualTo('张三'))
  .or()
  .titleMatches('*大学*', caseSensitive: false)
  .findAll();

7. 数据库监视器

Isar 提供了数据库监视器,可以实时检查应用中的 Isar 实例和集合。只需在调试模式下运行应用,然后在日志中打开监视器链接即可。

8. 数据库观察者

Isar 支持观察者模式,可以监听集合、对象或查询的变化:

// 监听集合变化
Stream<void> collectionStream = isar.emails.watchLazy();

// 监听查询结果变化
Stream<List<Email>> queryStream = isar.emails.watch();

queryStream.listen((newResult) {
  // 更新 UI
});

总结

Isar 数据库为鸿蒙平台的 Flutter 开发者提供了一个强大、高效且易于使用的数据存储解决方案。其丰富的功能包括全文检索、ACID 事务支持、类型安全查询等,使得数据管理变得简单而高效。

通过本指南,你可以快速上手在鸿蒙平台上使用 Isar 数据库,从包的引入到高级查询操作,全面掌握 Isar 的核心功能。

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐