Convert Figma logo to code with AI

agrosner logoDBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

4,850
595
4,850
45

Top Related Projects

12,591

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Realm is a mobile database: a replacement for SQLite & ORMs

Database for Android and JVM - first and fast, lightweight on-device vector database

2,610

Insanely easy way to work with Android Database.

3,125

requery - modern SQL based query & persistence for Java / Kotlin / Android

ORMLite Android functionality used in conjunction with ormlite-core

Quick Overview

DBFlow is a fast, powerful, and lightweight ORM (Object-Relational Mapping) Android database library. It simplifies database operations in Android applications by providing an intuitive API for defining models, querying data, and managing database migrations.

Pros

  • Fast and efficient, with minimal runtime impact
  • Extensive query language support, including complex joins and subqueries
  • Supports multiple databases and custom database wrappers
  • Robust migration system for easy schema updates

Cons

  • Steep learning curve for developers new to ORMs
  • Requires annotation processing, which can increase build times
  • Limited support for complex data types and relationships compared to some other ORMs
  • May be overkill for simple database operations in small projects

Code Examples

  1. Defining a model:
@Table(database = AppDatabase::class)
data class User(
    @PrimaryKey var id: Int = 0,
    @Column var name: String? = null,
    @Column var email: String? = null
)
  1. Inserting data:
val user = User(name = "John Doe", email = "john@example.com")
user.save()
  1. Querying data:
val users = (select from User::class
    where (User_Table.name.like("%John%"))
    and (User_Table.email.isNotNull))
    .queryList()
  1. Updating data:
(update<User>()
    set User_Table.name.eq("Jane Doe")
    where User_Table.id.eq(1))
    .execute()

Getting Started

  1. Add DBFlow to your project's build.gradle:
dependencies {
    def dbflow_version = "5.0.0-alpha1"
    kapt "com.github.agrosner.dbflow:processor:$dbflow_version"
    implementation "com.github.agrosner.dbflow:core:$dbflow_version"
    implementation "com.github.agrosner.dbflow:lib:$dbflow_version"
}
  1. Initialize DBFlow in your Application class:
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        FlowManager.init(this)
    }
}
  1. Define your database and models, then start using DBFlow in your app!

Competitor Comparisons

12,591

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Pros of greenDAO

  • Mature and well-established ORM with a large user base
  • Optimized for performance, especially with large datasets
  • Supports encryption out of the box

Cons of greenDAO

  • Less flexible query building compared to DBFlow
  • Requires more boilerplate code for entity definitions
  • Limited support for complex relationships between entities

Code Comparison

greenDAO entity definition:

@Entity
public class User {
    @Id private Long id;
    private String name;
    private int age;
}

DBFlow entity definition:

@Table(database = AppDatabase.class)
public class User extends BaseModel {
    @PrimaryKey private Long id;
    @Column private String name;
    @Column private int age;
}

Both ORMs offer similar functionality for defining entities, but DBFlow's syntax is more concise and uses a base model class for common operations. greenDAO requires separate generator code to create the DAO classes, while DBFlow generates them automatically.

In terms of querying, DBFlow provides a more fluent API:

List<User> users = SQLite.select()
    .from(User.class)
    .where(User_Table.age.greaterThan(18))
    .queryList();

greenDAO's query syntax is more verbose:

List<User> users = userDao.queryBuilder()
    .where(UserDao.Properties.Age.gt(18))
    .list();

Overall, DBFlow offers more flexibility and a more modern API, while greenDAO focuses on performance and stability.

Realm is a mobile database: a replacement for SQLite & ORMs

Pros of Realm

  • Faster query execution and overall performance
  • Built-in support for reactive programming and real-time data synchronization
  • Simpler API and less boilerplate code required

Cons of Realm

  • Larger APK size due to native libraries
  • Limited flexibility in database schema design
  • Steeper learning curve for developers familiar with SQLite-based ORMs

Code Comparison

Realm:

public class Person extends RealmObject {
    @PrimaryKey
    private String name;
    private int age;
    // Getters and setters
}

Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(r -> {
    Person person = r.createObject(Person.class, "John");
    person.setAge(30);
});

DBFlow:

@Table(database = AppDatabase.class)
public class Person extends BaseModel {
    @PrimaryKey
    String name;
    int age;
    // Getters and setters
}

Person person = new Person();
person.setName("John");
person.setAge(30);
person.save();

Both Realm and DBFlow are popular Android ORM libraries, but they have different approaches to database management. Realm uses its own database engine, while DBFlow is built on top of SQLite. The choice between them depends on specific project requirements and developer preferences.

Database for Android and JVM - first and fast, lightweight on-device vector database

Pros of ObjectBox

  • Higher performance and lower memory footprint
  • Built-in support for relations and queries
  • Simpler API with less boilerplate code

Cons of ObjectBox

  • Less flexible schema migration options
  • Smaller community and ecosystem compared to DBFlow
  • Limited support for custom SQL queries

Code Comparison

ObjectBox:

@Entity
public class User {
    @Id long id;
    String name;
    int age;
}

Box<User> userBox = store.boxFor(User.class);
userBox.put(new User(0, "John", 30));

DBFlow:

@Table(database = AppDatabase.class)
public class User extends BaseModel {
    @PrimaryKey(autoincrement = true)
    long id;
    @Column String name;
    @Column int age;
}

User user = new User();
user.name = "John";
user.age = 30;
user.save();

