From 79e475ed6a2aa8595b80535e29ddd85b480ae567 Mon Sep 17 00:00:00 2001 From: Timm Preetz Date: Sat, 26 Oct 2024 18:47:05 +0200 Subject: [PATCH] Add `deleteAll` method --- CHANGELOG.md | 4 ++++ lib/src/index_entity_store.dart | 14 +++++++++++++ pubspec.yaml | 2 +- test/indexed_entity_store_test.dart | 31 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b0374..a25cff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.1 + +* Add `deleteAll` method + ## 1.4.0 * Enhance subscriptions to not emit updates when the underlying database value has not changed diff --git a/lib/src/index_entity_store.dart b/lib/src/index_entity_store.dart index 2bfb1fa..9fd81dd 100644 --- a/lib/src/index_entity_store.dart +++ b/lib/src/index_entity_store.dart @@ -289,6 +289,20 @@ class IndexedEntityStore { } } + /// Removes all entries from the store + void deleteAll() { + final result = _database.select( + 'DELETE FROM `entity` WHERE `type` = ? RETURNING `key`', + [_entityKey], + ); + + _handleUpdate( + { + for (final row in result) row['key']!, + }, + ); + } + /// Deletes a single entity by its primary key void delete(K key) { deleteMany({key}); diff --git a/pubspec.yaml b/pubspec.yaml index f77b00b..964df01 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: indexed_entity_store description: A fast, simple, and synchronous entity store for Flutter applications. -version: 1.4.0 +version: 1.4.1 repository: https://github.com/LunaONE/indexed_entity_store environment: diff --git a/test/indexed_entity_store_test.dart b/test/indexed_entity_store_test.dart index b587d73..7b99b00 100644 --- a/test/indexed_entity_store_test.dart +++ b/test/indexed_entity_store_test.dart @@ -943,6 +943,37 @@ void main() { ), ); }); + + test('Delete all', () async { + final path = '/tmp/index_entity_store_test_${FlutterTimeline.now}.sqlite3'; + + final db = IndexedEntityDabase.open(path); + + final fooStore = db.entityStore(fooConnector); + + expect(fooStore.getAllOnce(), isEmpty); + + fooStore.insert( + _FooEntity(id: 1, valueA: 'a', valueB: 1, valueC: true), + ); + fooStore.insert( + _FooEntity(id: 2, valueA: 'b', valueB: 2, valueC: true), + ); + + expect(fooStore.getAllOnce(), hasLength(2)); + + final singleSubscription = fooStore.get(1); + final listSubscription = fooStore.query((cols) => cols['b'].lessThan(5)); + expect(singleSubscription.value, isA<_FooEntity>()); + expect(listSubscription.value, hasLength(2)); + + // Delete all rows + fooStore.deleteAll(); + + expect(singleSubscription.value, isNull); + expect(listSubscription.value, isEmpty); + expect(fooStore.getAllOnce(), isEmpty); + }); } class _FooEntity {