{"id":19648950,"url":"https://github.com/frideosapps/frideos_kvprx","last_synced_at":"2025-04-28T16:30:32.449Z","repository":{"id":56830829,"uuid":"184459992","full_name":"frideosapps/frideos_kvprx","owner":"frideosapps","description":"A library with persistent and reactive classes, and key/value pairs storing helpers, using the sqflite plugin.","archived":false,"fork":false,"pushed_at":"2019-06-21T08:06:53.000Z","size":88,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T09:34:01.348Z","etag":null,"topics":["database","dbprovider","flutter","flutter-library","flutter-package","frideos","frideos-kvprx","pairs-stored","reactive-programming","sqflite","sqflite-database","sqlite","streams"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frideosapps.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-01T18:12:27.000Z","updated_at":"2021-01-31T09:00:53.000Z","dependencies_parsed_at":"2022-09-02T04:00:31.897Z","dependency_job_id":null,"html_url":"https://github.com/frideosapps/frideos_kvprx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frideosapps%2Ffrideos_kvprx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frideosapps%2Ffrideos_kvprx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frideosapps%2Ffrideos_kvprx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frideosapps%2Ffrideos_kvprx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frideosapps","download_url":"https://codeload.github.com/frideosapps/frideos_kvprx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251345735,"owners_count":21574766,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","dbprovider","flutter","flutter-library","flutter-package","frideos","frideos-kvprx","pairs-stored","reactive-programming","sqflite","sqflite-database","sqlite","streams"],"created_at":"2024-11-11T14:50:58.537Z","updated_at":"2025-04-28T16:30:31.649Z","avatar_url":"https://github.com/frideosapps.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# frideos_kvprx [![pub package](https://img.shields.io/pub/v/frideos_kvprx.svg)](https://pub.dartlang.org/packages/frideos_kvprx)\n\n**frideos_kvprx** is a library that offers persistent and reactive classes, and helpers to easily store key/value pairs in the database, using the sqflite plugin.\n\n- **DbProvider** : class used to inizialize the database.\n- **PersistentValue and derived classes** : classes derived from the `StreamedValue` class of the [frideos_core](https://pub.dartlang.org/packages/frideos_core) package to make a key/value pair persistent and reactive using streams. Used along with the `StreamBuilder` or the  `ValueBuilder` widget of the [frideos](https://pub.dartlang.org/packages/frideos) package, make it possible to update the UI with the last value on the stream and save the value to the database every time a new value is set.\n- **KeyValue** : class to handle a key/value pair.\n- **KeyValueProvider** : class with methods to get, insert, update, delete the key/value pairs stored in the db.\n- **KeyValueMetaProvider** : similar to the `KeyValueProvider`, but it is possible add a tag to every key/value pair.\n\n\n## DbProvider\n\n### Initialization\n```dart\n// Initialize a database.\n// By default the database name is set to 'appdb.db'.\nfinal dbProvider = DbProvider(databaseName: 'test.db');\n\n// Init the dabase\nawait dbProvider.init();  \n```\n\n### Close the connection\n```dart\nawait dbProvider.close();  \n```\n\n### Delete the current database\n```dart\nawait dbProvider.deleteDatabase();\n```\n\n### Truncate a table of the database\n```dart\nawait dbProvider.truncate('tableName');\n```\n\n## PersistentValues\nThese classes derive from the `PersistentValue` class that extends the `StreamedValue` class of the frideos package in order to take advantages of the streams, so that, every time a new value is set, this is both stored in the database (if the flag `continuousSave` is set to `true` ) and sent to the stream to drive a `StreamBuilder` or a `ValueBuilder` to update the UI.\n\n\n#### - PersistentString\n#### - PersistentBoolean\n#### - PersistentInt\n#### - PersistentDouble\n\n### Usage:\n\n#### 1 - Declare an instance of a `PersistentValue` derived object\n\n- `table` is set by default to 'kvp', so that if not specified,\nall the instances of a `PersistentValue` derived class, will store their values to the same table. \n\n- `persistentKey` is the key associated to the `PersistentValue` derived entity.\n\n- `initialData` is used when no record is found in the database to make an insert with the first key/value pair having as key the argument passed to the `persistentKey` parameter and for value the argument passed to the `initialData` parameter. \n\n- `continuousSave` is set by default to `true`, so that every time to the `persistentString` is given a new value, the record in the db will be updated. If set to `false`, to update the record in the db, it is necessary to call the `save` method.\n\n\n```dart    \n  final persistentString = PersistentString(\n      table: 'kvprx',      \n      persistentKey: 'persistentValueString',\n      initialData: 'ABCDEFGHIJKLMNOPRQSTUVWXY',\n      continuousSave: true);\n```\n#### 2 - Initialization\n```dart\n// Initialize the dbProvider\nawait dbProvider.init();\n\n// Call the init method of the `PersistentString` class to initialize\n// the KeyValueProvider, check if a record is already present in the db\n// and set it as the current value. If no record is found, the argument passed\n// to the `initialData` parameters is used to insert a new record in the db.\nawait persistentString.init(dbProvider: dbProvider);\n```\n\n\n#### 3 - Change the value\n```dart\n// Every time a new value is set, this will be sent to stream and stored \n// in the database.\npersistentString.value = 'New String';\n\n// Only if `continuousSave` is set to `false` it is necessary to call\n// the `save` method in order to update the record in the db.\n// await persistentString.save();\n```\n\n\n## KeyValueProvider\nBy default the table name is set to 'kvp'. It is important to notice that if more `KeyValueProvider` are created with the default table name, both the `getAll` and `truncate` method will affects all the records. To avoid this behavior, use the `table` paramater to give to each provider a different table name.\n\n#### Initialization\n```dart\n    final dbProvider = DbProvider(databaseName: 'test.db');\n    await dbProvider.init();\n\n    final kvpDb = KeyValueProvider(dbProvider: dbProvider);\n    var init = await kvpDb.init();\n```\n\n### INSERT\n\n#### - Insert by insert method\n```dart\n    final kvpTest1 = KeyValue(key: 'testKeyValue', value: 'test value');\n    final kvpTest2 = KeyValue(key: 'testKeyValue2', value: 'test value2');\n\n    // INSERT\n    await kvpDb.insert(kvpTest1);\n    await kvpDb.insert(kvpTest2);\n```\n\n#### - Insert by insertKeyValue method\n\n```dart\n    await kvpDb.insertKeyValue('key', 'value')\n```\n\n#### - Bulk insert\n\n\n```dart\n final kvps = [\n      KeyValue(key: 'testKeyValue1', value: 'test value1'),\n      KeyValue(key: 'testKeyValue2', value: 'test value2'),\n      KeyValue(key: 'testKeyValue3', value: 'test value3'),\n      KeyValue(key: 'testKeyValue4', value: 'test value4'),\n      KeyValue(key: 'testKeyValue5', value: 'test value5'),\n      KeyValue(key: 'testKeyValue6', value: 'test value6'),\n    ];\n\n  await kvpDb.bulkInsert(kvps);  \n```\n\n\n#### - Insert a map\nGiven a `map` of type `\u003cString, String\u003e`, this method save all\nof its key/value pairs in the database.\n\n```dart\nfinal map = {'1': 'a', '2': 'b', '3': 'c', '4': 'd'};\n\n// INSERT\nawait kvpDb.insertMap(map);\n```\n\n### GET\n\n#### - Get all key/value pairs\nGet all the key/value pairs stored in the table. It is important \nto notice that if more `KeyValueProvider` share the same table\n(by default is set to 'kvp'), this method will get the ones created\nwith other providers. To avoid this behavior, use the `table` parameter\nto specify a different table.\n\n```dart\n    // GET ALL\n    final pairs = await kvpDb.getAll();\n```\n\n#### - Get single kvp by getKeyValue method\n```dart\nfinal kvpTest1 = KeyValue(key: 'testKeyValue', value: 'test value');\nvar kvp1 = await kvpDb.getKeyValue(kvpTest1);\n```\n\n#### - Get single kvp by getById method\n```dart\nvar kvp1 = await kvpDb.getById(id);\n```\n\n#### - Get single kvp by getByKey method\n```dart\nvar kvp1 = await kvpDb.getByKey('key');\n```\n\n### UPDATE\n\n#### - Update single kvp by update method\n```dart\nvar kvp1 = await kvpDb.getByKey('key');\nawait kvpDb.update(kvp1, 'newValue');\n```\n#### - Update single kvp by updateById method\n```dart\nawait kvpDb.updateById(id, 'newValue');\n```\n#### - Update single kvp by updateByKey method\n```dart\nawait kvpDb.updateByKey('key', 'newValue');\n```\n\n#### - Bulk update\n```dart\n\nfinal kvps = [\n      KeyValue(key: 'testKeyValue1', value: 'test value1'),\n      KeyValue(key: 'testKeyValue2', value: 'test value2'),\n      KeyValue(key: 'testKeyValue3', value: 'test value3'),\n      KeyValue(key: 'testKeyValue4', value: 'test value4'),\n      KeyValue(key: 'testKeyValue5', value: 'test value5'),\n      KeyValue(key: 'testKeyValue6', value: 'test value6'),\n    ];\n\n    // INSERT\n    await kvpDb.bulkInsert(kvps);\n\n    // GET ALL\n    var pairs = await kvpDb.getAll();\n    \n    // Change the value of each kvp\n    var updatedKvps = List\u003cKeyValue\u003e();\n    pairs.forEach((p) =\u003e updatedKvps\n        .add(KeyValue(key: p.key, value: 'newValue ${pairs.indexOf(p)}')));\n\n    // UPDATE\n    await kvpDb.bulkUpdate(updatedKvps);\n```\n\n\n### DELETE\n\n#### - Delete single kvp by delete method\n```dart\nvar kvp1 = await kvpDb.getByKey('key');\nawait kvpDb.delete(kvp1);\n```\n#### - Delete single kvp by deleteById method\n```dart\nawait kvpDb.deleteById(id);\n```\n#### - Delete single kvp by deleteByKey method\n```dart\nawait kvpDb.deleteByKey('key');\n```\n#### - Bulk delete keys starting with a given prefix\n```dart\nfinal kvps = [\n      KeyValue(key: 'prefix_Value1', value: 'test value1'),\n      KeyValue(key: 'prefix_Value2', value: 'test value2'),\n      KeyValue(key: 'Prefix2Value3', value: 'test value3'),\n      KeyValue(key: 'Prefix2Value4', value: 'test value4'),\n      KeyValue(key: 'Value5', value: 'test value5'),\n    ];\n\n// INSERT\nawait kvpDb.bulkInsert(kvps);\n\n// Bulk delete (2 out of 5)\nawait kvpDb.bulkDeleteKeysStartWith('prefix');\n```\n\n#### - Bulk delete a list of kvp\n```dart\nfinal kvps = [\n      KeyValue(key: 'testKeyValue1', value: 'test value1'),\n      KeyValue(key: 'testKeyValue2', value: 'test value2'),\n      KeyValue(key: 'testKeyValue3', value: 'test value3'),\n      KeyValue(key: 'testKeyValue4', value: 'test value4'),\n      KeyValue(key: 'testKeyValue5', value: 'test value5'),\n      KeyValue(key: 'testKeyValue6', value: 'test value6'),\n    ];\n\n// INSERT\nawait kvpDb.bulkInsert(kvps);\n\n// Bulk delete (3 out of 6)\nawait kvpDb.bulkDeleteKeys(['testKeyValue3', 'testKeyValue4','testKeyValue5']);\n```\n\n\n\n\n### TRUNCATE\n\nTo delete all the records in the table. It is important to notice\nthat if more provider used the same table (set by defalt to 'kvp')\nthis method will delete even the key/value pairs created with the other providers. To avoid this behavior, initialize the `KeyValueProvider` giving to the `table` parameter a different value for each provider.\n\n```dart\nawait kvpDb.truncate();\n```\n\n\n## KeyValueMetaProvider\n\nBy default the table name is set to 'kvpmeta'. It is important to notice that if more `KeyValueMetaProvider` are created with the default table name, both the `getAll` and `truncate` method will affects all the records. To avoid this behavior, use the `table` paramater to give to each provider a different table name.\n\nIt has most of the methods of the `KeyValueProvider` class. When needed, it uses the `KeyValueMeta` model class instead. \n\nE.g. insert two kvpmeta:\n\n```dart\n    final kvpTest1 = KeyValueMeta(key: 'testKeyValue', value: 'test value', meta: 'meta');\n    final kvpTest2 = KeyValueMeta(key: 'testKeyValue2', value: 'test value2', meta: 'meta2');   \n\n    // INSERT\n    await kvpDb.insert(kvpTest1);\n    await kvpDb.insert(kvpTest2);\n```\n\n\n\n#### Initialization\n```dart\n    final dbProvider = DbProvider(databaseName: 'test.db');\n    await dbProvider.init();\n\n    final kvpDb = KeyValueMetaProvider(dbProvider: dbProvider);\n    var init = await kvpDb.init();\n```\n\n\n#### - Get a list of kvp by getByMeta\n```dart\n    await kvpDb.insertKeyValueMeta('key1', 'value1', 'meta1');\n    await kvpDb.insertKeyValueMeta('key2', 'value2', 'meta1');\n    await kvpDb.insertKeyValueMeta('key3', 'value3', 'meta1');\n    await kvpDb.insertKeyValueMeta('key4', 'value1', 'meta2');\n    await kvpDb.insertKeyValueMeta('key5', 'value2', 'meta2');\n    await kvpDb.insertKeyValueMeta('key6', 'value3', 'meta2');\n\n    // Get all the kvp with the `meta` parameter equal to 'meta2'\n    final pairs = await kvpDb.getByMeta('meta2');\n```\n\n\n#### - Update the meta parameter of a kvp\n```dart\n    await kvpDb.insertKeyValueMeta('key1', 'value1', 'meta1');\n\n    // change the `meta` parameter from 'meta1' to 'meta2'\n    await kvpDb.updateMeta('key1', 'meta2');\n```\n\n#### - Bulk update meta\n```dart\n    await kvpDb.insertKeyValueMeta('key1', 'value1', 'meta1');\n    await kvpDb.insertKeyValueMeta('key2', 'value2', 'meta1');\n    await kvpDb.insertKeyValueMeta('key3', 'value3', 'meta1');\n    await kvpDb.insertKeyValueMeta('key4', 'value1', 'meta2');\n    await kvpDb.insertKeyValueMeta('key5', 'value2', 'meta2');\n    await kvpDb.insertKeyValueMeta('key6', 'value3', 'meta2');\n\n    // the parameter `meta` of all kvps with the meta equal to `meta2` will change to `meta3`\n    await kvpDb.bulkUpdateMeta('meta2', 'meta3');\n```\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrideosapps%2Ffrideos_kvprx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrideosapps%2Ffrideos_kvprx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrideosapps%2Ffrideos_kvprx/lists"}