KeyValueStore
The @effect/platform/KeyValueStore
module provides a robust and effectful interface for managing key-value pairs.
It supports asynchronous operations, ensuring data integrity and consistency, and includes built-in implementations for in-memory, file system-based, and schema-validated stores.
The module provides a single KeyValueStore
tag, which acts as the gateway for interacting with the store.
Example (Accessing the KeyValueStore Service)
The KeyValueStore
interface includes the following operations:
Operation | Description |
---|---|
get | Returns the value as string of the specified key if it exists. |
getUint8Array | Returns the value as Uint8Array of the specified key if it exists. |
set | Sets the value of the specified key. |
remove | Removes the specified key. |
clear | Removes all entries. |
size | Returns the number of entries. |
modify | Updates the value of the specified key if it exists. |
modifyUint8Array | Updates the value of the specified key if it exists. |
has | Check if a key exists. |
isEmpty | Check if the store is empty. |
forSchema | Create a SchemaStore for the specified schema. |
Example (Working with Key-Value Pairs)
The module provides several built-in implementations of the KeyValueStore
interface, available as layers, to suit different needs:
Implementation | Description |
---|---|
In-Memory Store | layerMemory provides a simple, in-memory key-value store, ideal for lightweight or testing scenarios. |
File System Store | layerFileSystem offers a file-based store for persistent storage needs. |
Schema Store | layerSchema enables schema-based validation for stored values, ensuring data integrity and type safety. |
The SchemaStore
interface allows you to validate and parse values according to a defined schema.
This ensures that all data stored in the key-value store adheres to the specified structure, enhancing data integrity and type safety.
Example (Using Schema Validation in KeyValueStore)