Skip to main content

Module db

Module db 

Source
Expand description

Database layer — SQLite with per-account databases.

Architecture:

  • Read pool: multiple connections for parallel reads (WAL mode)
  • Write pool: single Mutex-protected connection (serialized writes)
  • RAII guards: auto-return connections to pools on drop

All connection functions use static DATA_DIR — no Tauri AppHandle required.

Re-exports§

pub use settings::get_sql_setting;
pub use settings::set_sql_setting;
pub use settings::get_pkey;
pub use settings::set_pkey;
pub use settings::get_seed;
pub use settings::set_seed;
pub use settings::remove_setting;
pub use settings::get_signer_type;
pub use settings::set_signer_type;
pub use settings::get_bunker_url;
pub use settings::set_bunker_url;
pub use settings::get_bunker_remote_pubkey;
pub use settings::set_bunker_remote_pubkey;
pub use settings::commit_bunker_account_setup;

Modules§

bots
Last-known bot manifests (kind 10304) — the / command picker’s persistent layer. One row per bot pubkey, replaced only by a NEWER manifest edition, so the picker serves instantly from boot while a background refetch converges. Manifests are PUBLIC replaceable events: rows are plaintext by design.
chats
Chat database operations — CRUD for the chats table.
community
Persistence for Community protocol local state (GROUP_PROTOCOL.md).
events
Event storage — save_event for the flat event architecture.
id_cache
ID cache — maps chat identifiers and npubs to SQLite row IDs.
nip17_keys
NIP-17 ephemeral wrap-key vault.
profiles
Profile database operations — read/write SlimProfile to SQLite.
schema
Database schema and migrations.
settings
Settings key-value store operations.
wrappers
Wrapper tracking — NIP-59 gift wrap dedup + NIP-77 negentropy.

Structs§

ConnectionGuard
RAII guard for READ connections — auto-returns to pool on drop.
WriteConnectionGuard
RAII guard for the WRITE connection — auto-returns on drop.

Enums§

SystemEventType

Functions§

account_dir
Single source of truth for per-account directories. Every per-account subsystem (DB, Tor state) resolves its path through this; compose further subpaths with .join(...) — never insert layers between <app_data> and <npub>.
clear_active_account_file
Remove the active-account marker. Used after deleting the active account.
clear_current_account_in_memory
Clear the in-memory active account WITHOUT touching the on-disk marker. Used by reset_session() so the next-boot marker stays intact while in-process state is torn down for an inline account swap.
clear_id_caches
close_database
Close all database connections (for logout / account switch). Bumps POOL_GENERATION first so in-flight guards fail their Drop check and discard the connection instead of returning it to the (now-cleared) pool.
get_accounts
Get all available accounts (npub directories in app data).
get_app_data_dir
get_current_account
get_database_path
Get database path for a given npub.
get_db_connection_guard_static
Get a READ connection (headless-safe — no AppHandle).
get_download_dir
Platform-appropriate download directory for file attachments.
get_or_create_chat_id
Get or create a chat_id integer for a chat identifier string.
get_or_create_user_id
Get or create a user_id integer for an npub.
get_profile_directory
Get the profile directory path for a given npub.
get_write_connection_guard_static
Get the WRITE connection (headless-safe — no AppHandle).
init_database
Initialize the database for a given account (creates tables if needed).
list_account_npubs
Scan the app data directory for valid npub directories. Strict bech32 regex rejects typos and stray subdirectories. Does NOT validate that each account has a usable database — callers do that separately.
read_active_account_file
Read the active-account marker file. Returns the stored npub if it exists, is well-formed, AND the corresponding account directory still exists. Any failure path returns Ok(None) so boot falls back to single-account or picker.
set_app_data_dir
set_current_account
Set the currently-active npub for THIS process AND persist it to the <app_data>/active_account marker so the next boot picks the same account.
set_download_dir
Install the host-resolved download directory. Must be called at startup before any get_download_dir() consumer runs; callers that run earlier hit the fallback.
write_active_account_file
Atomic write of the active-account marker (temp + rename).