rust-sqlx-patterns

Implement SQLx database query patterns for reads, transactions, and dynamic filters in Rust.

Updated Feb 24, 2026
One-click install
npx skills add https://github.com/brian861105/codex-skills --skill rust-sqlx-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-sqlx-patterns
Source: https://github.com/brian861105/codex-skills/tree/main/rust-sqlx-patterns
Command: npx skills add https://github.com/brian861105/codex-skills --skill rust-sqlx-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing clean, maintainable database access in Rust by providing proven SQLx patterns for reads, writes, transactions, and dynamic queries.

Core Features & Use Cases

  • Simple Reads/Writes: wrap queries into small helper functions using PgPool and map results to domain structs.
  • Transactional Operations: use Transaction<'_, Postgres> to guarantee atomicity across statements.
  • Reusable Queries with PgExecutor: write generic queries that work with a pool or a transaction.
  • Complex/Optional Filters with SeaQuery: build dynamic filters for flexible querying.
  • Migrations: structure and guidelines for SQL migrations with sqlx.

Quick Start

Start by implementing a simple read or write against a PgPool, then upgrade to transactional blocks and a reusable PgExecutor for complex operations.

Frequently Asked Questions about rust-sqlx-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write reusable Rust SQLx queries for both PgPool and transactions?

To write reusable Rust SQLx queries, implement a PgExecutor abstraction. This allows generic query functions to execute against either a PgPool for simple reads or a Transaction for atomic operations without duplicating code.

What is the best way to handle complex and optional filters in Rust SQLx?

The best way to handle complex and optional filters in Rust SQLx is using SeaQuery. It allows you to build dynamic query filters flexibly, mapping results directly to domain structs while maintaining clean data-access layers.

How do I guarantee atomicity across multiple database statements in Rust SQLx?

To guarantee atomicity across multiple database statements in Rust SQLx, use Transaction<'_, Postgres>. This ensures transactional operations either fully commit together or roll back, maintaining data integrity across writes.

How should I structure SQL migrations when using sqlx in Rust?

You should structure SQL migrations using established sqlx guidelines. This involves organizing migration files sequentially to ensure database schema changes are tracked and applied reliably within your Rust application.

Can I use SQLx patterns for simple reads and writes in Rust without complex setups?

Yes, you can use SQLx patterns for simple reads and writes by wrapping queries into small helper functions. These functions use PgPool to execute queries and map the results directly to your Rust domain structs.