MCPcopy Index your code
hub / github.com/cornucopia-rs/cornucopia

github.com/cornucopia-rs/cornucopia @cornucopia-v1.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release cornucopia-v1.0.0 ↗ · + Follow
1,224 symbols 2,329 edges 141 files 117 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Cornucopia logo

Cornucopia

Generate type-checked Rust from your SQL

Crates.io Book License Dependencies

[!NOTE] Cornucopia 1.0 merged the Clorinde fork back into the original project, adopting its rewritten codegen, expanded capabilities, and accumulated fixes. Huge thanks to @beanpuppy and the Clorinde contributors for their work. If you are upgrading from Cornucopia 0.9.x, see the migration guide.

Cornucopia generates type-checked Rust interfaces from PostgreSQL queries, with an emphasis on compile-time safety and high performance. It works by preparing your queries against an actual database and then running an extensive validation suite on them. Rust code is then generated into a separate crate, 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 a crate with type-safe interfaces to those queries.
  3. Import and use the generated code in your project.

You can learn more about Cornucopia by reading the book, or you can get a quickstart by looking at the examples.

Key Features

  • Type Safety - Catch SQL errors at compile time with powerful diagnostics.
  • SQL-First - Write plain SQL queries, get generated Rust code. No ORMs or query builders, just the SQL you know and love.
  • Fast - Performance close to hand-written rust-postgres code.
  • Flexible - Works with sync/async code and connection pools.
  • PostgreSQL Native - Full support for custom types, enums, and arrays. Leverage PostgreSQL's advanced features without compromise.
  • Custom Types - Map database types to your own Rust structs.

Installation

Install with:

cargo install cornucopia

Quick Example

Write your PostgreSQL queries with annotations and named parameters:

-- queries/authors.sql

--! insert_author
INSERT INTO authors
    (first_name, last_name, country)
VALUES
    (:first_name, :last_name, :country);

--! authors
SELECT first_name, last_name, country FROM authors;

Generate the crate with cornucopia, then you can import it into your project after adding it to your Cargo.toml:

cornucopia = { path = "./cornucopia" }

And use the generated crate in your code:

use cornucopia::queries::authors::{authors, insert_author};

insert_author()
    .bind(&mut client, &"Agatha", &"Christie", &"England")
    .unwrap();

let all_authors = authors().bind(&mut client).all().unwrap();

for author in all_authors {
    println!(
        "[{}] {}, {}",
        author.country,
        author.last_name.to_uppercase(),
        author.first_name,
    );
}

For more examples go to the examples directory, or head over to the book to learn more.

Prior Art

  • sqlc (Go) - Generate type-safe code from SQL
  • Kanel (TypeScript) - Generate TypeScript types from Postgres
  • jOOQ (Java) - Generate typesafe SQL from your database schema

License

Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

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.

Extension points exported contracts — how you extend this code

GenericClient (Interface)
Abstraction over multiple types of asynchronous clients. This allows you to use tokio_postgres clients and transactions [4 …
tests/codegen/codegen/src/client/async_/generic_client.rs
GenericClient (Interface)
Abstraction over multiple types of asynchronous clients. This allows you to use tokio_postgres clients and transactions [4 …
examples/custom_types/db/custom_types_codegen/src/client/async_/generic_client.rs
GenericClient (Interface)
Abstraction over multiple types of asynchronous clients. This allows you to use tokio_postgres clients and transactions [4 …
examples/basic_async_wasm/basic_async_wasm_codegen/src/client/async_/generic_client.rs
GenericClient (Interface)
Abstraction over multiple types of asynchronous clients. This allows you to use tokio_postgres clients and transactions [4 …
examples/auto_build/auto_build_codegen/src/client/async_/generic_client.rs
GenericClient (Interface)
Abstraction over multiple types of asynchronous clients. This allows you to use tokio_postgres clients and transactions [4 …
examples/basic_async/basic_async_codegen/src/client/async_/generic_client.rs
StringSql (Interface)
(no doc) [5 implementers]
examples/basic_sync/basic_sync_codegen/src/type_traits.rs
StringSql (Interface)
(no doc) [5 implementers]
tests/codegen/codegen/src/type_traits.rs
StringSql (Interface)
(no doc) [5 implementers]
examples/custom_types/db/custom_types_codegen/src/type_traits.rs

Core symbols most depended-on inside this repo

map
called by 129
tests/codegen/codegen/src/queries/stress.rs
map
called by 102
src/parser.rs
name
called by 99
src/config.rs
slice_iter
called by 64
tests/codegen/codegen/src/utils.rs
one
called by 64
tests/codegen/codegen/src/client/sync.rs
opt
called by 64
tests/codegen/codegen/src/client/sync.rs
raw
called by 64
tests/codegen/codegen/src/client/sync.rs
execute
called by 52
tests/codegen/codegen/src/client/async_/deadpool.rs

Shape

Method 476
Class 359
Function 325
Interface 40
Enum 24

Languages

Rust100%

Modules by API surface

tests/codegen/codegen/src/queries/syntax.rs93 symbols
src/parser.rs57 symbols
src/config.rs50 symbols
tests/codegen/codegen/src/queries/stress.rs48 symbols
tests/codegen/codegen/src/queries/nullity.rs40 symbols
benches/generated/src/queries/bench.rs36 symbols
examples/basic_sync/basic_sync_codegen/src/queries/module_2.rs33 symbols
examples/basic_async_wasm/basic_async_wasm_codegen/src/queries/module_2.rs33 symbols
examples/basic_async/basic_async_codegen/src/queries/module_2.rs33 symbols
tests/codegen/codegen/src/queries/named.rs31 symbols
tests/codegen/codegen/src/queries/params.rs27 symbols
src/prepare_queries.rs27 symbols

Datastores touched

mydbDatabase · 1 repos
postgresDatabase · 1 repos

For agents

$ claude mcp add cornucopia \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact