The SQLite client on the standard library's sqlite3 (Effect's
@effect/sql-sqlite-node SqliteClient).
Each client opens one connection, on a worker thread of its own, and shares it between fibers: statements wait for it in turn, and a transaction holds it until it ends.
Re-exports#
Classes#
Functions#
make#
make(config) -> Effect[SqliteClientService, SqlError, Scope | Reactivity]Open the database and build a client on it; the connection closes with the scope.
| Parameter | Type | Description |
|---|---|---|
config | SqliteClientConfig |
Returns Effect[SqliteClientService, SqlError, Scope | Reactivity]
def make(config: SqliteClientConfig) -> Effect[SqliteClientService, SqlError, Scope | Reactivity]:
"""Open the database and build a client on it; the connection closes
with the scope."""
compiler = make_compiler_sqlite(config.transform_query_names)
transform_rows = (
None
if config.transform_result_names is None
else default_transforms(config.transform_result_names).array
)
busy_timeout = _busy_timeout_millis(config.busy_timeout)
def configure(conn: SqliteConnection) -> Effect[SqliteConnection, SqlError]:
setup = conn.exec_config(f"PRAGMA busy_timeout = {busy_timeout}")
if not config.disable_wal and not config.readonly:
setup = setup.and_then(_enable_wal(conn, busy_timeout))
return setup.as_(conn)
opened = acquire_release(
open_connection(
config.filename,
readonly=config.readonly,
prepare_cache_size=config.prepare_cache_size,
),
lambda conn, _: conn.close(),
).flat_map(configure)
def build(conn: SqliteConnection) -> Effect[SqliteClientService, Never, Reactivity]:
acquirers = make_sqlite_acquirers(
connection=_Suspend(lambda: _succeed(conn)),
semaphore=Semaphore(1),
is_transaction=lambda c: c.is_transaction(),
)
client = make_client(
driver="sqlite-node",
acquirer=acquirers.acquirer,
borrower=acquirers.borrower,
compiler=compiler,
transaction_acquirer=acquirers.transaction_acquirer,
on_commit_failure=acquirers.on_commit_failure,
is_transaction=acquirers.is_transaction,
release_savepoint=lambda name: f"RELEASE SAVEPOINT {name}",
begin_transaction="BEGIN" if config.readonly else "BEGIN IMMEDIATE",
span_attributes=[*config.span_attributes.items(), ("db.system.name", "sqlite")],
transform_rows=transform_rows,
)
return client.map(
lambda c: SqliteClientService(config=config, connection=conn, **c.fields())
)
return opened.flat_map(build)layer#
layer(config) -> Layer[SqliteClient | SqlClient, SqlError]Provides the client as SqliteClient and SqlClient, with its
own Reactivity.
| Parameter | Type | Description |
|---|---|---|
config | SqliteClientConfig |
Returns Layer[SqliteClient | SqlClient, SqlError]
def layer(config: SqliteClientConfig) -> Layer[SqliteClient | SqlClient, SqlError]:
"""Provides the client as ``SqliteClient`` and ``SqlClient``, with its
own ``Reactivity``."""
return layer_effect_context(make(config).map(_contexts)).pipe(provide(reactivity_layer))layer_config#
layer_config(config) -> Layer[SqliteClient | SqlClient, ConfigError | SqlError]layer with the config read from the ConfigProvider: a
Config of the whole config, or a mapping from field names to their
Config (Effect's Config.Wrap)::
sqlite.layer_config({"filename": fx.config.string("DATABASE_PATH")})| Parameter | Type | Description |
|---|---|---|
config | Config[SqliteClientConfig] | Mapping[str, Any] |
Returns Layer[SqliteClient | SqlClient, ConfigError | SqlError]
def layer_config(
config: Config[SqliteClientConfig] | Mapping[str, Any],
) -> Layer[SqliteClient | SqlClient, ConfigError | SqlError]:
"""``layer`` with the config read from the ``ConfigProvider``: a
``Config`` of the whole config, or a mapping from field names to their
``Config`` (Effect's ``Config.Wrap``)::
sqlite.layer_config({"filename": fx.config.string("DATABASE_PATH")})
"""
read: Effect[SqliteClientConfig, ConfigError] = (
config
if isinstance(config, Config)
else unwrap(config).map(lambda fields: SqliteClientConfig(**fields))
)
client = read.flat_map(make).map(_contexts)
return layer_effect_context(client).pipe(provide(reactivity_layer))