Name pending (currently DB sets)
https://github.com/jayy-lmao/sql-db-set-macros
Inpsired a little bit by Toasty rs and my work on sql-gen. Idea is to implement the most common SQLX queries, but allow you to still ultimately use SQLX for anything more complex than a basic query.
Currently it's only for PostgresQL, so that I can dogfood. But PRs welcome.
Why not X
Why not SQLX?
I love sqlx. It's a delight to work with. I like writing SQL.
That's why this is sqlx. This macro allows me to continue writing SQLX, but just shaves off some sqlx prepare
cycles, and some boring same queries you write over and over again.
Ultimately I still just want to write SQLX, but just save myself all the by id
style queries.
Why not SeaORM? I've used SeaORM before at a job. I find it verbose.
Why not Diesel? I like the look of diesel. I don't like dedicated single-use config filetypes. Might end up having to write one though for my codegen tool, so maybe I'll eat those words.
Why not Toasy? Same as diesel. And it's not out yet. But I must like the look of it a little, as I took inspiration from its model definitions.
Current features
Can currently:
- Query one
- Query many
- Insert (ignoring auto fields)
- Update
- Delete
- Support for enums
- Create a version of https://github.com/jayy-lmao/sql-gen for generating these
Roadmap
TODO:
- Allow for multiple-field primary keys for query-one
- Allow query many by one key field when there are two key fields
- Update
- Delete
- [~] Release early version!
- Support for optional enums
- Limit / Offset
- Do more than just
eq
to match fields (map of ops for each type)
Examples
// DbSet also implements sqlx::FromRow by default
// Used for queries, will be used for codegen
// Fetch one user
let user: User = one
.id_eq // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.fetch_one
.await?;
let user_maybe: = one
.id_eq // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.fetch_optional
.await?;
// We can also just write regular SQLX queries.
// DbSet implements FromRow for your struct also.
let same_user_again = query_as!
.fetch_one
.await?;
// Fetch all users
let users = many
.fetch_all // Can call without setting fields to match to get all results
.await?;
// Fetch many users with one field
let users = many
.name_eq // Can set fields to match on
.fetch_all
.await?;
// Fetch many users with multiple fields
let users = many
.name_eq
.details_eq // Can set multiple fields to match on
.fetch_all
.await?;
// Insert a user
let inserted_user = insert
.id
.email
.name
.insert // Due to type-state insert can't be called until all non-nullable (besides auto) fields have been set
.await?;
// Update a user
user.details = Some;
user.email = String from;
update
.data
.update
.await?;
// Delete a user
one
.id_eq // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.delete
.await?;