SQLStore#
- class langchain_community.storage.sql.SQLStore(*, namespace: str, db_url: str | Path | None = None, engine: Engine | AsyncEngine | None = None, engine_kwargs: Dict[str, Any] | None = None, async_mode: bool | None = None)[source]#
BaseStore interface that works on an SQL database.
Examples
Create a SQLStore instance and perform operations on it:
from langchain_community.storage import SQLStore # Instantiate the SQLStore with the root path sql_store = SQLStore(namespace="test", db_url="sqlite://:memory:") # Set values for keys sql_store.mset([("key1", b"value1"), ("key2", b"value2")]) # Get values for keys values = sql_store.mget(["key1", "key2"]) # Returns [b"value1", b"value2"] # Delete keys sql_store.mdelete(["key1"]) # Iterate over keys for key in sql_store.yield_keys(): print(key)
Methods
__init__
(*, namespace[, db_url, engine, ...])amdelete
(keys)Async delete the given keys and their associated values.
amget
(keys)Async get the values associated with the given keys.
amset
(key_value_pairs)Async set the values for the given keys.
ayield_keys
(*[, prefix])Async get an iterator over keys that match the given prefix.
drop
()mdelete
(keys)Delete the given keys and their associated values.
mget
(keys)Get the values associated with the given keys.
mset
(key_value_pairs)Set the values for the given keys.
yield_keys
(*[, prefix])Get an iterator over keys that match the given prefix.
- Parameters:
namespace (str)
db_url (str | Path | None)
engine (Engine | AsyncEngine | None)
engine_kwargs (Dict[str, Any] | None)
async_mode (bool | None)
- __init__(*, namespace: str, db_url: str | Path | None = None, engine: Engine | AsyncEngine | None = None, engine_kwargs: Dict[str, Any] | None = None, async_mode: bool | None = None)[source]#
- Parameters:
namespace (str)
db_url (str | Path | None)
engine (Engine | AsyncEngine | None)
engine_kwargs (Dict[str, Any] | None)
async_mode (bool | None)
- async amdelete(keys: Sequence[str]) None [source]#
Async delete the given keys and their associated values.
- Parameters:
keys (Sequence[K]) – A sequence of keys to delete.
- Return type:
None
- async amget(keys: Sequence[str]) List[bytes | None] [source]#
Async get the values associated with the given keys.
- Parameters:
keys (Sequence[K]) – A sequence of keys.
- Returns:
A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.
- Return type:
List[bytes | None]
- async amset(key_value_pairs: Sequence[Tuple[str, bytes]]) None [source]#
Async set the values for the given keys.
- Parameters:
key_value_pairs (Sequence[Tuple[K, V]]) – A sequence of key-value pairs.
- Return type:
None
- async ayield_keys(*, prefix: str | None = None) AsyncIterator[str] [source]#
Async get an iterator over keys that match the given prefix.
- Parameters:
prefix (str) – The prefix to match.
- Yields:
Iterator[K | str] – An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.
- Return type:
AsyncIterator[str]
- mdelete(keys: Sequence[str]) None [source]#
Delete the given keys and their associated values.
- Parameters:
keys (Sequence[K]) – A sequence of keys to delete.
- Return type:
None
- mget(keys: Sequence[str]) List[bytes | None] [source]#
Get the values associated with the given keys.
- Parameters:
keys (Sequence[K]) – A sequence of keys.
- Returns:
A sequence of optional values associated with the keys. If a key is not found, the corresponding value will be None.
- Return type:
List[bytes | None]
- mset(key_value_pairs: Sequence[Tuple[str, bytes]]) None [source]#
Set the values for the given keys.
- Parameters:
key_value_pairs (Sequence[Tuple[K, V]]) – A sequence of key-value pairs.
- Return type:
None
- yield_keys(*, prefix: str | None = None) Iterator[str] [source]#
Get an iterator over keys that match the given prefix.
- Parameters:
prefix (str) – The prefix to match.
- Yields:
Iterator[K | str] – An iterator over keys that match the given prefix. This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.
- Return type:
Iterator[str]