Crate skytable

Source
Expand description

§Skytable driver

Crates.io Test docs.rs GitHub release (latest SemVer including pre-releases)

This is Skytable’s official client driver for Rust that you can use to build applications. The client-driver is distributed under the liberal Apache-2.0 License and hence you can use it in your applications without any licensing issues.

📁 You can find some usage examples in this folder here.

§Definitive example

use skytable::{
    Query, Response, Config, query,
    response::Rows,
};

#[derive(Query, Response)]
#[derive(Clone, PartialEq, Debug)] // we just do these for the assert (they are not needed)
struct User {
    userid: String,
    pass: String,
    followers: u64,
    email: Option<String>,
}

let our_user = User { userid: "user".into(), pass: "pass".into(), followers: 120, email: None };

let mut db = Config::new_default("username", "password").connect().unwrap();

// insert data
let q = query!("insert into myspace.mymodel(?, ?, ?, ?)", our_user.clone());
db.query_parse::<()>(&q).unwrap();

// select data
let user: User = db.query_parse(&query!("select * from myspace.mymodel where username = ?", &our_user.userid)).unwrap();
assert_eq!(user, our_user);

// select multiple rows
let users: Rows<User> = db.query_parse(&query!("select all * from myspace.mymodel limit ?", 1000u64)).unwrap();
assert_eq!(users[0].userid, "user");

§Getting started

Make sure you have Skytable set up. If not, follow the official installation guide here and then come back here.

Let’s run our first query. Connections are set up using the Config object and then we establish a connection by calling Config::connect or use other functions for different connection configurations (for TLS or async).

use skytable::{Config, query};
let mut db = Config::new_default("username", "password").connect().unwrap();
// the query `sysctl report status` returns a `Response::Empty` indicating that everything is okay. This is equivalent to
// rust's unit type `()` so that's what the driver uses
db.query_parse::<()>(&query!("sysctl report status")).unwrap();

We used the query! macro above which allows us to conveniently create queries when we don’t need to handle complex cases and we have a fixed number of arguments.

§Diving in

Now let’s say that we have a model create model myspace.mymodel(username: string, password: string, followers: uint64) and we want to do some DML operations. Here’s how we do it.

use skytable::{Config, query};
let mut db = Config::new_default("username", "password").connect().unwrap();

let insert_query = query!("insert into myspace.mymodel(?, ?, ?)", "sayan", "pass123", 1_500_000_u64);
db.query_parse::<()>(&insert_query).unwrap(); // insert will return empty on success

let select_query = query!("select password, followers FROM myspace.mymodel WHERE username = ?", "sayan");
let (pass, followers): (String, u64) = db.query_parse(&select_query).unwrap();
assert_eq!(pass, "pass123");
assert_eq!(followers, 1_500_000_u64);

let update_query = query!("update myspace.mymodel set followers += ? where username = ?", 1u64, "sayan");
db.query_parse::<()>(&update_query).unwrap(); // update will return empty on success

Now you’re ready to run your own queries!

§Going advanced

You can use the Query and Response derive macros to directly pass complex types as parameters and read as responses. This should cover most of the general use-cases (otherwise you can manually implement them).

§Need help? Get help!

Jump into Skytable’s official Discord server where maintainers, developers and fellow users help each other out.

Re-exports§

pub use config::Config;
pub use error::ClientResult;
pub use query::Pipeline;
pub use query::Query;

Modules§

aio
Asynchronous database I/O
config
Configuration
error
Client errors
pool
Connection pooling
query
Queries
response
Responses
syncio
Synchronous database I/O

Macros§

pipe
The pipe! macro is used to create pipelines
query
This macro can be used to create a Query, almost like a variadic function

Structs§

Connection
A skyhash/TCP connection
ConnectionAsync
An async skyhash/TCP connection
ConnectionTls
A skyhash/TLS connection
ConnectionTlsAsync
An async skyhash/TLS connection

Derive Macros§

Query
The Query derive macro enables you to directly pass complex types as parameters into queries
Response
The Response derive macro enables you to directly pass complex types as parameters into queries