25 releases (8 breaking)

0.9.0 Nov 7, 2022
0.7.4 May 6, 2022

#1874 in Database interfaces

Download history 490/week @ 2024-06-22 518/week @ 2024-06-29 364/week @ 2024-07-06 531/week @ 2024-07-13 568/week @ 2024-07-20 570/week @ 2024-07-27 426/week @ 2024-08-03 660/week @ 2024-08-10 332/week @ 2024-08-17 313/week @ 2024-08-24 357/week @ 2024-08-31 456/week @ 2024-09-07 446/week @ 2024-09-14 575/week @ 2024-09-21 603/week @ 2024-09-28 592/week @ 2024-10-05

2,292 downloads per month

MIT/Apache

115KB
3K SLoC

Cornucopia

Generate type-checked Rust from your SQL


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:

  1. Write your PostgreSQL queries.
  2. Use Cornucopia to generate Rust code.
  3. Use the generated code in your project.

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:

  • SQL-first. Your SQL is the only source of truth. No intricate ORM.
  • Powerful query validation. Catch errors before runtime, with powerful (and pretty) diagnostics.
  • Supports custom user types (composites, domains, and enums) and one-dimensional arrays.
  • Sync and async driver support, with optional pooling.
  • Ergonomic non-allocating row mapping.
  • Granular type nullity control.
  • Available as a library and a CLI.
  • As close to native 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.

A quick taste of Cornucopia

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.

MSRV

This crate uses Rust 2021 edition, which requires at least version 1.62.1.

License

Licensed under either of

Contribution

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.

Dependencies

~13–23MB
~339K SLoC