Isar NoSQL数据库:为Flutter量身打造的高性能数据库

Isar NoSQL数据库:为Flutter量身打造的高性能数据库

isar Extremely fast, easy to use, and fully async NoSQL database for Flutter isar 项目地址: https://gitcode.com/gh_mirrors/isar/isar

1. 项目介绍

Isar 是一个为 Flutter 设计的超高速、易用的 NoSQL 数据库。它具有高度的可扩展性、丰富的功能、完全异步的操作,并且是多平台的,支持 iOS、Android 和桌面应用。Isar 数据库以其出色的性能、一致性的 ACID 语义、静态类型检查和美丽的文档而受到开发者们的青睐。

2. 项目快速启动

添加依赖

首先,在 pubspec.yaml 文件中添加 Isar 的依赖:

dependencies:
  isar:
    version: 3.1.8
    hosted: https://pub.isar-community.dev/

定义数据模型

定义一个数据模型,例如一个 Email 类:

@collection
class Email {
  final int id = Isar.autoIncrement;
  late String title;
  late List<Recipient> recipients;
  @enumerated
  late Status status;
}

@embedded
class Recipient {
  late String name;
  late String address;
}

enum Status { draft, pending, sent }

打开数据库

在应用启动时打开一个 Isar 数据库实例:

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

查询数据库

使用 Isar 的查询功能检索数据:

final emails = await isar.emails
  .filter()
  .titleContains('awesome', caseSensitive: false)
  .sortByStatusDesc()
  .limit(10)
  .findAll();

3. 应用案例和最佳实践

CRUD 操作

Isar 提供了基本的 CRUD 操作,以下是如何使用它们:

final newEmail = Email()..title = 'Amazing new database';
await isar.writeTxn(() {
  await isar.emails.put(newEmail); // 插入或更新
});

final existingEmail = await isar.emails.get(newEmail.id!); // 获取

await isar.writeTxn(() {
  await isar.emails.delete(existingEmail.id!); // 删除
});

数据库查询

Isar 的查询语言允许使用索引、过滤对象、使用复杂数组逻辑,并且能够排序结果:

final importantEmails = isar.emails
  .where()
  .titleStartsWith('Important')
  .limit(10)
  .findAll();

final specificEmails = isar.emails
  .filter()
  .recipient((q) => q.nameEqualTo('David'))
  .or()
  .titleMatches('*university*', caseSensitive: false)
  .findAll();

4. 典型生态项目

Isar 社区支持多个生态项目,例如:

  • Isar Generator:用于生成 Isar 的数据模型代码。
  • Isar Flutter Libs:包含了 Isar 核心库。
  • Isar Inspector:一个实时检查 Isar 实例和集合的工具。

Isar 生态项目为开发者提供了丰富的工具和库,以简化数据库集成和使用过程。

isar Extremely fast, easy to use, and fully async NoSQL database for Flutter isar 项目地址: https://gitcode.com/gh_mirrors/isar/isar

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌桃莺Talia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值