Cornucopia

Apr 29, 2024 | Programming

Generate type-checked Rust from your SQL


Overview

Cornucopia is a powerful tool powered by rust-postgres specially designed to generate type-checked Rust interfaces from your PostgreSQL queries. It works its magic through a systematic process:

  • Write your PostgreSQL queries.
  • Use Cornucopia to generate Rust code.
  • Utilize the generated code in your project.

What sets Cornucopia apart from other Rust database interfaces is its simplicity and the ergonomically designed code, devoid of heavy macros or complex generics. The generated Rust structs are straightforward, allowing for easy modifications and enhancements.

Features

Cornucopia shines with a variety of defining features:

  • SQL-first approach, treating your SQL as the primary source of truth.
  • Robust query validation to catch errors before runtime.
  • Support for custom user types, including composites, domains, 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 command-line interface (CLI).
  • Performance that closely matches native rust-postgres performance.

Getting Started

To embark on your journey with Cornucopia, follow these simple steps:

A Quick Taste of Cornucopia

The following example gives a flavor of how to use Cornucopia. Let’s say you write the following PostgreSQL queries:

sql
-- 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 and the named bind parameters.

Generating Rust Code

After generating the Rust code using Cornucopia’s CLI, you can import it into your project like this:

rust
mod cornucopia;
use cornucopia::{authors, insert_author};

Here’s how you might use these queries in your Rust project:

rust
insert_author.bind(client, "Agatha", "Christie", "England");
let all_authors = authors().bind(client).all();
for author in all_authors {
    println!("{} {} from {}", author.first_name, author.last_name.to_uppercase(), author.country);
}

With Cornucopia, customizing your queries is easy, allowing you to manage parameters, row structures, and type nullity control effortlessly. For a deeper understanding, refer to our book.

MSRV

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

License

Cornucopia is licensed under either of the following:

Troubleshooting

In case you encounter any issues, here are some troubleshooting suggestions:

  • Verify that you are using the correct Rust version (1.62.1 or above).
  • Ensure your PostgreSQL queries are correctly formatted.
  • Check if your database connection settings are accurate.
  • Refer to the documentation for additional guidance.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox