Struct polodb_core::Database
source · pub struct Database { /* private fields */ }
Expand description
API wrapper for Rust-level
Use open API to open a database. A main database file will be generated in the path user provided.
When you own an instance of a Database, the instance holds a file descriptor of the database file. When the Database instance is dropped, the handle of the file will be released.
Collection
A Collection is a dataset of a kind of data. You can use create_collection to create a data collection. To obtain an exist collection, use collection,
Transaction
You an manually start a transaction by start_transaction method. If you don’t start it manually, a transaction will be automatically started in your every operation.
Example
use polodb_core::Database;
use polodb_core::bson::{Document, doc};
let db = Database::open_file(db_path).unwrap();
let collection = db.collection::<Document>("books");
let docs = vec![
doc! { "title": "1984", "author": "George Orwell" },
doc! { "title": "Animal Farm", "author": "George Orwell" },
doc! { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" },
];
collection.insert_many(docs).unwrap();
Implementations§
source§impl Database
impl Database
pub fn set_log(v: bool)
sourcepub fn get_version() -> String
pub fn get_version() -> String
Return the version of package version in string.
Defined in Cargo.toml
.
pub fn open_memory() -> DbResult<Database>
pub fn open_memory_with_config(config: Config) -> DbResult<Database>
pub fn open_file<P: AsRef<Path>>(path: P) -> DbResult<Database>
pub fn open_file_with_config<P: AsRef<Path>>(
path: P,
config: Config
) -> DbResult<Database>
sourcepub fn create_collection<T: Serialize>(&self, name: &str) -> DbResult<()>
pub fn create_collection<T: Serialize>(&self, name: &str) -> DbResult<()>
Creates a new collection in the database with the given name
.
sourcepub fn collection<T: Serialize>(&self, col_name: &str) -> Collection<'_, T>
pub fn collection<T: Serialize>(&self, col_name: &str) -> Collection<'_, T>
Return an exist collection. If the collection is not exists, a new collection will be created.
sourcepub fn start_transaction(&self, ty: Option<TransactionType>) -> DbResult<()>
pub fn start_transaction(&self, ty: Option<TransactionType>) -> DbResult<()>
Manually start a transaction. There are three types of transaction.
None
: Auto transactionSome(Transaction::Write)
: Write transactionSome(Transaction::Read)
: Read transaction
When you pass None
to type parameter. The PoloDB will go into
auto mode. The PoloDB will go into read mode firstly, once the users
execute write operations(insert/update/delete), the DB will turn into
write mode.
pub fn commit(&self) -> DbResult<()>
pub fn rollback(&self) -> DbResult<()>
pub fn dump(&self) -> DbResult<FullDump>
sourcepub fn list_collection_names(&self) -> DbResult<Vec<String>>
pub fn list_collection_names(&self) -> DbResult<Vec<String>>
Gets the names of the collections in the database.