Crate replit_db

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

Examples

Example (Synchronous)

use replit_db::{self, Synchronous};

fn main() -> Result<(), Error> {

    let db = replit_db::Database::new(replit_db::Config::new());
    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::NONE)?; // List all keys
    db.get(Some("H"))?; // List keys with "H" prefix
}

Example (Asynchronous)

use replit_db::{self, Asynchronous};
use tokio;

#[tokio::main]
fn main() -> Result<(), Error> {

    let db = replit_db::Database::new(replit_db::Config::new());
    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::NONE).await?; // List all keys
    db.get(Some("H")).await?; // List keys with "H" prefix
}

Structs

  • Configuration struct that contains information needed for Database.
  • Database main struct. Please use this database with traits. (Availables are Synchronous and Asynchronous)
  • Error struct for giving useful information about what goes wrong.

Enums

  • Error kind. (Http Error, No Item Found Error, Decode String Error)

Constants

Traits

  • Asynchronous support for Database struct. Use this trait by import it then use it right away!
  • Synchronous support for Database struct. Use this trait by import it then use it right away!