HBDB โ Hybrid Binary Database
๐ Introduction
HBDB is a Hybrid Binary Database for Dart/Flutter that allows you to store three main types of data inside a single ".db" file:
- Map/JSON structured data using Adapters
- Binary files (PDF, images, audio, any file)
- Cover image (thumbnail)
It uses a custom binary format with a DB lock file, supports auto compact (clean-up), stream reading, typed boxes, and listeners for DB changes.
๐ Features
- Type-safe data storage using HBAdapter
<T> add,update,delete,query,getAll,getAllStream- File entry support with compression
- Cover image support (set, get, delete)
- Auto compaction of database
- Built-in listeners for database and box-level events
- Single-file database design
๐ง How to Use
1. Initialize Database
final db = HBDB.getInstance();
await db.open('mydata.db');
2. Create Adapter for Your Model
class User {
final String name;
final int autoId; // [`autoId`] database auto generated
User({required this.name, this.autoId = 0});
Map<String, dynamic> toMap() {
return <String, dynamic>{'name': name, 'autoId': autoId};
}
factory User.fromMap(Map<String, dynamic> map) {
return User(name: map['name'] as String, autoId: map['autoId'] as int);
}
@override
String toString() {
return name;
}
}
class UserAdapter extends HBAdapter<User> {
@override
int getUniqueFieldId() {
return 1;
}
@override
User fromMap(Map<String, dynamic> map) {
return User.fromMap(map);
}
@override
Map<String, dynamic> toMap(User value) {
return value.toMap();
}
}
3. Register Adapter
db.setAdapter<User>(UserAdapter());
4. Add Data
final id = await db.add<User>(User(name: 'Than'));
5. Read All
final users = await db.getAll<User>();
6. Update
await db.update<User>(id, User(name: 'New Name'));
7. Delete
await db.delete<User>(id);
๐ฆ Box API
Note About
autoIdFieldEvery model class used with TDB must include an
autoIdfield. This field will be automatically populated by the database during insertion.Example:
class User { final int autoId; // MUST exist โ TDB writes newId into this field final String name; User({ this.autoId = 0, required this.name }); }If
autoIdis missing:
- The database cannot assign a generated ID back into the object
- Update / Delete operations may not function correctly
- Querying by ID becomes impossible
TDBox<T> is a typed data container created automatically when you call db.setAdapter<T>().
It provides an easy, safe CRUD interface on top of the TDB core.
๐ง How Box Works Internally
A Box is connected to:
- the database instance (
TDB) - the registered adapter for type
T
When you call:
final box = db.getBox<User>();
TDB internally maps:
- adapter โ serialization
- box โ CRUD access by type
Each Box only accesses records that match its adapter's unique field ID.
(Similar to Hive)
TDB provides a simple Box API through TDBox<T>.
A Box is automatically created when you call db.setAdapter<T>().
Creating and Using a Box
final userBox = db.getBox<User>();
Add Data
final id = await userBox.add(User(name: 'Than'));
Read All
final users = await userBox.getAll();
Update
await userBox.update(id, User(name: 'New Name'));
Delete
await userBox.delete(id);
๐ File Handling
Add File
await db.addFile(File('test.pdf'));
Extract File
await db.extractFile('output/', entry);
Delete File
await db.deleteFile(entry);
๐ผ Cover Image
await db.setCover(imageBytes);
final data = await db.getCoverData();
await db.deleteCover();
๐งน Auto Compact
Database automatically rebuilds when deleted size or delete count exceeds limits. You can also manually compact:
await db.compact();
๐ Database Stream
Stream
// DB Cast
db.getAllStream<User>().listen((user) {
print('user: $user');
});
// Box
userBox.getAllStream().listen((user) {
print('user: $user');
});
Query
// db
User? user1 = await db.query<User>((value) => value.name == 'name');
// box
User? user = await userBox.query((value) => value.name == 'name');
// Stream
/// db
db.queryStream<User>((value) => value.name == 'name').listen((user) {
print('user: $user');
});
/// user
userBox.queryStream((value) => value.name == 'name').listen((user) {
print('user: $user');
});
๐ Listeners
class DBListener implements HBListener {
@override
void onHBDBChanged(HBListenerType type, int? adapterFieldId, int? autoId) {
print(
'[DB] Type: $type - adapterFieldId: $adapterFieldId - autoId: $autoId',
);
}
}
db.addListener(DBListener());
Type-specific box listeners:
final box = db.getBox<User>();
class UserBoxListener implements HBBoxListener {
@override
void onHBBoxChanged(HBBoxListenerType type, int? autoId) {
print('[User Box] Type: $type - autoId: $autoId');
}
}
userBox.addListener(UserBoxListener());
๐ฒ๐ฒ แแผแแบแแฌแแฌแแฌ Documentation
HBDB แแญแฏ Dart/Flutter แกแแฝแแบแแฑแธแแฌแธแแฒแท Hybrid Binary Database แแผแ แบแแผแฎแธ database file แแ แบแแฏแกแแฝแแบแธ
- JSON/Map data
- Binary File (PDF, Image, video, zip)
- Cover image
แแฝแฑแแญแฏ แกแแฏแแบแแฑแซแแบแธแแญแแบแธแแญแฏแทแแแซแแแบแ
DB lock, compact, stream reading, adapter system, listeners แแฝแฑแแฒแทแแญแฏแแฑแฌแท lightweight Hive + File system mix แแญแฏแกแแฏแถแธแแผแฏแแญแฏแทแแแซแแแบแ
๐ Feature แกแแปแแบแธแแปแฏแแบ
- Adapter แแผแแทแบ type-safe data
- add/update/delete/query/getAll/getAllStream
- File แแฝแฑแแแทแบ/แแแบ/แแผแแทแบแแญแฏแแบ
- Cover image แแแทแบ/แแแบแแพแฌแธแแญแฏแแบ
- Auto compact database clean-up
- Listener system included
- Database file แแ แบแแฏแแแบแธ
๐ Conclusion
HBDB is simple, fast, compact, and flexible for apps that need mixed JSON + File storage in one database.
Libraries
- hb_db
- Support for doing something awesome.