Struct Database

Source
pub struct Database<PA> { /* private fields */ }
Expand description

A Database provides methods to access data.

The config private field is wrapped in an Arc to be shared among threads.

Implementations§

Source§

impl<PA> Database<PA>
where PA: AsRef<Path> + Send + Sync + Clone + 'static,

Source

pub fn new(path: PA, extension: Option<&str>) -> Self

Create a new Database with a mandatory path and an optional file extension.

§Examples
use csv_db::Database;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct User {
    id: usize,
    first_name: String,
    last_name: String,
    age: u32,
}

#[tokio::main]
async fn main() {
    let db = Database::new("data", None);
}
Source

pub async fn find<T, P>( &self, collection: &str, predicate: P, ) -> Result<Vec<T>, Box<dyn Error>>
where T: Serialize + for<'de> Deserialize<'de> + Send + 'static, P: FnMut(&T) -> bool,

Find documents by filtering with a predicate on a collection.

§Examples
use csv_db::Database;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct User {
    id: usize,
    first_name: String,
    last_name: String,
    age: u32,
}

#[tokio::main]
async fn main() {
    let db = Database::new("data", None);

    let user = User {
        id: 1,
        first_name: String::from("First"),
        last_name: String::from("Last"),
        age: 20,
    };

    let adults = db
        .find("users", |u: &User| u.age >= 18)
        .await
        .expect("Problem searching user.");
}
Source

pub async fn insert<T>( &self, collection: &str, document: T, ) -> Result<(), Box<dyn Error>>
where T: Serialize + for<'de> Deserialize<'de> + Send + 'static,

Insert a new document into a collection.

§Examples
use csv_db::Database;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct User {
    id: usize,
    first_name: String,
    last_name: String,
    age: u32,
}

#[tokio::main]
async fn main() {
    let db = Database::new("data", None);

    let user = User {
        id: 1,
        first_name: String::from("First"),
        last_name: String::from("Last"),
        age: 20,
    };

    db.insert("users", user)
        .await
        .expect("Problem inserting user.");
}
Source

pub async fn delete<T, P>( &self, collection: &str, predicate: P, ) -> Result<(), Box<dyn Error>>
where T: Serialize + for<'de> Deserialize<'de> + PartialEq + Send + 'static, P: FnMut(&&T) -> bool,

Delete a document by filtering with a predicate from a collection.

§Examples
use csv_db::Database;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct User {
    id: usize,
    first_name: String,
    last_name: String,
    age: u32,
}

#[tokio::main]
async fn main() {
    let db = Database::new("data", None);

    db.delete("users", |u: &&User| u.id == 1)
        .await
        .expect("Problem deleting user.");
}
Source

pub async fn update<T, P>( &self, collection: &str, document: T, predicate: P, ) -> Result<(), Box<dyn Error>>
where T: Serialize + for<'de> Deserialize<'de> + PartialEq + Send + 'static, P: FnMut(&&T) -> bool,

Update a document by filtering with a predicate on a collection.

§Examples
use csv_db::Database;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct User {
    id: usize,
    first_name: String,
    last_name: String,
    age: u32,
}

#[tokio::main]
async fn main() {
    let db = Database::new("data", None);

    let user = User {
        id: 1,
        first_name: String::from("First"),
        last_name: String::from("Last"),
        age: 21,
    };

    db.update("users", user, |u: &&User| u.id == 1)
        .await
        .expect("Problem updating user.");
}

Auto Trait Implementations§

§

impl<PA> Freeze for Database<PA>

§

impl<PA> RefUnwindSafe for Database<PA>
where PA: RefUnwindSafe,

§

impl<PA> Send for Database<PA>
where PA: Sync + Send,

§

impl<PA> Sync for Database<PA>
where PA: Sync + Send,

§

impl<PA> Unpin for Database<PA>

§

impl<PA> UnwindSafe for Database<PA>
where PA: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.