tinybase/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5use alloc::sync::Arc;
6
7use sled::Config;
8
9pub mod index;
10pub use index::Index;
11
12pub mod query_builder;
13pub use query_builder::{ConditionBuilder, QueryBuilder};
14
15pub mod result;
16pub use result::DbResult;
17
18pub mod record;
19pub use record::Record;
20
21pub mod table;
22pub use table::Table;
23use table::{TableInner, TableType};
24
25pub mod constraint;
26pub use constraint::Constraint;
27
28mod encoding;
29mod subscriber;
30
31/// A tiny structured database based on sled.
32pub struct TinyBase {
33    engine: sled::Db,
34}
35
36impl TinyBase {
37    /// Create a new instance of `TinyBase`.
38    ///
39    /// # Arguments
40    ///
41    /// * `path` - An optional path to the database file. If `None`, an in-memory database is created.
42    /// * `temporary` - If `true`, the database file will be deleted on close.
43    pub fn new(path: Option<&str>, temporary: bool) -> Self {
44        Self {
45            engine: if let Some(path) = path {
46                Config::new().path(path).temporary(temporary)
47            } else {
48                Config::new().temporary(temporary)
49            }
50            .open()
51            .unwrap(),
52        }
53    }
54
55    /// Open a table for a given type.
56    ///
57    /// # Arguments
58    ///
59    /// * `name` - The name of the table.
60    ///
61    /// # Returns
62    ///
63    /// A `Table` instance for the given type.
64    pub fn open_table<T: TableType>(&self, name: &str) -> DbResult<Table<T>> {
65        Ok(Table(Arc::new(TableInner::new(&self.engine, name)?)))
66    }
67}