📚

Exploring Isar Database in Flutter

Aug 11, 2024

Flutter Original Show - Isar Database

Introduction

  • Host: Maud
  • Guest: Simon (Flutter engineer from Munich, Germany)
  • Topic: Isar, a NoSQL database package for Flutter
  • Recent updates to Isar demonstrated promising benchmarks.
  • Importance of databases in applications, especially for offline-first scenarios.

Guest Introduction

  • Simon's background in creating open-source packages related to databases (e.g., Hive, Isar).
  • Focus on making databases easy to use and performant.

Overview of Databases

Hive

  • Simple key-value database written in Dart.
  • Own storage format, but has limitations:
    • No support for multiple isolates.
    • Can be slow, especially at app startup.
    • Queries must be implemented in Dart, which can be inefficient.
    • Lacks indexing capabilities.

Isar

  • NoSQL database with advanced features.
  • Built for ease of use and performance.
  • Made up of three components:
    1. Isar Core: Backend written in Rust for performance.
    2. Isar Generator: Analyzes code at build time, ensuring type correctness.
    3. Isar Package: Lightweight runtime layer for database interaction.
  • Offers object builder style queries, reducing the likelihood of errors.

Features of Isar

  • Performance: Built with Rust for speed; benchmarks show good performance against competitors.
  • Storage Options: New version offers both native Isar engine and CQ light engine for interaction with SQL databases.
  • Queries: Simplified querying with static checks to avoid mistakes.
  • Encryption: Optional encryption using industry-standard protocols.
  • Multi-threading: Supports transactions to ensure consistent database state during operations.

Benchmarks

  • Benchmarking databases is complex and may not be fair due to varying capabilities.
  • Isar shows competitive performance in insertion and querying compared to other databases.

Implementation

Creating a User Object

  • Use a User class with properties like name, username, email, etc.
  • Annotate class with @Collection for Isar to recognize it as a collection.
  • Generate code using build_runner.

Opening an Instance

  • Use isar.open() to create a database instance, which requires a schema and directory path.
  • Important to handle async operations correctly to avoid UI blocking.

Using Transactions

  • Transactions are crucial for atomic operations and maintaining database integrity.
  • Required for write operations to avoid performance issues.

Querying

  • Provides generated methods for various query types.
  • Statically checked queries to prevent runtime errors.

Inspector Feature

  • A debugging tool to explore the schema and data in Isar.
  • Allows live updates and modifications to the database content.

Future Enhancements

  • Potential for adding adapters for SQL databases like PostgreSQL or MySQL.
  • Encourages community contribution and feedback to improve documentation and functionality.

Conclusion

  • Simon invites viewers to engage with the Isar community and contribute to improvements.
  • Acknowledges the importance of user feedback for enhancing documentation.
  • Host thanks Simon for the insights and encourages future visits for deeper dives into Isar.