Expand description
This is an rqlite database client library with optional extra convenience.
rqlite is an easy-to-use, lightweight, distributed relational database, which uses SQLite as its storage engine. It is super-simple to deploy, operating it is very straightforward, and its clustering capabilities provide you with fault-tolerance and high-availability.
See the documentation of rqlite database for a Quick start!
rqlite_client provides a type safe Rust library API to use some rqlite database backend from your code.
There is the possibility to create type safe queries and retrieve type safe results.
It has an optional implementation for database scheme migration and rollback.
Per default the HTTP(S) requests are handled by a provided
RequestBuilder
implementation based on crate ureq.
But you can provide any implementation yourself for supporting your preferred HTTP client.
The crate supports log or tracing.
See Usage and Examples for further information!
§Features
-
default = [“monitor”, “ureq”, “url”]
-
logUses
logfor some logging. Logger need to be configured vialogcrate in your application code. -
migrationEnables support for schema migration of rqlite database. See
Migration. -
migration_embedEnables schema migration support with embedding SQL from files in the application code. See
Migration. -
monitorEnables monitor endpoints. See Monitor.
-
percent_encodingIf you disable feature
url, you have to add featurepercent_encodingto get working GET SELECT queries. -
tracingUses
tracingfor some logging. Tracing need to be configured viatracingcrate in your application code. -
ureqThe default HTTP client used for communication with the rqlite database. If you disable the feature, you have to provide an own
RequestBuilderimplementation to handle your replacement ofRequest. -
ureq_charsetEnables Non-UTF8 charset handling in
ureqrequests. -
ureq_socks_proxyEnables support of Socks Proxy Urls in
ureq. -
ureq_tlsEnables TLS support for
ureq-requests with loading certs from system store. -
ureq_webpkiEnables TLS support for
ureq-requests with only embedded Mozilla cert store. -
urlUses per default
url::Urlinstead of string manipulation andpercent_encoding.
§Support, Issues, Contributing
rqlite_client is an Open Source project and everybody should be encouraged to contribute
with the individual possibilities to improve the project and its results.
If you need help with your development based on this library, you may ask questions and for assistance in the Github discussions.
For any assistance with rqlite database, you are requested to get in contact with the rqlite project at https://rqlite.io/community/.
You are free to create meaningful and reproducable issue reports in Github issues. Please be kind, tolerant and forbear with developers providing you a beneficial product, although it isn’t everybodys flavour and can’t be perfect :wink:
§Code contribution
You can provide Pull-Requests to the Github main branch.
It is preferred that there are no warnings with warn(clippy::pedantic) and the build needs to
be successful with forbid(unsafe_code).
The stable-toolchain is used for development, but the crate should compile back to specified MSRV in Cargo.toml.
§Running tests
For running tests you’ll need a working rqlited installation in subdirectory rqlite.
There is an rqlite/install.sh script which could do the installation for your environment, if the environment
is already supported in the script.
The test routines are looking up rqlited in a directory structure like rqlite/<arch>/rqlite/rqlited.
For testing make use of cargo.
$ cargo test --all-featuresThere is also a shell script ./test-features.sh to test all meaningful feature combinations.
$ ./test-features.shBefore crate release the tests need to be successful in all combinations with the cargo addon test-all-features:
$ cargo test-all-features§Usage
§Url encoding
By default the enabled feature url handles url encoding. If you don’t want to use url dependency, there is the possibility
to enable feature percent_encoding for handling url encoding.
One or the other needs to be enabled or the generated urls won’t be correct!
§Database scheme migration and rollback support
If you want to use database scheme migration and rollback, you have to enable migration or migration_embed feature.
See Migration
for further documentation.
§Logging
rqlite_client does some logging if there is enabled the feature log or tracing and the crates has been initialised
for logging.
§Examples
§Query database
A simple query of your local database might look like…
use std::time::Duration;
use rqlite_client::{
request_type::Get, response, Connection, Mapping, Query, Request,
RequestBuilder, response::Result,
};
let url = "http://localhost:4001";
#[cfg(feature = "url")]
let con = Connection::new(url).expect("url failed");
#[cfg(not(feature = "url"))]
let con = Connection::new(url);
let query = con
.query()
.set_sql_str_slice(&["SELECT COUNT(*) FROM tbl WHERE col = ?", "test"]);
let response_result = response::query::Query::from(query.request_run().unwrap());
if let Some(Mapping::Standard(success)) = response_result.results().next() {
let row = 0;
let col = 0;
if let Some(rows_found) = &success.value(row, col) {
println!("tbl has {rows_found} row(s)");
}
}See Query for further documentation.
§Insert data
To insert data you have to use
Connection::execute()
and request_type::Post.
use std::time::Duration;
use rqlite_client::{
request_type::Post, response, Connection, Mapping, Query, Request,
RequestBuilder, response::Result,
};
let url = "http://localhost:4001";
#[cfg(feature = "url")]
let con = Connection::new(url).expect("url failed");
#[cfg(not(feature = "url"))]
let con = Connection::new(url);
let query = con
.execute()
.push_sql_str_slice(&["INSERT INTO tbl (col) VALUES (?)", "test"]);
let response_result = response::query::Query::from(query.request_run().unwrap());
if let Some(Mapping::Execute(success)) = response_result.results().next() {
println!("last inserted primary key {}", success.last_insert_id);
}See Query for further documentation.
Re-exports§
pub use ureq;
Modules§
- consistency_
level - rqlite support various Read-
ConsistencyLevel - endpoint
- The supported
Endpoints execute, nodes, query, readyz, request, status - freshness
- Limiting read staleness with
Freshness - migration
Migrationand rollback of database definition with SQL- monitor
Monitorstates forQuery<State>- request_
type - HTTP
RequestTypes forGetandPost - response
- Everything related to rqlite database server
responsehandling - state
- Consistency states for
Query<State> - timeout
Timeoutparameter forQuery
Macros§
- embed_
migrations - Embed a provided directory structure with file content compressed into crate
- map_
deserialized - Generate implementations of
TryFrom<Mapping>for astructure - varparam
- Handle different convertable
Values in a slice
Structs§
- Connection
- Create and configure your
Connectionand get aQuery - Query
- Builder for the SQL statement
Query - Request
- Implemented
Requesthandling utilizing crateureq - Serde
Error - This type represents all possible errors that can occur when serializing or deserializing JSON data.
Enums§
- Data
Type - Supported
DataTypes - Error
- All obtainable
Errorvalues - Mapping
Mappingis used inresponse::ResultandQuery::results()- Response
Responseenumfor handling different rqlited database server responses- Value
- Represents any valid JSON value.
Constants§
- BUILD_
TIME - Build time stamp of the crate
Traits§
- Request
Builder - Trait
RequestBuilderis implemented to build a runnable HTTP request - Request
Type - Trait
RequestTypeofRequest