Convert Figma logo to code with AI

launchbadge logosqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.

12,950
1,235
12,950
604

Top Related Projects

12,665

A safe, extensible ORM and Query Builder for Rust

6,922

🐚 An async & dynamic ORM for Rust

🚂 Engine components of Prisma ORM

2,246

Rust Compile Time ORM robustness,async, pure Rust Dynamic SQL

Quick Overview

SQLx is a powerful and versatile Rust SQL toolkit designed to be efficient, ergonomic, and type-safe. It supports multiple databases, including PostgreSQL, MySQL, SQLite, and Microsoft SQL Server, and provides both synchronous and asynchronous APIs.

Pros

  • Strong type safety with compile-time checked queries
  • Support for multiple database systems
  • Asynchronous and synchronous APIs available
  • Extensive feature set, including migrations and connection pooling

Cons

  • Steeper learning curve compared to some other ORMs
  • Macro-heavy syntax may be less intuitive for some developers
  • Limited support for complex relationships compared to full-fledged ORMs
  • Performance overhead for compile-time checks in some cases

Code Examples

  1. Executing a simple query:
let pool = SqlitePool::connect("sqlite::memory:").await?;

let row: (i64,) = sqlx::query_as("SELECT $1")
    .bind(150_i64)
    .fetch_one(&pool).await?;

assert_eq!(row.0, 150);
  1. Using the query! macro for type-safe queries:
let pool = PgPool::connect("postgres://postgres:password@localhost/test").await?;

let row = sqlx::query!("SELECT * FROM users WHERE id = $1", 1)
    .fetch_one(&pool).await?;

println!("User: {} {}", row.first_name, row.last_name);
  1. Performing a transaction:
let mut tx = pool.begin().await?;

sqlx::query!("INSERT INTO users (name) VALUES ($1)", "Alice")
    .execute(&mut tx).await?;

sqlx::query!("UPDATE users SET active = true WHERE name = $1", "Alice")
    .execute(&mut tx).await?;

tx.commit().await?;

Getting Started

To use SQLx in your Rust project, add it to your Cargo.toml:

[dependencies]
sqlx = { version = "0.6", features = ["runtime-tokio-rustls", "postgres"] }
tokio = { version = "1", features = ["full"] }

Then, in your Rust code:

use sqlx::postgres::PgPool;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPool::connect("postgres://username:password@localhost/database").await?;
    
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool).await?;
    
    println!("Result: {}", row.0);
    
    Ok(())
}

This example sets up a connection pool, executes a simple query, and prints the result.

Competitor Comparisons

12,665

A safe, extensible ORM and Query Builder for Rust

Pros of Diesel

  • More mature and established ORM with a larger ecosystem
  • Provides a powerful query builder and type-safe SQL abstractions
  • Supports database migrations out of the box

Cons of Diesel

  • Steeper learning curve due to its more complex API
  • Requires a separate CLI tool for code generation
  • Less flexible for raw SQL queries compared to SQLx

Code Comparison

Diesel query example:

let results = users
    .filter(published.eq(true))
    .limit(5)
    .load::<Post>(&mut conn)?;

SQLx query example:

let results = sqlx::query_as!(
    Post,
    "SELECT * FROM posts WHERE published = $1 LIMIT $2",
    true,
    5
)
.fetch_all(&pool)
.await?;

Both SQLx and Diesel are popular Rust database libraries, but they have different approaches. Diesel focuses on being a full-featured ORM with a robust query builder, while SQLx emphasizes compile-time checked SQL queries and async support. SQLx is generally easier to get started with and offers more flexibility for raw SQL, while Diesel provides more powerful abstractions and type-safety for complex queries. The choice between them depends on your project's specific needs and your preferred development style.

6,922

🐚 An async & dynamic ORM for Rust

Pros of sea-orm

  • Provides a higher-level ORM abstraction with entity models and relationships
  • Supports async/await syntax for database operations
  • Offers built-in migration tools for schema management

