Skip to content

Commit f169d38

Browse files
committed
docs(ht_data_inmemory_client): add initialData constructor parameter for preloading data
- Implement optional initial data population through the 'initialData' parameter - Add error handling for duplicate IDs in initial data - Update README.md to reflect new feature - Provide code example for using initialData in main function
1 parent af8be96 commit f169d38

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Key characteristics:
1414
- **In-Memory Storage:** Data is stored locally in Dart `Map` objects. Data is lost when the client instance is destroyed.
1515
- **Dependency on `ht_data_client`:** Implements the standard data client interface.
1616
- **Requires ID and JSON Logic:** You must provide functions to extract a unique ID (`getId`) and serialize items to JSON (`toJson`) during instantiation. The client does *not* generate IDs itself.
17+
- **Optional Initial Data:** You can optionally provide a `List<T>` via the `initialData` constructor parameter to pre-populate the client. Throws `ArgumentError` if duplicate IDs are found in the initial data.
1718
- **Simple Querying:** The `readAllByQuery` method performs basic key-value matching on the JSON representation of items. It does not support complex queries (ranges, advanced sorting, etc.).
1819
- **Error Simulation:** Throws exceptions like `NotFoundException` and `BadRequestException` (from `package:ht_http_client`) to simulate common API errors.
1920
- **Pagination:** Supports basic pagination via `startAfterId` and `limit` parameters on `readAll` and `readAllByQuery`.
@@ -88,10 +89,15 @@ class MyModel {
8889
}
8990
9091
void main() async {
91-
// Instantiate the client
92+
// Instantiate the client (empty)
9293
final client = HtDataInMemoryClient<MyModel>(
9394
toJson: (item) => item.toJson(),
9495
getId: (item) => item.id,
96+
// Optionally provide initial data:
97+
// initialData: [
98+
// MyModel(id: 'pre1', name: 'Preloaded 1', value: 10),
99+
// MyModel(id: 'pre2', name: 'Preloaded 2', value: 20),
100+
// ],
95101
);
96102
97103
// Create an item

0 commit comments

Comments
 (0)