Struct Joydb

Source
pub struct Joydb<S: State, A: Adapter> { /* private fields */ }
Expand description

A struct that represents a database. It’s thread-safe and can be shared between multiple threads.

Essentially the database is a combination of a state that needs to be persisted and an adapter that is used to persist/load the state.

§CRUD operations

OperationMethods
Createinsert, upsert
Readget, get_all, get_all_by, count
Updateupdate, upsert
Deletedelete, delete_all_by

Implementations§

Source§

impl<S: State, A: Adapter + FromPath> Joydb<S, A>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, JoydbError>

Opens a database from the given file or directory. If the database does not exist, it will be created.

Source§

impl<S: State, A: Adapter> Joydb<S, A>

Source

pub fn new_in_memory() -> Result<Self, JoydbError>

Creates a new in-memory database. This database is not persisted to the file system. This is intended to be used mostly in tests.

Source

pub fn open_with_config(config: JoydbConfig<A>) -> Result<Self, JoydbError>

Source

pub fn insert<M: Model>(&self, model: &M) -> Result<(), JoydbError>
where S: GetRelation<M>,

Inserts a new record. Returns the inserted record.

§Errors

Returns an error if the record with the same id already exists.

Source

pub fn get<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>
where S: GetRelation<M>,

Finds a record by its id. Returns None if the record is not found.

Source

pub fn get_all<M: Model>(&self) -> Result<Vec<M>, JoydbError>
where S: GetRelation<M>,

Returns all records that corresponds to the model type. The order of the records is not guaranteed and is a subject to change in the future versions.

Source

pub fn get_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
where M: Model, S: GetRelation<M>, F: Fn(&M) -> bool,

Return all records that match the predicate.

Source

pub fn count<M: Model>(&self) -> Result<usize, JoydbError>
where S: GetRelation<M>,

Returns the number of records that corresponds to the model type.

§Errors

No real errors are expected to happen. However, Result<usize, JoydbError> is used to keep the API consistent with other methods and to make the user treat interaction with the database as fallible operations.

Source

pub fn update<M: Model>(&self, new_record: &M) -> Result<(), JoydbError>
where S: GetRelation<M>,

Source

pub fn upsert<M: Model>(&self, record: &M) -> Result<(), JoydbError>
where S: GetRelation<M>,

Upserts a record. If the record with the same id already exists, it will be updated. Otherwise, it will be inserted.

Source

pub fn delete<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>
where S: GetRelation<M>,

Deletes a record by its id and returns the deleted record. If the record is not found, it returns None.

Source

pub fn delete_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
where M: Model, S: GetRelation<M>, F: Fn(&M) -> bool,

Deletes all records that match the predicate. Returns the deleted records.

Source

pub fn flush(&self) -> Result<(), JoydbError>

Flushes the state to the file system. If there are any unsaved changes the corresponding file(s) will be rewritten from scratch. This method is also always called automatically on drop.

Trait Implementations§

Source§

impl<S: State, A: Adapter> Clone for Joydb<S, A>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + State, A: Debug + Adapter> Debug for Joydb<S, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S, A> Freeze for Joydb<S, A>

§

impl<S, A> RefUnwindSafe for Joydb<S, A>

§

impl<S, A> Send for Joydb<S, A>

§

impl<S, A> Sync for Joydb<S, A>

§

impl<S, A> Unpin for Joydb<S, A>

§

impl<S, A> UnwindSafe for Joydb<S, A>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.