sos_database/
lib.rs

1#![deny(missing_docs)]
2#![forbid(unsafe_code)]
3#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))]
4//! Database storage layer for the [Save Our Secrets](https://saveoursecrets.com) SDK.
5#[cfg(feature = "archive")]
6pub mod archive;
7#[cfg(feature = "audit")]
8pub mod audit_provider;
9pub mod entity;
10pub mod event_log;
11pub mod migrations;
12#[cfg(feature = "preferences")]
13mod preferences;
14mod server_origins;
15#[cfg(feature = "system-messages")]
16mod system_messages;
17mod vault_writer;
18
19pub use event_log::{
20    AccountEventLog, DatabaseEventLog, DeviceEventLog, EventLogOwner,
21    FolderEventLog,
22};
23
24#[cfg(feature = "preferences")]
25pub use preferences::PreferenceProvider;
26
27#[cfg(feature = "system-messages")]
28pub use system_messages::SystemMessagesProvider;
29
30pub use server_origins::ServerOrigins;
31pub use vault_writer::VaultDatabaseWriter;
32
33#[cfg(feature = "files")]
34pub use event_log::FileEventLog;
35
36mod error;
37pub use async_sqlite;
38pub use error::Error;
39
40/// Result type for the library.
41pub(crate) type Result<T> = std::result::Result<T, Error>;
42
43use async_sqlite::{Client, ClientBuilder, JournalMode};
44use std::path::Path;
45
46/// Open a database file from a path with WAL journal mode enabled.
47pub async fn open_file(path: impl AsRef<Path>) -> Result<Client> {
48    Ok(ClientBuilder::new()
49        .path(path.as_ref())
50        .journal_mode(JournalMode::Wal)
51        .open()
52        .await?)
53}
54
55/// Open a database file from a specific journal mode.
56pub async fn open_file_with_journal_mode(
57    path: impl AsRef<Path>,
58    mode: JournalMode,
59) -> Result<Client> {
60    Ok(ClientBuilder::new()
61        .path(path.as_ref())
62        .journal_mode(mode)
63        .open()
64        .await?)
65}
66
67/// Open an in-memory database and run migrations.
68pub async fn open_memory() -> Result<Client> {
69    let mut client = ClientBuilder::new().open().await?;
70    crate::migrations::migrate_client(&mut client).await?;
71    Ok(client)
72}
73
74#[cfg(debug_assertions)]
75#[allow(dead_code)]
76pub(crate) fn dump_rows<C>(
77    conn: C,
78    table: &str,
79) -> std::result::Result<(), async_sqlite::rusqlite::Error>
80where
81    C: std::ops::Deref<Target = async_sqlite::rusqlite::Connection>,
82{
83    println!("--- BEGIN DUMP ---");
84    let mut stmt = conn.prepare(&format!(
85        r#"
86            SELECT * FROM {}
87        "#,
88        table
89    ))?;
90    let mut rows = stmt.query([])?;
91    while let Ok(Some(row)) = rows.next() {
92        println!("{:#?}", row);
93    }
94    println!("--- END DUMP ---");
95    Ok(())
96}