Skip to content

sambhav2358/TinyDB

Repository files navigation

TinyDB

API Latest Version Repo Size JitPack GitHub commits GitHub issues Forks Stars State Code Quality

TinyDB is a lightweight, high-performance database library for Android designed for effortless data persistence. Occupying less than 8KB, it provides a streamlined alternative to SharedPreferences with support for multiple database instances and real-time data observers.

Key Features

  • Ultra-Lightweight: Minimal impact on APK size (~5KB–8KB).
  • High Compatibility: Supports API level 19+ (covering 99.7%+ of Android devices).
  • Observer Pattern: Built-in ValueChangeListener to react to data modifications.
  • Multi-Instance Support: Maintain separate database files for different data scopes.
  • Fast Integration: Boilerplate-free implementation.

Implementation

1. Configure Repository

Add the JitPack repository to your settings.gradle file:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url '[https://jitpack.io](https://jitpack.io)' }
    }
}

2. Add Dependency

Add the following line to your app-level build.gradle:

dependencies {
    implementation 'com.github.sambhav2358:TinyDB:2.0.1'
}

3. Quick Start

Initialize Database You can use the default instance or create a custom named database for specific use cases.

// Default Instance
TinyDefaultDB defaultDB = TinyDB.getInstance().getDefaultDatabase(this);

// Custom Named Instance
TinyCustomDB customDB = TinyDB.getInstance().getCustomDatabase(this, "SettingsDB");

4. CRUD Operations

TinyDB simplifies data handling compared to the standard SharedPreferences.Editor workflow.

// Saving Data (Automatic background persistence)
defaultDB.putInt("user_score", 1024);
defaultDB.putString("username", "JohnDoe");

// Retrieving Data
int score = defaultDB.getInt("user_score", 0);

// Deleting Data
defaultDB.clearKey("user_score"); // Single key
defaultDB.clearAll();             // Entire database

5. Real-time Data Observation

Implement ValueChangeListener to observe changes across your application.

public class MainActivity extends AppCompatActivity implements ValueChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        TinyDefaultDB db = TinyDB.getInstance().getDefaultDatabase(this);
        db.setValueChangeListener(this);
    }

    @Override
    public <E> void onValueAdded(String key, E value, String dbName) {
        // Triggered on putX() methods
    }

    @Override
    public void onKeyRemoved(String key, String dbName) {
        // Triggered on clearKey()
    }

    @Override
    public void onAllKeysRemoved(String dbName) {
        // Triggered on clearAll()
    }
}

6. Supported Data Types


Primitives: Integer, Boolean, Float, String

Collections: List

Generic Objects: Use put() and get() for custom object serialization.

7. Comparison: TinyDB vs. SharedPreferences

TinyDB removes the need for manual .edit() and .apply() calls, significantly reducing boilerplate code.

Feature Standard SharedPreferences TinyDB
Code Verbosity High (Requires Editor) Minimal (Direct API)
Change Listeners Manual/Complex Native Interface
Multiple Files Requires manual management Instance-based
Learning Curve Moderate Low

8. Contributions & Support

Feature Requests: Please open an Issue describing your proposed feature.

Sample App: View the MainActivity.java for a full implementation example.

Credits

Special thanks to the Paper project for inspiration.

License

Free to use and modify under the terms of the project's license.


Built with ❤️ by Sambhav Khandelwal