Cons of sea-orm

  • Steeper learning curve due to more complex API and concepts
  • May introduce additional overhead compared to sqlx's lighter approach
  • Less flexible for raw SQL queries and custom database operations

Code Comparison

sea-orm example:

let order: Model = Order::find().one(db).await?;
let items: Vec<item::Model> = order.find_related(Item).all(db).await?;

sqlx example:

let order = sqlx::query_as!(Order, "SELECT * FROM orders WHERE id = ?", id)
    .fetch_one(&pool).await?;
let items = sqlx::query_as!(Item, "SELECT * FROM items WHERE order_id = ?", order.id)
    .fetch_all(&pool).await?;

sea-orm focuses on entity relationships and ORM-style queries, while sqlx provides a more direct approach to SQL execution with type-safe macros. sea-orm offers a higher level of abstraction, which can be beneficial for complex data models but may come at the cost of performance and flexibility compared to sqlx's lightweight design.

🚂 Engine components of Prisma ORM

Pros of Prisma Engines

  • More comprehensive ORM functionality with advanced features like migrations and schema management
  • Supports multiple programming languages, not just Rust
  • Provides a higher-level abstraction for database operations

Cons of Prisma Engines

  • Steeper learning curve due to its more complex architecture
  • Potentially higher overhead for simple database operations
  • Less flexibility for fine-grained control over SQL queries

Code Comparison

SQLx example:

let mut conn = pool.acquire().await?;
let user = sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?")
    .bind(user_id)
    .fetch_one(&mut conn)
    .await?;

Prisma Engines example (via Prisma Client):

const user = await prisma.user.findUnique({
  where: { id: userId },
});

While SQLx provides a more direct SQL-like interface, Prisma Engines offers a higher-level abstraction through its client libraries. SQLx is generally more lightweight and Rust-specific, while Prisma Engines supports multiple languages and provides more advanced ORM features at the cost of added complexity.

2,246

Rust Compile Time ORM robustness,async, pure Rust Dynamic SQL

Pros of rbatis

  • Supports multiple databases (MySQL, PostgreSQL, SQLite, etc.) with a unified API
  • Provides dynamic SQL generation and macro-based ORM features
  • Offers both synchronous and asynchronous operations

Cons of rbatis

  • Less mature and less widely adopted compared to sqlx
  • Documentation may be less comprehensive or up-to-date
  • Steeper learning curve due to macro-heavy approach

Code Comparison

rbatis:

#[crud_table]
#[derive(Clone, Debug)]
pub struct User {
    pub id: Option<i32>,
    pub name: String,
}

let user = User { id: None, name: "John".to_string() };
rb.save(&user, &[]).await?;

sqlx:

struct User {
    id: i32,
    name: String,
}

let user = sqlx::query_as!(User, "INSERT INTO users (name) VALUES ($1) RETURNING *", "John")
    .fetch_one(&pool)
    .await?;

Both rbatis and sqlx are Rust libraries for database operations, but they have different approaches. rbatis focuses on ORM-like features with macro-based code generation, while sqlx emphasizes type-safe SQL queries with compile-time checking. sqlx is generally considered more lightweight and closer to raw SQL, while rbatis provides higher-level abstractions for database interactions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

SQLx

🧰 The Rust SQL Toolkit


Built with ❤️ by The LaunchBadge team

Have a question? Be sure to check the FAQ first!

SQLx is an async, pure Rust† SQL crate featuring compile-time checked queries without a DSL.

  • Truly Asynchronous. Built from the ground-up using async/await for maximum concurrency.

  • Compile-time checked queries (if you want). See SQLx is not an ORM.

  • Database Agnostic. Support for PostgreSQL, MySQL, MariaDB, SQLite.

    • MSSQL was supported prior to version 0.7, but has been removed pending a full rewrite of the driver as part of our SQLx Pro initiative.
  • Pure Rust. The Postgres and MySQL/MariaDB drivers are written in pure Rust using zero unsafe†† code.

  • Runtime Agnostic. Works on different runtimes (async-std / tokio / actix) and TLS backends (native-tls, rustls).

† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way we could be pure Rust for SQLite is by porting all of SQLite to Rust).

†† SQLx uses #![forbid(unsafe_code)] unless the sqlite feature is enabled. The SQLite driver directly invokes the SQLite3 API via libsqlite3-sys, which requires unsafe.


  • Cross-platform. Being native Rust, SQLx will compile anywhere Rust is supported.

  • Built-in connection pooling with sqlx::Pool.

  • Row streaming. Data is read asynchronously from the database and decoded on demand.

  • Automatic statement preparation and caching. When using the high-level query API (sqlx::query), statements are prepared and cached per connection.

  • Simple (unprepared) query execution including fetching results into the same Row types used by the high-level API. Supports batch execution and returns results from all statements.

  • Transport Layer Security (TLS) where supported (MySQL, MariaDB and PostgreSQL).

  • Asynchronous notifications using LISTEN and NOTIFY for PostgreSQL.

  • Nested transactions with support for save points.

  • Any database driver for changing the database driver at runtime. An AnyPool connects to the driver indicated by the URL scheme.

Install

SQLx is compatible with the async-std, tokio, and actix runtimes; and, the native-tls and rustls TLS backends. When adding the dependency, you must choose a runtime feature that is runtime + tls.

# Cargo.toml
[dependencies]
# PICK ONE OF THE FOLLOWING:

# tokio (no TLS)
sqlx = { version = "0.8", features = [ "runtime-tokio" ] }
# tokio + native-tls
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-native-tls" ] }
# tokio + rustls with ring
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-rustls-ring" ] }
# tokio + rustls with aws-lc-rs
sqlx = { version = "0.8", features = [ "runtime-tokio", "tls-rustls-aws-lc-rs" ] }

# async-std (no TLS)
sqlx = { version = "0.8", features = [ "runtime-async-std" ] }
# async-std + native-tls
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-native-tls" ] }
# async-std + rustls with ring
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-rustls-ring" ] }
# async-std + rustls with aws-lc-rs
sqlx = { version = "0.8", features = [ "runtime-async-std", "tls-rustls-aws-lc-rs" ] }

Cargo Feature Flags

For backward-compatibility reasons, the runtime and TLS features can either be chosen together as a single feature, or separately.

For forward compatibility, you should use the separate runtime and TLS features as the combination features may be removed in the future.

  • runtime-async-std: Use the async-std runtime without enabling a TLS backend.

  • runtime-async-std-native-tls: Use the async-std runtime and native-tls TLS backend (SOFT-DEPRECATED).

  • runtime-async-std-rustls: Use the async-std runtime and rustls TLS backend (SOFT-DEPRECATED).

  • runtime-tokio: Use the tokio runtime without enabling a TLS backend.

  • runtime-tokio-native-tls: Use the tokio runtime and native-tls TLS backend (SOFT-DEPRECATED).

  • runtime-tokio-rustls: Use the tokio runtime and rustls TLS backend (SOFT-DEPRECATED).

    • Actix-web is fully compatible with Tokio and so a separate runtime feature is no longer needed.
  • tls-native-tls: Use the native-tls TLS backend (OpenSSL on *nix, SChannel on Windows, Secure Transport on macOS).

  • tls-rustls: Use the rustls TLS backend (cross-platform backend, only supports TLS 1.2 and 1.3).

  • postgres: Add support for the Postgres database server.

  • mysql: Add support for the MySQL/MariaDB database server.

  • mssql: Add support for the MSSQL database server.

  • sqlite: Add support for the self-contained SQLite database engine.

  • any: Add support for the Any database driver, which can proxy to a database driver at runtime.

  • derive: Add support for the derive family macros, those are FromRow, Type, Encode, Decode.

  • macros: Add support for the query*! macros, which allows compile-time checked queries.

  • migrate: Add support for the migration management and migrate! macro, which allow compile-time embedded migrations.

  • uuid: Add support for UUID (in Postgres).

  • chrono: Add support for date and time types from chrono.

  • time: Add support for date and time types from time crate (alternative to chrono, which is preferred by query! macro, if both enabled)

  • bstr: Add support for bstr::BString.

  • bigdecimal: Add support for NUMERIC using the bigdecimal crate.

  • rust_decimal: Add support for NUMERIC using the rust_decimal crate.

  • ipnetwork: Add support for INET and CIDR (in postgres) using the ipnetwork crate.

  • json: Add support for JSON and JSONB (in postgres) using the serde_json crate.

  • Offline mode is now always enabled. See sqlx-cli/README.md.

