Expand description
§replit_db
An unofficial database adapater for Replit Database for Rust!
§Usage
You need to import Database
, Config
, and a trait (Synchronous
, Asynchronous
).
Then initialize Database::new()
with Config::new()
then database will give you function in either synchronously or asynchronously based on trait you imported in to the scope.
§Possible Exceptions
Error
struct contain useful informations and both std::fmt::Display
and std::error::Error
(support “?”).
Also there’s different error kinds that can happens, here’s the list of them.
ErrorKind::HttpError
Raised when there’s something wrong when doing HTTP request.ErrorKind::NoItemFoundError
Raised when item is not foundErrorKind::DecodeError
Raised when the key name is undecodable to UTF-8 string.
§Examples
§Example (Synchronous)
ⓘ
use replit_db::{Synchronous, Error};
fn main() -> Result<(), Error> {
let db = replit_db::Database::new(replit_db::Config::new().unwrap());
db.get("Hello")?; // Get a value from key's name.
db.set("Hello", "World")?; // Set a value to that key
db.delete("Hello")?; // Delete a key
db.list(replit_db::STRING_NONE)?; // List all keys
db.list(Some("H"))?; // List keys with "H" prefix
return Ok(())
}
§Example (Asynchronous)
ⓘ
use replit_db::{Asynchronous, Error};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Error> {
let db = replit_db::Database::new(replit_db::Config::new().unwrap());
db.get("Hello").await?; // Get a value from key's name.
db.set("Hello", "World").await?; // Set a value to that key
db.delete("Hello").await?; // Delete a key
db.list(replit_db::STRING_NONE).await?; // List all keys
db.list(Some("H")).await?; // List keys with "H" prefix
return Ok(())
}
Structs§
- Config
- Configuration struct that contains information needed for Database.
- Database
- Database main struct.
Please use this database with traits. (Availables are
Synchronous
andAsynchronous
) - Error
- Error struct for giving useful information about what goes wrong.
Enums§
- Error
Kind - Error kind. (Http Error, No Item Found Error, Decode String Error)
Constants§
- STRING_
NONE - This type is a shorthand for
Option<&str>::None
orNone::<&str>
.
Traits§
- Asynchronous
- Asynchronous support for Database struct. Use this trait by import it then use it right away!
- Synchronous
- Synchronous support for Database struct. Use this trait by import it then use it right away!