Skip to content

Commit c0cdc7e

Browse files
committed
test and web support
1 parent 3df2529 commit c0cdc7e

15 files changed

+1287
-536
lines changed

example/example.dart

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,40 @@ class HomePage extends StatefulWidget {
3232

3333
class _HomePageState extends State<HomePage> {
3434
String userID = 'user-123';
35+
bool _connectionTest = false;
36+
37+
@override
38+
void initState() {
39+
super.initState();
40+
_testConnectionAfterHotReload();
41+
}
42+
43+
/// Test database connection after hot reload
44+
Future<void> _testConnectionAfterHotReload() async {
45+
try {
46+
// Test database connection immediately after hot reload
47+
final result = await LocalDB.GetById(userID);
48+
result.when(
49+
ok: (data) {
50+
setState(() {
51+
_connectionTest = true;
52+
});
53+
print('✅ Hot reload test: Database connection is working');
54+
},
55+
err: (error) {
56+
setState(() {
57+
_connectionTest = true;
58+
});
59+
print('⚠️ Hot reload test: $error');
60+
},
61+
);
62+
} catch (e) {
63+
print('❌ Hot reload test failed: $e');
64+
setState(() {
65+
_connectionTest = true;
66+
});
67+
}
68+
}
3569

3670
Future<void> _createUser() async {
3771
final result = await LocalDB.Post(userID, {
@@ -70,7 +104,19 @@ class _HomePageState extends State<HomePage> {
70104
@override
71105
Widget build(BuildContext context) {
72106
return Scaffold(
73-
appBar: AppBar(title: const Text('Local DB Example')),
107+
appBar: AppBar(
108+
title: const Text('Local DB Example'),
109+
actions: [
110+
// Hot reload connection indicator
111+
Container(
112+
margin: const EdgeInsets.only(right: 16),
113+
child: Icon(
114+
_connectionTest ? Icons.check_circle : Icons.hourglass_empty,
115+
color: _connectionTest ? Colors.green : Colors.orange,
116+
),
117+
),
118+
],
119+
),
74120
body: Padding(
75121
padding: const EdgeInsets.all(16.0),
76122
child: FutureBuilder(

lib/flutter_local_db.dart

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
///
33
/// Provides unified database operations across all platforms:
44
/// - **Native platforms** (Android, iOS, macOS): Rust + LMDB via FFI
5-
/// - **Web platform**: In-memory storage with localStorage persistence
5+
/// - **Web platform**: IndexedDB for persistent storage
66
///
77
/// Features:
88
/// - ✅ Unified API across all platforms
99
/// - ✅ High-performance native backend (10,000+ ops/sec)
10-
/// - ✅ Web-optimized in-memory storage
10+
/// - ✅ Web-optimized IndexedDB storage (persistent, non-volatile)
1111
/// - ✅ Result-based error handling
1212
/// - ✅ JSON-serializable data storage
1313
/// - ✅ Hot restart support
@@ -19,16 +19,35 @@
1919
/// // Initialize database
2020
/// await LocalDB.init();
2121
///
22-
/// // Create a record
23-
/// final result = await LocalDB.Post('user-123', {
22+
/// // ✅ CREATE a new record (fails if ID already exists)
23+
/// final createResult = await LocalDB.Post('user-123', {
2424
/// 'name': 'John Doe',
2525
/// 'email': 'john@example.com',
2626
/// 'age': 30
2727
/// });
2828
///
29-
/// result.when(
30-
/// ok: (model) => print('Created user: ${model.id}'),
31-
/// err: (error) => print('Error: ${error.message}')
29+
/// createResult.when(
30+
/// ok: (model) => print('✅ New user created: ${model.id}'),
31+
/// err: (error) {
32+
/// if (error.message.contains('already exists')) {
33+
/// print('❌ User exists! Use Put to update instead.');
34+
/// }
35+
/// }
36+
/// );
37+
///
38+
/// // ✅ UPDATE existing record (fails if ID doesn't exist)
39+
/// final updateResult = await LocalDB.Put('user-123', {
40+
/// 'name': 'John Smith', // Updated name
41+
/// 'age': 31 // Updated age
42+
/// });
43+
///
44+
/// updateResult.when(
45+
/// ok: (model) => print('✅ User updated: ${model.id}'),
46+
/// err: (error) {
47+
/// if (error.message.contains('does not exist')) {
48+
/// print('❌ User not found! Use Post to create first.');
49+
/// }
50+
/// }
3251
/// );
3352
///
3453
/// // Retrieve the record

lib/src/database_factory.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'core/log.dart';
66
// Conditional imports for platform-specific implementations
77
import 'platform/database_stub.dart'
88
if (dart.library.io) 'platform/database_io.dart'
9-
if (dart.library.js_interop) 'platform/database_web.dart';
9+
if (dart.library.js) 'platform/database_web.dart';
1010

1111
/// Factory class for creating platform-appropriate database instances
1212
///
@@ -178,7 +178,7 @@ class DatabaseFactory {
178178
backend = 'Rust + LMDB via FFI';
179179
} else if (implementation.contains('Web')) {
180180
platform = 'Web Browser';
181-
backend = 'In-memory + localStorage';
181+
backend = 'IndexedDB (persistent)';
182182
} else {
183183
platform = 'Unknown';
184184
backend = 'Unknown';

0 commit comments

Comments
 (0)