ObjectBox offers a more concise syntax for defining entities and performing database operations. DBFlow requires more annotations and explicit method calls for similar functionality.

2,610

Insanely easy way to work with Android Database.

Pros of Sugar

  • Simpler API with less boilerplate code
  • Easier to set up and use for beginners
  • Lightweight and minimal impact on app size

Cons of Sugar

  • Limited query capabilities compared to DBFlow
  • Fewer advanced features and customization options
  • Less active development and community support

Code Comparison

Sugar:

@Table
public class Book extends SugarRecord {
    String title;
    String author;
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

DBFlow:

@Table(database = AppDatabase.class)
public class Book extends BaseModel {
    @PrimaryKey(autoincrement = true)
    long id;
    
    @Column
    String title;
    
    @Column
    String author;
}

Both libraries aim to simplify database operations in Android apps, but they differ in complexity and features. Sugar focuses on simplicity and ease of use, making it suitable for smaller projects or developers new to database management. DBFlow offers more advanced features and customization options, making it better suited for larger, more complex applications that require fine-grained control over database operations.

3,125

requery - modern SQL based query & persistence for Java / Kotlin / Android

Pros of requery

  • Supports multiple database types (SQLite, MySQL, PostgreSQL, Oracle)
  • Offers reactive streams and RxJava integration
  • Provides a more flexible and powerful query API

Cons of requery

  • Steeper learning curve due to more complex API
  • Less active community and fewer resources compared to DBFlow
  • May require more boilerplate code for simple use cases

Code Comparison

requery:

@Entity
public abstract class Person {
    @Key @Generated
    int id;
    
    @Index("name_index")
    String name;
    
    int age;
}

DBFlow:

@Table(database = AppDatabase.class)
public class Person extends BaseModel {
    @PrimaryKey(autoincrement = true)
    int id;
    
    @Column
    @Index("name_index")
    String name;
    
    @Column
    int age;
}

Both libraries offer annotation-based entity definitions, but requery's approach is more concise and doesn't require extending a base class. DBFlow's syntax is more explicit in defining the database and column annotations.

requery provides a more flexible querying system and supports multiple database types, making it suitable for complex applications. However, DBFlow's simpler API and active community make it more approachable for beginners and smaller projects.

ORMLite Android functionality used in conjunction with ormlite-core

Pros of ORMLite

  • Mature and stable library with extensive documentation
  • Supports multiple database types beyond SQLite
  • Lightweight with minimal dependencies

Cons of ORMLite

  • Less performant for complex queries compared to newer alternatives
  • Requires more boilerplate code for database operations
  • Limited support for modern Android features and Kotlin

Code Comparison

ORMLite:

@DatabaseTable(tableName = "accounts")
public class Account {
    @DatabaseField(id = true)
    private String name;
    @DatabaseField
    private String password;
}

DBFlow:

@Table(database = AppDatabase::class)
class Account(
    @PrimaryKey var name: String = "",
    @Column var password: String = ""
)

DBFlow offers a more concise syntax, especially when using Kotlin, while ORMLite requires more verbose annotations and field declarations. DBFlow also provides better integration with modern Android development practices and Kotlin features.

While ORMLite is a reliable choice for simple database operations and cross-platform projects, DBFlow offers better performance, more Android-specific features, and a more modern API. However, ORMLite's maturity and extensive documentation may be advantageous for developers who prioritize stability and comprehensive resources.

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

DBFlow

JitPack.io

A Kotlin Multiplatform SQLite library. Annotate tables and databases; a compiler plugin generates adapters and a typed query DSL.

Targets: Android, JVM, iOS, macOS.

@Database(version = 1, tables = [User::class])
abstract class AppDatabase : DBFlowDatabase<AppDatabase>() {
    abstract val userAdapter: ModelAdapter<User>
}

@Table
data class User(
    @PrimaryKey(autoincrement = true) val id: Int = 0,
    val name: String,
)
import com.dbflow5.database.createDB

val db = createDB<AppDatabase>(context) { copy(name = "App") }

db.writableTransaction {
    userAdapter.save(User(name = "Ada"))
    val users = (userAdapter.select() where (User.name eq "Ada")).list()
}

What’s included

ModuleRole
libRuntime, query DSL, transactions, Flow observers
coreAnnotations and converters (pulled in by lib)
compiler + plugin com.dbflow5Table companions (adapters + column properties), *_Adapter helpers, *_Database
sqlcipherEncrypted Android databases
livedata / paging / reactive-streamsAndroid extras

Coroutines are built into lib. There is no separate coroutines artifact.

Docs

Usage lives under usage2/. The table of contents is in SUMMARY.md.

Published HTML: dbflow.gitbook.io/dbflow

Install

See Including in a project for version catalogs, the compiler plugin, and generated sources.

Quick start with a version catalog:

[plugins]
dbflow = { id = "com.dbflow5", version = "5.0.0-alpha2" }
plugins {
    kotlin("multiplatform")
    alias(libs.plugins.dbflow)
}

That is the whole setup. The plugin adds the com.dbflow5:lib dependency, the compiler plugin, the generated-sources directory on commonMain, -lsqlite3 linker opts for native binaries, and a dbflowGenerate task that keeps generated code in sync with your models.

Until the plugin is on Maven Central, use a composite build of this repo.

Contribute

  1. Match existing Kotlin style.
  2. Keep the change scoped to the issue.
  3. Open PRs against master.

Maintainers

Started at Raizlabs / Rightpoint. Maintained by agrosner.