Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.
/ kvsql Public archive

A type safe key/value store backed by Sqlite for Flutter

License

Notifications You must be signed in to change notification settings

synw/kvsql

Repository files navigation

Kvsql

pub package Build Status Coverage Status

A type safe key/value store for Flutter backed by Sqlite. Powered by Sqlcool.

Usage

Initialize

import 'package:kvsql/kvsql.dart';

store = KvStore();
await store.onReady;

Initialize with an existing Sqlcool database:

import 'package:kvsql/kvsql.dart';
import 'package:sqlcool/sqlcool.dart';

final db = Db();
await db.init(path: "mydb.db", schema=[kvSchema()]);
store = KvStore(db: db);

Insert or update

await store.put<String>("mykey", "myvalue");

Supported value types are: String, int, double, bool, List<T>, Map<K, V>

Allowed types for map keys are: String, int and double

Allowed types for lists and maps values are String, int, bool, double and dynamic

Delete

await store.delete("mykey");

Select

Returns a typed value

final List<int> myValue = await store.select<List<int>>("mykey");

Select sync

Synchronously select a value.

final Map<String, int> myValue = store.selectSync<Map<String, int>>("mykey");

For this to work you need to initialize the store with the inMemory option that keeps an in memory copy of the store values.

store = KvStore(inMemory = true);

Push

This method upserts a key/value using a queue: it can be safely called concurrently. Useful for high throughput updates.

store.push("mykey", "my_value");

Limitations:

  • This method is executed asynchronously but can not be awaited
  • It does not control the type of the data

Note: if you don't await your mutations or use push you are exposed to eventual consistency

Check the examples for detailled usage.

Persistant state

The kvstore can be used to persist the app state. Example with provider:

import 'package:flutter/foundation.dart';
import 'package:kvsql/kvsql.dart';

final stateStore =
    KvStore(inMemory: true, path: "stateStore.db");

class AppState with ChangeNotifier {
  int get value => stateStore.selectSync<int>("value");
  set value(int v) => stateStore.put<int>("value", v);

  void updateValue(int val) {
    value = val;
    notifyListeners();
  }
}