#toql

toql_rocket

Rocket integration for toql

6 releases

✓ Uses Rust 2018 edition

0.1.5 May 20, 2019
0.1.4 May 20, 2019

#3 in #toql

38 downloads per month

MIT/Apache

771KB
193 lines

Toql Rocket

Description

Use Toql with Rocket. This crate adds:

  • Toql URL query parameters
  • HTTP response for Toql errors
  • load_many() function that works with URL query parameters
  • Response headers with count information

Resources

There is a CRUD example and the API documentation.

Installation

Add this to your Cargo.toml:

[dependencies]
toql_rocket = { version = "0.1", features= ["mysql"] }

License

Toql Rocket is distributed under the terms of both the MIT license and the Apache License (Version 2.0).


lib.rs:

Rocket integration of Toql. This contains

  • A high level function to query Toql structs.
  • Query parameters.
  • Support to add counting information to HTTP response headers

This allows to query Toql structs like this

#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;

use toql_rocket::{ToqlQuery, Counted, Result, mysql::load_many, toql::sql_mapper::SqlMapperCache};
use rocket::request::Form;
use rocket_contrib::json::Json;
use myql::Conn;

#[database("example_db")]
struct ExampleDbConnection(mysql::Conn);

struct User {id:u64, username: Option<String>};

#[get("/?<toql..>")]
fn query( mappers: State<SqlMapperCache>,
              conn: ExampleDbConnection, 
              toql: Form<ToqlQuery>)
-> Result<Counted<Json<Vec<User>>>> {
   let ExampleDbConnection(mut c) = conn;

   let r = load_many::<User>(&toql, &mappers, &mut c)?;
   Ok(Counted(Json(r.0), r.1))
}

Dependencies

~11MB
~198K SLoC