Crate sqlitefs

Source
Expand description

§SQLiteFS

SQLiteFS is a Rust Crate to provide you with an In-App Filesystem based on SQLite.

The base for this are the SQLite Docs:

This crate is basically just a wrapper around a SQLite Database.

This crate makes use of sqlx and uses the default (bundled) feature. So SQLite should be included on build and there should be no need to install SQLite separately on your machine.

§Example 1 - In-Memory FS

use sqlitefs::{
    datatypes::FsStatus,
    file::File,
    fs::{FsMode, SqliteFs},
};
use std::io::Read;

#[tokio::main]
async fn main() -> Result<(), FsStatus> {
    // read a file so we have some data to work with
    let mut s = String::new();
    let mut x = std::fs::File::open("Cargo.toml").unwrap();
    x.read_to_string(&mut s).unwrap();
    drop(x);
    // ----------------------------------------------------------------------

    // Init the FS. Must be called befor anything usefull can be done with it
    SqliteFs::init(FsMode::Memory).await?;

    for i in 1..=100 {
        let uuid = uuid::Uuid::now_v7();
        let f_name = format!("/my/new/file_{i}_{uuid}");

        // create a new file in the SQLiteFS
        let mut f = File::create(&f_name).await?;

        // write data to it
        f.write(s.as_bytes()).await?;
    }

    // Optional: Save the memory fs to disk. Otherwise everything will get lost.
    // Right now there is no way to load such saved FS back into memory
    // let fs_path = "FS.SQLITE".to_string();
    // SqliteFs::save_memory_fs_to_disk(&fs_path).await?;

    Ok(())
}

§Example 2 - Persistent FS

use sqlitefs::{
    datatypes::FsStatus,
    file::File,
    fs::{FsMode, SqliteFs},
};
use std::io::Read;

#[tokio::main]
async fn main() -> Result<(), FsStatus> {
    // read a file so we have some data to work with
    let mut s = String::new();
    let mut x = std::fs::File::open("Cargo.toml").unwrap();
    x.read_to_string(&mut s).unwrap();

    // ----------------------------------------------------------------------

    // Init the FS. Must be called befor anything usefull can be done with it
    let fs_path = "FS.SQLITE".to_string();
    SqliteFs::init(FsMode::Persist(fs_path)).await?;

    for i in 1..=100 {
        let uuid = uuid::Uuid::now_v7();
        let f_name = format!("/my/new/file_{i}_{uuid}");

        // create a new file in the SQLiteFS
        let mut f = File::create(&f_name).await?;

        // write data to it
        f.write(s.as_bytes()).await?;
    }

    Ok(())
}

Modules§

datatypes
file
fs