๐๏ธ Offline First Core
High-performance LMDB-based local storage library optimized for FFI integration with Flutter and cross-platform applications.
โจ Features
- ๐ LMDB-powered - Battle-tested database engine used by Bitcoin Core and OpenLDAP
- ๐ฑ Flutter-ready - Hot restart compatible FFI interface
- โก High performance - Zero-copy reads and ACID transactions
- ๐ Cross-platform - Works on iOS, Android, Windows, macOS, and Linux
- ๐ฆ Simple API - Only 9 functions to learn
๐ Quick Start
Flutter Integration
import 'dart:ffi';
import 'dart:convert';
// 1. Load the native library
final dylib = DynamicLibrary.open('liboffline_first_core.so');
// 2. Define FFI functions
typedef CreateDbNative = Pointer Function(Pointer<Utf8>);
typedef CreateDb = Pointer Function(Pointer<Utf8>);
final createDb = dylib.lookupFunction<CreateDbNative, CreateDb>('create_db');
// 3. Use the database
final dbPointer = createDb("my_app_database".toNativeUtf8());
final jsonData = jsonEncode({
"id": "user_123",
"hash": "content_hash",
"data": {"name": "John Doe", "email": "john@example.com"}
});
final result = pushData(dbPointer, jsonData.toNativeUtf8());
Rust Direct Usage
use ;
use json;
๐ API Reference
Core Functions
| Function | Rust | FFI | Description |
|---|---|---|---|
| Initialize | AppDbState::init(name) |
create_db(name) |
Create or open database |
| Insert | db.push(model) |
push_data(db, json) |
Add new record |
| Get by ID | db.get_by_id(id) |
get_by_id(db, id) |
Retrieve specific record |
| Get All | db.get() |
get_all(db) |
Retrieve all records |
| Update | db.update(model) |
update_data(db, json) |
Update existing record |
| Delete | db.delete_by_id(id) |
delete_by_id(db, id) |
Remove record |
| Clear | db.clear_all_records() |
clear_all_records(db) |
Remove all records |
| Reset | db.reset_database(name) |
reset_database(db, name) |
Reset database |
| Close | db.close_database() |
close_database(db) |
Close connection |
Data Model
๐ฏ Usage Examples
1. User Preferences
use ;
use json;
2. Shopping Cart
3. Offline Cache
๐ง Advanced Usage
Error Handling
use ;
match db.push
Batch Operations
Hot Restart (Flutter)
class DatabaseManager {
static Pointer? _dbPointer;
static void initDatabase() {
_dbPointer = createDb("my_app_db".toNativeUtf8());
}
static void closeDatabase() {
if (_dbPointer != null) {
closeDatabase(_dbPointer!);
_dbPointer = null;
}
}
}
๐ ๏ธ Setup
Installation
Add to your Cargo.toml:
[]
= "0.3.0"
= { = "1.0", = ["derive"] }
= "1.0"
For FFI projects:
[]
= "my_storage_lib"
= ["staticlib", "cdylib"]
Building
# Debug build
# Release build
# Cross-platform builds
โ ๏ธ Important Notes
LMDB Limitations
// โ Empty IDs not supported
let invalid = LocalDbModel ;
// โ
Always use non-empty IDs
let valid = LocalDbModel ;
Memory Safety (FFI)
// โ
Always check null pointers
void* db = ;
if
// โ
Free returned strings
const char* result = ;
if
Performance Tips
// โ
DO: Reuse connections
let db = init?;
for i in 0..1000
// โ DON'T: Create new connections
for i in 0..1000
๐งช Testing
๐ฆ Integration Examples
Flutter Plugin
// pubspec.yaml
dependencies:
ffi: ^2.0.0
// lib/database.dart
import 'dart:ffi';
import 'dart:io';
class NativeDatabase {
late DynamicLibrary _lib;
NativeDatabase() {
if (Platform.isAndroid) {
_lib = DynamicLibrary.open('liboffline_first_core.so');
} else if (Platform.isIOS) {
_lib = DynamicLibrary.process();
}
}
// Define your FFI functions here...
}
React Native
// Install react-native-ffi
npm install react-native-ffi
// Use the library
import from 'react-native';
const = NativeModules;
๐ Changelog
v0.3.0 - LMDB Migration
- โจ Migrated from redb to LMDB
- โจ Added
close_database()function - ๐ Fixed Flutter hot restart issues
- ๐ง Improved error handling
- ๐ Added comprehensive test suite (60+ tests)
v0.2.0 - Feature Expansion
- โจ Added
clear_all_records()andreset_database() - ๐ง Improved error handling
- ๐ก๏ธ Enhanced FFI safety
v0.1.0 - Initial Release
- โจ Basic CRUD operations
- ๐ FFI interface for cross-platform integration
๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- LMDB team for the robust database engine
- redb project for inspiration
- Rust community for excellent FFI tools
Made with โค๏ธ for developers who need reliable offline storage
โญ Star on GitHub โข ๐ฆ View on Crates.io โข ๐ Read the Docs