https://github.com/rkusa/sqlm
An `sql!` macro to write compile-time checked database queries similar to how `format!` works
https://github.com/rkusa/sqlm
postgres postgresql rust sql
Last synced: 7 months ago
JSON representation
An `sql!` macro to write compile-time checked database queries similar to how `format!` works
- Host: GitHub
- URL: https://github.com/rkusa/sqlm
- Owner: rkusa
- License: apache-2.0
- Created: 2023-06-17T13:34:13.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-11T17:30:09.000Z (about 1 year ago)
- Last Synced: 2025-04-18T20:33:18.336Z (12 months ago)
- Topics: postgres, postgresql, rust, sql
- Language: Rust
- Homepage: https://docs.rs/sqlm-postgres
- Size: 311 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# `sqlm`
An `sql!` macro to write compile-time checked database queries similar to how `format!` works.
[Documentation](https://docs.rs/sqlm-postgres)
## Example
```rust
let id: i64 = 1;
let user: User = sql!("SELECT * FROM users WHERE id = {id}").await?;
#[derive(Debug, FromRow)]
struct User {
id: i64,
name: String,
role: Role,
}
#[derive(Debug, Default, FromSql, ToSql, Enum)]
#[postgres(name = "role")]
enum Role {
#[default]
#[postgres(name = "user")]
User,
#[postgres(name = "admin")]
Admin,
}
```
## Usage
- Add `sqlm-postgres` to your dependencies
```bash
cargo add sqlm-postgres
```
- Make the `DATABASE_URL` env variable available during compile time (e.g. via adding an `.env` file)
```bash
echo DATABASE_URL=postgres://your-user@localhost/your-db > .env
```
- Start using the `sql!` macro (no further setup necessary; a connection pool is automatically created for you)
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option.
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.