Skip to content

Commit

Permalink
Add deleteAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed Oct 26, 2024
1 parent 456fb06 commit 79e475e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/src/index_entity_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,20 @@ class IndexedEntityStore<T, K> {
}
}

/// 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});
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
31 changes: 31 additions & 0 deletions test/indexed_entity_store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 79e475e

Please sign in to comment.