25 releases (8 breaking)

0.9.0 Nov 7, 2022
0.7.4 May 6, 2022

#2213 in Database interfaces

Download history 604/week @ 2024-08-09 356/week @ 2024-08-16 323/week @ 2024-08-23 325/week @ 2024-08-30 471/week @ 2024-09-06 402/week @ 2024-09-13 593/week @ 2024-09-20 602/week @ 2024-09-27 698/week @ 2024-10-04 507/week @ 2024-10-11 596/week @ 2024-10-18 487/week @ 2024-10-25 584/week @ 2024-11-01 641/week @ 2024-11-08 473/week @ 2024-11-15 253/week @ 2024-11-22

2,030 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
~349K SLoC