Skip to main content

hb_db 1.2.2 copy "hb_db: ^1.2.2" to clipboard
hb_db: ^1.2.2 copied to clipboard

HBDB is a **Hybrid Binary Database** for Dart/Flutter that allows you to store three main types of data inside a single ".db" file

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 autoId Field

Every model class used with TDB must include an autoId field. 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 autoId is 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.


0
likes
0
points
33
downloads

Publisher

unverified uploader

Weekly Downloads

HBDB is a **Hybrid Binary Database** for Dart/Flutter that allows you to store three main types of data inside a single ".db" file

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on hb_db