Struct JsonDatabase

Source
pub struct JsonDatabase<T> { /* private fields */ }
Expand description

A simple JSON file-based database ORM for Rust.

Implementations§

Source§

impl<T> JsonDatabase<T>
where T: Serialize + for<'de> Deserialize<'de> + Clone + Identifiable,

Source

pub fn new(model_name: Option<&str>) -> Self

Creates a new JsonDatabase instance.

§Arguments
  • model_name - Optional model name to initialize the database with.
§Examples
let db: JsonDatabase<User> = JsonDatabase::new(Some("users"));
Source

pub fn model(&mut self, model_name: &str) -> &mut Self

Sets the model name for the database and ensures the directory exists.

§Arguments
  • model_name - The name of the model to use.
§Examples
let mut db: JsonDatabase<User> = JsonDatabase::new(None);
db.model("users");
Source

pub fn create_model(&self, data: T)

Creates a new model in the database.

§Arguments
  • data - The data to create.
§Panics

Panics if the data does not have an ID field.

Source

pub fn create(&self, id: &str, data: T)

Creates a new record in the database.

§Arguments
  • id - The ID of the record.
  • data - The data to create.
Source

pub fn find_by_id(&self, id: &str) -> Option<T>

Finds a record by ID.

§Arguments
  • id - The ID of the record to find.
§Returns

Returns Some(T) if the record is found, or None if not.

Source

pub fn update_by_id(&self, id: &str, data: Value)

Updates a record by ID.

§Arguments
  • id - The ID of the record to update.
  • data - The data to update.
Source

pub fn delete_by_id(&self, id: &str)

Deletes a record by ID.

§Arguments
  • id - The ID of the record to delete.
Source

pub fn find_all(&self) -> Vec<T>

Finds all records.

§Returns

Returns a vector of all records.

Source

pub fn find(&self, condition: &Value) -> Vec<T>

Finds records matching a condition.

§Arguments
  • condition - The condition to match.
§Returns

Returns a vector of matching records.

Source

pub fn find_one(&self, condition: &Value) -> Option<T>

Finds the first record matching a condition.

§Arguments
  • condition - The condition to match.
§Returns

Returns Some(T) if a matching record is found, or None if not.

Source

pub fn count(&self, condition: &Value) -> usize

Counts the number of records matching a condition.

§Arguments
  • condition - The condition to match.
§Returns

Returns the number of matching records.

Source

pub fn update_many(&self, condition: &Value, data: &Value)

Updates multiple records matching a condition.

§Arguments
  • condition - The condition to match.
  • data - The data to update.
Source

pub fn delete_many(&self, condition: &Value)

Deletes multiple records matching a condition.

§Arguments
  • condition - The condition to match.
Source

pub fn push(&self, condition: &Value, array_path: &str, element: &Value)

Adds an element to an array in records matching a condition.

§Arguments
  • condition - The condition to match.
  • array_path - The path to the array.
  • element - The element to add.
Source

pub fn pull(&self, condition: &Value, array_path: &str, pull_condition: &Value)

Removes elements from an array in records matching a condition.

§Arguments
  • condition - The condition to match.
  • array_path - The path to the array.
  • pull_condition - The condition to match elements to remove.
Source

pub fn update_array( &self, condition: &Value, array_path: &str, array_condition: &Value, updates: &Value, )

Updates elements in an array in records matching a condition.

§Arguments
  • condition - The condition to match.
  • array_path - The path to the array.
  • array_condition - The condition to match array elements.
  • updates - The updates to apply to matching elements.
Source§

impl JsonDatabase<()>

Source

pub fn set_base_dir(base_dir: &str)

Sets the base directory for the database.

§Arguments
  • base_dir - The base directory for the database.
§Examples
JsonDatabase::set_base_dir("custom-dir");

Trait Implementations§

Source§

impl<T: Clone> Clone for JsonDatabase<T>

Source§

fn clone(&self) -> JsonDatabase<T>

Returns a duplicate 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<T: Debug> Debug for JsonDatabase<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for JsonDatabase<T>

§

impl<T> RefUnwindSafe for JsonDatabase<T>
where T: RefUnwindSafe,

§

impl<T> Send for JsonDatabase<T>
where T: Send,

§

impl<T> Sync for JsonDatabase<T>
where T: Sync,

§

impl<T> Unpin for JsonDatabase<T>
where T: Unpin,

§

impl<T> UnwindSafe for JsonDatabase<T>
where T: UnwindSafe,

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.