SQLx is not an ORM!

SQLx supports compile-time checked queries. It does not, however, do this by providing a Rust API or DSL (domain-specific language) for building queries. Instead, it provides macros that take regular SQL as input and ensure that it is valid for your database. The way this works is that SQLx connects to your development DB at compile time to have the database itself verify (and return some info on) your SQL queries. This has some potentially surprising implications:

  • Since SQLx never has to parse the SQL string itself, any syntax that the development DB accepts can be used (including things added by database extensions)
  • Due to the different amount of information databases let you retrieve about queries, the extent of SQL verification you get from the query macros depends on the database

If you are looking for an (asynchronous) ORM, you can check out our new Ecosystem wiki page!

Usage

See the examples/ folder for more in-depth usage.

Quickstart

use sqlx::postgres::PgPoolOptions;
// use sqlx::mysql::MySqlPoolOptions;
// etc.

#[async_std::main] // Requires the `attributes` feature of `async-std`
// or #[tokio::main]
// or #[actix_web::main]
async fn main() -> Result<(), sqlx::Error> {
    // Create a connection pool
    //  for MySQL/MariaDB, use MySqlPoolOptions::new()
    //  for SQLite, use SqlitePoolOptions::new()
    //  etc.
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect("postgres://postgres:password@localhost/test").await?;

    // Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL/MariaDB)
    let row: (i64,) = sqlx::query_as("SELECT $1")
        .bind(150_i64)
        .fetch_one(&pool).await?;

    assert_eq!(row.0, 150);

    Ok(())
}

Connecting

A single connection can be established using any of the database connection types and calling connect().

use sqlx::Connection;

let conn = SqliteConnection::connect("sqlite::memory:").await?;

Generally, you will want to instead create a connection pool (sqlx::Pool) for the application to regulate how many server-side connections it's using.

let pool = MySqlPool::connect("mysql://user:pass@host/database").await?;

Querying

In SQL, queries can be separated into prepared (parameterized) or unprepared (simple). Prepared queries have their query plan cached, use a binary mode of communication (lower bandwidth and faster decoding), and utilize parameters to avoid SQL injection. Unprepared queries are simple and intended only for use where a prepared statement will not work, such as various database commands (e.g., PRAGMA or SET or BEGIN).

SQLx supports all operations with both types of queries. In SQLx, a &str is treated as an unprepared query, and a Query or QueryAs struct is treated as a prepared query.

// low-level, Executor trait
conn.execute("BEGIN").await?; // unprepared, simple query
conn.execute(sqlx::query("DELETE FROM table")).await?; // prepared, cached query

We should prefer to use the high-level query interface whenever possible. To make this easier, there are finalizers on the type to avoid the need to wrap with an executor.

sqlx::query("DELETE FROM table").execute(&mut conn).await?;
sqlx::query("DELETE FROM table").execute(&pool).await?;

The execute query finalizer returns the number of affected rows, if any, and drops all received results. In addition, there are fetch, fetch_one, fetch_optional, and fetch_all to receive results.

