cratestack-sqlx
SQLx-backed Postgres delegates for CrateStack models.
Overview
cratestack-sqlx provides async model delegates backed by PostgreSQL via SQLx. It's the server-side database layer that include_schema! generates delegate implementations for.
Installation
[]
= "0.2"
= { = "0.8", = ["runtime-tokio-rustls", "postgres", "chrono", "uuid"] }
Usage
Delegate Methods
Generated by include_schema!:
use include_schema;
include_schema!;
let pool = connect.await?;
let cool = builder.build;
// Find by ID
let user: = cool.user.find_by_id.await?;
// Find many with filters
let posts = cool.post
.find_many
.where_expr
.order_by
.limit
.run
.await?;
// Create
let user = cool.user.create.await?;
// Update
let user = cool.user.update.await?;
// Delete
cool.user.delete.await?;
Projections
Select specific fields and relations:
let user = cool.user
.find_unique
.select
.run
.await?;
Transactions
use Acquire;
let mut tx = pool.begin.await?;
let account = find_by_id.await?;
update_balance.await?;
update_balance.await?;
tx.commit.await?;
Transaction Isolation
Use explicit isolation for procedures:
mutation procedure transfer(amount: Decimal, to: String): Transfer {
@isolation("serializable")
}
// Generated procedure wrapper handles isolation
let result = cool.transfer.await?;
Audit Integration
Enable audit on models:
model Transfer {
id String @id
amount Decimal
@@audit
}
Audit events are recorded transactionally with the mutation via AuditSink.
Decimal Backend
Select backend at compile time:
[]
= ["decimal-rust-decimal"] # 128-bit fixed
= ["cratestack-sqlx/decimal-bigdecimal"]
Related Crates
cratestack-core- Core typescratestack-sql- Dialect-agnostic SQL primitivescratestack-rusqlite- SQLite backend (on-device, sync)
License
MIT