Cornucopia is a tool powered by rust-postgres
designed to generate type-checked Rust interfaces from your PostgreSQL queries. It works by preparing your queries against an actual database and then running an extensive validation suite on them. Once the queries are prepared and validated, Rust code is generated into a module, which can be imported and used in your project.
The basic premise is thus to:
Compared to other Rust database interfaces, Cornucopia's approach has the benefits of being simple to understand while also generating code that is both ergonomic and free of heavy macros or complex generics. Since Cornucopia generates plain Rust structs, you can also easily build upon the generated items.
Here are some defining features:
rust-postgres
performance as we can make it.You can learn more about using Cornucopia by reading our book, or you can get a quickstart by looking at our examples.
The book is the place to go to get more in-depth explanations, but here is the simplest of tasters to give you an idea.
Let's say you have the following PostgreSQL queries
-- queries/some_query_file.sql
--! authors
SELECT first_name, last_name, country FROM Authors;
--! insert_author
INSERT INTO Authors(first_name, last_name, country)
VALUES (:first_name, :last_name, :country)
Notice the query annotations (--! authors
, --! insert_authors
) and the named bind parameters (:first_name
, etc.).
Then, after generating the Rust code with Cornucopia's CLI, you can import it into your project like so:
mod cornucopia;
use cornucopia::{authors, insert_author};
Finally here is an example usage of these queries:
insert_author.bind(&client, "Agatha", "Christie", "England");
let all_authors = authors().bind(&client).all();
for author in all_authors {
println!("[{}] {}, {}",
author.country,
author.last_name.to_uppercase(),
author.first_name
)
}
You can customize pretty much every aspect of your queries easily with Cornucopia (custom parameters and row structs, renaming, nullity control, etc.), so please head over to the book if you're interested to learn more.
This crate uses Rust 2021 edition, which requires at least version 1.62.1.
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.