Rust and Axum and Working with Databases

13 March 2024

As I have started a new project, I was certain that I'd choose Rust, and as I've worked with Actix in the past, I wanted to try something new, Axum. As it's been some time since my last project with Axum, I am not 100% certain about that, but so far, I don't notice any difference between these two libraries right now. That was just a quick side-note.

Candidates

There are prisma for rust, diesel.rs and SQLx. Let me explain my choices: I started with Prisma, as I knew it from the JS-ecosystem and there, it has been one of the best tools I've worked with. Seriously. Then, there's diesel, which I've worked with before and lastly, @Mo recommended SQLx to me after I ranted about diesel.

Prisma for Rust

It would be awesome IF it were complete, but it isn't (yet) and the developer hasn't committed anything to the master branch for over 2 months and I haven't gotten any response to my issue for more than three weeks. Some important functions I was missing were nested creates or the _count the original Prisma client provides on relations.

Diesel

Diesel is a bit more complex to write and it doesn't hide SQl as much as Pisma does. My biggest problem with it are error messages. Seriously. You have a little mistake and the compiler complains that trait x isn't implemented for struct y and the problems is that one of the column-names doesn't match the name of the field in your struct. That's really annoying, since the error messages mostly say: "There is something wrong". You can figure out the rest on your own. Apart from that, diesel is really thought out and easy to read and write, BUT this is the dealbreaker for me. Getting enums to work also required a 3rd-party crate.

SQLx

That's not an orm. You write your SQL-query by hand and it checks them at compile time, but it can still map your result to a struct. The built-in pool-manager is also pretty much plug and play. Writing manual SQL can have advantages and disadvantages. That's pretty much all I can say about that, but what SQLx makes way better than Diesel: It gives really good error messages. Getting my enums to work is possible, but not 100% plug and play, but good enough.

Conclusion

Prisma is by far the best, if it wasn't that limited. It supports enums out of the box, has an easy-to-read schema and so much more. The API is great and easy to use. Diesel is fully featured, but the API feels a bit inconsistent, but that's far from a problem as it's all well thought out and and when it works, it's great, but figuring out where the error is, is the biggest challenge. It also seemed to increase compile-times by quite a bit. Lastly, there's SQLx. I like it, but you really have to know SQL for it.

As always, know the right tool for the job, but I can't think of a use-case where one would choose diesel as Prisma is by far the best for some small database-operations and packages the whole sql-thing really well, but as mentioned, it has its limits. One step up would be diesel, but if you are working with more complex queries and data, you can just get the full control with SQLx. As much as I'd like to love diesel, the "competition" makes it really hard.

4 0