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,
Session
A ClientSession
represents a logical session used for ordering sequential
operations.
You an manually start a transaction by ClientSession::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 mut session = db.start_session().unwrap();
session.start_transaction(None).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_with_session(docs, &mut session).unwrap();
session.commit_transaction().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(&self, name: &str) -> DbResult<()>
pub fn create_collection(&self, name: &str) -> DbResult<()>
Creates a new collection in the database with the given name
.
sourcepub fn create_collection_with_session(
&self,
name: &str,
session: &mut ClientSession<'_>
) -> DbResult<()>
pub fn create_collection_with_session( &self, name: &str, session: &mut ClientSession<'_> ) -> 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.
pub fn start_session(&self) -> DbResult<ClientSession<'_>>
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.
sourcepub fn list_collection_names_with_session(
&self,
session: &mut ClientSession<'_>
) -> DbResult<Vec<String>>
pub fn list_collection_names_with_session( &self, session: &mut ClientSession<'_> ) -> DbResult<Vec<String>>
Gets the names of the collections in the database.
sourcepub fn handle_request<R: Read>(
&self,
pipe_in: &mut R
) -> DbResult<HandleRequestResult>
pub fn handle_request<R: Read>( &self, pipe_in: &mut R ) -> DbResult<HandleRequestResult>
handle request for database