Convert Figma logo to code with AI

simolus3 logodrift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.

2,540
362
2,540
149

Top Related Projects

Flutter database for super-fast Dart object persistence

4,067

Lightweight and blazing fast key-value database written in pure Dart.

2,855

SQLite flutter plugin

Quick Overview

Drift is a reactive persistence library for Dart and Flutter applications. It provides a type-safe and efficient way to work with SQLite databases, offering features like database migrations, custom queries, and reactive streams for data changes.

Pros

  • Type-safe database operations with code generation
  • Reactive streams for real-time data updates
  • Support for complex queries and custom SQL statements
  • Seamless integration with Flutter and Dart applications

Cons

  • Limited to SQLite databases only
  • Learning curve for developers new to reactive programming
  • Requires additional setup and code generation steps
  • May have performance overhead for very large datasets

Code Examples

  1. Defining a table:
class Todos extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(min: 6, max: 32)();
  BoolColumn get completed => boolean().withDefault(const Constant(false))();
}
  1. Querying data:
Future<List<Todo>> getCompletedTodos() {
  return (select(todos)..where((t) => t.completed.equals(true))).get();
}
  1. Inserting data:
Future<int> createTodo(TodosCompanion entry) {
  return into(todos).insert(entry);
}
  1. Watching data changes:
Stream<List<Todo>> watchAllTodos() {
  return select(todos).watch();
}

Getting Started

  1. Add drift to your pubspec.yaml:
dependencies:
  drift: ^2.8.0
dev_dependencies:
  drift_dev: ^2.8.0
  build_runner: ^2.4.0
  1. Define your database structure:
import 'package:drift/drift.dart';

part 'database.g.dart';

class Todos extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
  BoolColumn get completed => boolean()();
}

@DriftDatabase(tables: [Todos])
class MyDatabase extends _$MyDatabase {
  MyDatabase(QueryExecutor e) : super(e);

  @override
  int get schemaVersion => 1;
}
  1. Run code generation:
dart run build_runner build
  1. Use the generated database class in your application:
final database = MyDatabase(NativeDatabase.memory());

Competitor Comparisons

Flutter database for super-fast Dart object persistence

Pros of ObjectBox

  • High performance with native C++ core, optimized for mobile and IoT devices
  • Supports relations and queries across multiple entities
  • Offers automatic schema migration

Cons of ObjectBox

  • Less flexible schema definition compared to Drift
  • Limited SQL support, primarily focused on object-oriented database operations
  • Steeper learning curve for developers familiar with SQL-based ORMs

Code Comparison

ObjectBox:

@Entity()
class Person {
  int id;
  String name;
  int age;

  Person({this.id = 0, required this.name, required this.age});
}

Drift:

class People extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get name => text()();
  IntColumn get age => integer()();
}

ObjectBox uses annotations to define entities, while Drift uses a more SQL-like approach with table definitions. ObjectBox's syntax is more concise, but Drift offers greater flexibility in schema definition.

Both libraries provide type-safe query APIs, but their approaches differ. ObjectBox focuses on object-oriented queries, while Drift offers a more SQL-like query builder. The choice between the two depends on the specific project requirements, performance needs, and developer preferences.

4,067

Lightweight and blazing fast key-value database written in pure Dart.

Pros of Hive

  • Offers encryption for secure data storage
  • Supports custom objects and lists, allowing for more complex data structures
  • Provides a more user-friendly API with less boilerplate code

Cons of Hive

  • Limited query capabilities compared to Drift's SQL support
  • Lacks built-in migration support, which Drift offers
  • May have slower performance for complex queries on large datasets

Code Comparison

Hive example:

var box = Hive.box('myBox');
await box.put('key', 'value');
var value = box.get('key');

Drift example:

@DataClassName('Todo')
class Todos extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(min: 6, max: 32)();
  BoolColumn get completed => boolean().withDefault(const Constant(false))();
}

Both libraries offer efficient local storage solutions for Flutter applications, but they cater to different use cases. Hive excels in simplicity and ease of use, while Drift provides more advanced querying capabilities and better support for complex database schemas.

2,855

SQLite flutter plugin

Pros of sqflite

  • Direct SQLite integration, providing native performance for mobile apps
  • Simpler setup and usage for basic SQLite operations
  • Wider adoption and longer history in the Flutter ecosystem

Cons of sqflite

  • Requires manual SQL query writing, which can be error-prone
  • Lacks type-safety and compile-time checks for database operations
  • Limited support for complex queries and database migrations

Code Comparison

sqflite:

final db = await openDatabase('my_db.db');
await db.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
await db.insert('users', {'name': 'John'});
final List<Map> users = await db.query('users');

drift:

@DriftDatabase(tables: [Users])
class MyDatabase extends _$MyDatabase {
  MyDatabase(QueryExecutor e) : super(e);
  Future<List<User>> getAllUsers() => select(users).get();
}

drift offers a more type-safe and declarative approach to database operations, while sqflite provides a lower-level interface closer to raw SQL. drift's code generation reduces boilerplate and potential runtime errors, but sqflite's simplicity may be preferred for smaller projects or developers more comfortable with direct SQL manipulation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Drift

Build Status Using melos

CoreGenerator
Main versionGenerator version

Drift is a reactive persistence library for Flutter and Dart, built on top of SQLite. Drift is

  • Flexible: Drift lets you write queries in both SQL and Dart, providing fluent apis for both languages. You can filter and order results or use joins to run queries on multiple tables. You can even use complex SQL features like WITH and WINDOW clauses.
  • 🔥 Feature rich: Drift has builtin support for transactions, schema migrations, complex filters and expressions, batched updates and joins. We even have a builtin IDE for SQL!
  • 📦 Modular: Thanks to builtin support for daos and imports in SQL files, drift helps you keep your database code simple.
  • 🛡️ Safe: Drift generates type-safe code based on your tables and queries. If you make a mistake in your queries, drift will find it at compile time and provide helpful and descriptive lints.
  • ⚡ Fast: Even though drift lets you write powerful queries, it can keep up with the performance of key-value stores like shared preferences and Hive. Drift is the only major persistence library with builtin threading support, allowing you to run database code across isolates with zero additional effort.
  • Reactive: Turn any SQL query into an auto-updating stream! This includes complex queries across many tables
  • ⚙️ Cross-Platform support: Drift works on Android, iOS, macOS, Windows, Linux and the web. This template is a Flutter todo app that works on all platforms.
  • 🗡️ Battle tested and production ready: Drift is stable and well tested with a wide range of unit and integration tests. It powers production Flutter apps.

With drift, persistence on Flutter is fun!

To start using drift, read our detailed docs.

If you have any questions, feedback or ideas, feel free to create an issue. If you enjoy this project, I'd appreciate your 🌟 on GitHub.

Sponsors

Drift is proudly Sponsored by Stream 💙


Try the Flutter Chat Tutorial  ðŸ’¬

Working on this project

This repository contains a number of packages making up the drift project, most notably:

  • drift: The main runtime for drift, which provides most APIs.
  • drift_dev: The compiler for drift tables, databases and daos. It also contains a fully-featured SQL IDE for the Dart analyzer.
  • sqlparser: A SQL parser and static analyzer, written in pure Dart. This package can be used without drift to perform analysis on SQL statements. It's on pub at sqlparser

We use melos to manage the different packages in this repository.

You can install it with dart pub global activate melos. To activate it in this repository, run dart pub get in this directory followed by melos bootstrap.