The Query type returned from sqlx::query will return Row<'conn> from the database. Column values can be accessed by ordinal or by name with row.get(). As the Row retains an immutable borrow on the connection, only one Row may exist at a time.

The fetch query finalizer returns a stream-like type that iterates through the rows in the result sets.

// provides `try_next`
use futures::TryStreamExt;
// provides `try_get`
use sqlx::Row;

let mut rows = sqlx::query("SELECT * FROM users WHERE email = ?")
    .bind(email)
    .fetch(&mut conn);

while let Some(row) = rows.try_next().await? {
    // map the row into a user-defined domain type
    let email: &str = row.try_get("email")?;
}

To assist with mapping the row into a domain type, one of two idioms may be used:

let mut stream = sqlx::query("SELECT * FROM users")
    .map(|row: PgRow| {
        // map the row into a user-defined domain type
    })
    .fetch(&mut conn);
#[derive(sqlx::FromRow)]
struct User { name: String, id: i64 }

let mut stream = sqlx::query_as::<_, User>("SELECT * FROM users WHERE email = ? OR name = ?")
    .bind(user_email)
    .bind(user_name)
    .fetch(&mut conn);

Instead of a stream of results, we can use fetch_one or fetch_optional to request one required or optional result from the database.

Compile-time verification

We can use the macro, sqlx::query! to achieve compile-time syntactic and semantic verification of the SQL, with an output to an anonymous record type where each SQL column is a Rust field (using raw identifiers where needed).

let countries = sqlx::query!(
        "
SELECT country, COUNT(*) as count
FROM users
GROUP BY country
WHERE organization = ?
        ",
        organization
    )
    .fetch_all(&pool) // -> Vec<{ country: String, count: i64 }>
    .await?;

// countries[0].country
// countries[0].count

Differences from query():

  • The input (or bind) parameters must be given all at once (and they are compile-time validated to be the right number and the right type).

  • The output type is an anonymous record. In the above example the type would be similar to:

    { country: String, count: i64 }
    
  • The DATABASE_URL environment variable must be set at build time to a database which it can prepare queries against; the database does not have to contain any data but must be the same kind (MySQL, Postgres, etc.) and have the same schema as the database you will be connecting to at runtime.

    For convenience, you can use a .env file1 to set DATABASE_URL so that you don't have to pass it every time:

    DATABASE_URL=mysql://localhost/my_database
    

The biggest downside to query!() is that the output type cannot be named (due to Rust not officially supporting anonymous records). To address that, there is a query_as!() macro that is mostly identical except that you can name the output type.

// no traits are needed
struct Country { country: String, count: i64 }

let countries = sqlx::query_as!(Country,
        "
SELECT country, COUNT(*) as count
FROM users
GROUP BY country
WHERE organization = ?
        ",
        organization
    )
    .fetch_all(&pool) // -> Vec<Country>
    .await?;

// countries[0].country
// countries[0].count

To avoid the need of having a development database around to compile the project even when no modifications (to the database-accessing parts of the code) are done, you can enable "offline mode" to cache the results of the SQL query analysis using the sqlx command-line tool. See sqlx-cli/README.md.

Compile-time verified queries do quite a bit of work at compile time. Incremental actions like cargo check and cargo build can be significantly faster when using an optimized build by putting the following in your Cargo.toml (More information in the Profiles section of The Cargo Book)

[profile.dev.package.sqlx-macros]
opt-level = 3

1 The dotenv crate itself appears abandoned as of December 2021 so we now use the dotenvy crate instead. The file format is the same.

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

If the sqlite feature is enabled, this is downgraded to #![deny(unsafe_code)] with #![allow(unsafe_code)] on the sqlx::sqlite module. There are several places where we interact with the C SQLite API. We try to document each call for the invariants we're assuming. We absolutely welcome auditing of, and feedback on, our unsafe code usage.

License

Licensed under either of

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.