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
Operation | Methods |
---|---|
Create | insert , upsert |
Read | get , get_all , get_all_by , count |
Update | update , upsert |
Delete | delete , delete_all_by |
Implementations§
Source§impl<S: State, A: Adapter> Joydb<S, A>
impl<S: State, A: Adapter> Joydb<S, A>
Sourcepub fn new_in_memory() -> Result<Self, JoydbError>
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.
pub fn open_with_config(config: JoydbConfig<A>) -> Result<Self, JoydbError>
Sourcepub fn insert<M: Model>(&self, model: &M) -> Result<(), JoydbError>where
S: GetRelation<M>,
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.
Sourcepub fn get<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>where
S: GetRelation<M>,
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.
Sourcepub fn get_all<M: Model>(&self) -> Result<Vec<M>, JoydbError>where
S: GetRelation<M>,
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.
Sourcepub fn get_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
pub fn get_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
Return all records that match the predicate.
Sourcepub fn count<M: Model>(&self) -> Result<usize, JoydbError>where
S: GetRelation<M>,
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.
pub fn update<M: Model>(&self, new_record: &M) -> Result<(), JoydbError>where
S: GetRelation<M>,
Sourcepub fn upsert<M: Model>(&self, record: &M) -> Result<(), JoydbError>where
S: GetRelation<M>,
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.
Sourcepub fn delete<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>where
S: GetRelation<M>,
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
.
Sourcepub fn delete_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
pub fn delete_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
Deletes all records that match the predicate. Returns the deleted records.
Sourcepub fn flush(&self) -> Result<(), JoydbError>
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.