Struct just_orm::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.

Trait Implementations§

source§

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

source§

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

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

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de, T> Deserialize<'de> for JsonDatabase<T>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> Serialize for JsonDatabase<T>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> 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,

§

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>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,