---
name: sqlx-database
description: Async Rust SQL toolkit with compile-time checked queries.
metadata:
  labels: [rust, sqlx, database, async, postgresql]
  triggers:
    files: ['**/*.rs', 'sqlx-data.json']
    keywords: [sqlx, query, query_as, PgPool]
---

# SQLx Standards

## Connection Pool

```rust
use sqlx::postgres::PgPoolOptions;

let pool = PgPoolOptions::new()
    .max_connections(5)
    .connect("postgres://user:pass@localhost/db")
    .await?;

// Or from environment
let pool = PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
```

## Compile-Time Checked Queries

```rust
// Requires DATABASE_URL at compile time
let user = sqlx::query_as!(
    User,
    "SELECT id, name, email FROM users WHERE id = $1",
    user_id
)
.fetch_one(&pool)
.await?;

// Query with type override
let count = sqlx::query_scalar!(
    r#"SELECT COUNT(*) as "count!" FROM users"#
)
.fetch_one(&pool)
.await?;
```

## Runtime Queries

```rust
use sqlx::{query, query_as, FromRow};

#[derive(FromRow)]
struct User {
    id: i64,
    name: String,
    email: String,
}

// Named struct mapping
let users: Vec<User> = query_as("SELECT * FROM users WHERE active = $1")
    .bind(true)
    .fetch_all(&pool)
    .await?;

// Dynamic query
let user = query("SELECT * FROM users WHERE id = $1")
    .bind(user_id)
    .fetch_optional(&pool)
    .await?;
```

## Fetch Methods

| Method | Returns | Use Case |
|--------|---------|----------|
| `fetch_one` | `T` | Exactly one row expected |
| `fetch_optional` | `Option<T>` | Zero or one row |
| `fetch_all` | `Vec<T>` | All rows in memory |
| `fetch` | `Stream<T>` | Large result sets |

## Transactions

```rust
let mut tx = pool.begin().await?;

sqlx::query("INSERT INTO users (name) VALUES ($1)")
    .bind(&user.name)
    .execute(&mut *tx)
    .await?;

sqlx::query("INSERT INTO audit_log (action) VALUES ($1)")
    .bind("user_created")
    .execute(&mut *tx)
    .await?;

tx.commit().await?;

// Or automatic rollback on drop
```

## Migrations

```bash
# Create migration
sqlx migrate add create_users_table

# Run migrations
sqlx migrate run

# Revert last migration
sqlx migrate revert
```

```rust
// Run embedded migrations at startup
sqlx::migrate!("./migrations")
    .run(&pool)
    .await?;
```

## Type Mappings

| PostgreSQL | Rust | Notes |
|------------|------|-------|
| `BIGINT` | `i64` | |
| `INTEGER` | `i32` | |
| `TEXT/VARCHAR` | `String` | |
| `BOOLEAN` | `bool` | |
| `TIMESTAMP` | `chrono::NaiveDateTime` | Requires `chrono` feature |
| `TIMESTAMPTZ` | `chrono::DateTime<Utc>` | |
| `UUID` | `uuid::Uuid` | Requires `uuid` feature |
| `JSONB` | `serde_json::Value` | Requires `json` feature |

## Best Practices

1. **Compile-time checks**: Use `query!` macros when possible
2. **Connection limits**: Match pool size to Postgres `max_connections`
3. **Prepared statements**: sqlx caches automatically per connection
4. **Offline mode**: Generate `sqlx-data.json` for CI without database
5. **Nullable columns**: Use `Option<T>` for nullable, or override with `"column!"`


---
Source attribution: ngxtm
Original project: https://github.com/ngxtm/devkit/blob/9439f27ce3fa7fe53ff9278a1ee9a44bffa70eba/rules/rust/sqlx-database/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2024

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ngxtm
Original project: https://github.com/ngxtm/devkit/blob/9439f27ce3fa7fe53ff9278a1ee9a44bffa70eba/rules/rust/sqlx-database/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering
Roles: Backend Developer, Data Engineer, Database Administrator
Specialties: Backend, Database
Tools: PostgreSQL
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 7bf8742f180ff3e7760d004ff9ff3c70d46c6d4bfbfc4a7b06d0d1a881fa4d22
