lokv 1.0.1 lokv: ^1.0.1 copied to clipboard
LoKV is a platform-independent embedded key-value database that runs on desktops, mobile devices, web browsers, and any platform that has a Dart or JavaScript runtime.
import 'package:lokv/lokv.dart';
Future<int> main() async {
// Create a LoKV Database called lrig.
var lokv = VolatileLoKV.create('lrig');
// You need open the database before using it.
// If the database does not exist, it will be created.
await lokv.open();
// Put a record into the database.
await lokv.put('Suzuko Homura', 'Ril');
// Put another one.
await lokv.put('Hanna Mikage', 'Nanashi');
// Get the record we just put into.
var value = await lokv.get('Suzuko Homura');
assert(value == 'Ril');
value = await lokv.get('Hanna Mikage');
assert(value == 'Nanashi');
// If a record does not exist, it will return `null`.
value = await lokv.get('Chinatsu Morikawa');
assert(value == null);
// Put a value to an existing key will overwrite the existing value.
await lokv.put('Suzuko Homura', 'Mel');
value = await lokv.get('Suzuko Homura');
assert(value == 'Mel');
// Delete a record
await lokv.delete('Hanna Mikage');
value = await lokv.get('Hanna Mikage');
assert(value == null);
// Closing the database makes the database unreadable and unwritable,
// but does not cause data loss.
await lokv.close();
// After reopening, we can see that the previous data still exists.
await lokv.open();
value = await lokv.get('Suzuko Homura');
assert(value == 'Mel');
// We can use a write batch to complete multiple write operations,
// these operations either succeed together or fail together.
await lokv.write(WriteBatch()
..put('Kiyoi Mizushima', 'Piruluk')
..delete('Suzuko Homura')
..put('Chinatsu Morikawa', 'Mel'));
// Similarly, we can use a read batch to complete multiple read operations.
var values = await lokv.read(ReadBatch()
..get('Chinatsu Morikawa')
..get('Suzuko Homura')
..get('Kiyoi Mizushima'));
assert(values[0] == 'Mel');
assert(values[1] == null);
assert(values[2] == 'Piruluk');
// Drop a database will lose all the data,
// and free up the space used by the database.
await lokv.drop();
// You can open the database again,
// but this database is not the original one but newly created.
await lokv.open();
// We can see that the record we put before is gone.
value = await lokv.get('Kiyoi Mizushima');
assert(value == null);
// Remember to drop it when you no longer use a database.
await lokv.drop();
return 0;
}