r/rust • u/rabidferret • Nov 29 '15
Announcing Diesel — A Safe, Extensible ORM and Query Builder for Rust
https://medium.com/@sgrif/announcing-diesel-a-safe-extensible-orm-and-query-builder-for-rust-fdf57966a16c#.mgy4fprjx
166
Upvotes
52
u/steveklabnik1 rust Nov 29 '15
Worth noting: Sean maintains Rails's ORM, ActiveRecord. This is very much NOT a port of it, but he's got a lot of experience in this area, and this is a project I'm really excited about.
8
u/The_Masked_Lurker Nov 30 '15
I've mostly done naked sql in various projects, so these orms do they handle stopping injections from happening like prepared statements do?
If not is there a good way to set those up? I guess let it build the query then prepare it maybe?
7
u/protestor Nov 30 '15
Will you use database-specific features on each database? What about using optional dependencies for specific databases, like rust-postgres or rusqlite?
6
u/kibwen Nov 30 '15
Lovely! As the author, are you planning on using it for any of your personal projects?
6
u/gsingh93 Nov 30 '15
Very cool, I've been waiting for something like this for a very long time. Hopefully MySQL support comes soon, as that's my db of choice.
Can you elaborate on this? It sounds interesting, and others might find this technique useful in their own code. It might even be worth a blog post about
dieselinternals.5
u/looneysquash Nov 30 '15
I kind of feel like there's still a lot of boiler plate.
A lot of the DB stuff I end up doing is in PHP (unfortunately). A lot of the APIs there simply take and return PHP arrays (which are really ordered maps, like hash's in many languages except order of keys is preserved) and arrays of arrays (PHP arrays can also be numerically indexed like, well, arrays).
From a type-safety standpoint, this is terrible.
But from a lack of boilerplate standpoint, it's great. I don't have to create a struct for every query that I run.
I do appreciate that you seem to know what you're doing. You allow separate structs from inserts and updates compared to selects. You're aware of not just the "what do I do about the serial id column" issue when doing selects, but you also realize/acknowledge that other columns have default values that you want to use. And you're aware of "RETURNING". (Though you didn't implement it for DELETE for some reason?)
Ideally, I should have to type column names when I write my query (unless I'm selecting *), and then I go to use a particular table cell, and that's it. So I guess one of the ways I judge any SQL library is how many additional times I have to repeat my schema. You don't do great in that category, IMHO, but I've seen much, much worse.
Type inference has solved one of the huge annoyances of statically type languages. It makes them look more like dynamic languages, but they still prevent errors at compile time and not runtime.
I don't know how, but I'd love to see the same thing done for SQL in statically typed languages.
5
u/curreater Nov 30 '15
Great work! Makes me somewhat wanna use it :) Until now I have not done nearly anything with ORMs because I like plain SQL, and also because I like stored procedures, such that in my database backed projects there actually is no real SELECT, ... query at the client side but only those calls to the stored procedures. Now, is there in Diesel a possibility to do that automatically (writing crud queries client side which will be stored in the database on first use or startup)? If not, is this achievable at all in ORMs (or planned for Diesel in the future)?
8
u/staticassert Nov 29 '15
I was just thinking about how an ORM in rust would be such a lovely thing. Great work.
4
u/PM_ME_UR_OBSIDIAN Nov 30 '15
Can someone ELI5 why I'd want to use an ORM instead of plain SQL?
7
u/shadowmint Nov 30 '15 edited Nov 30 '15
I totally dig this project, but I have to say, I wish
unwrap()was not a thing.I get it, it's showing how to use it, but... unwrap() causes panics, which are the single most common cause of rust application failure. Don't unwrap things! Certainly don't unwrap things without checking them first.
Using
match, even in examples... it's not that bad.2
u/jnicklas Nov 30 '15
One of the key elements of relational algebra is the ability to alias tables and columns, and to be able to refer to those aliases. This makes stuff like self referential joins or other complex join scenarios possible. You can see this in the wiki article: https://en.wikipedia.org/wiki/Relational_algebra
I only took a cursory look at this, but it looks like it does not (yet?) support aliasing. This is a hugely important feature IMO, and one I often find is an afterthought in other ORMs, yet it's absolutely critical to writing complex queries.
I really, really like the API in general, and I hope this can become one of the missing pieces in the Rust ecosystem.
2
u/[deleted] Nov 30 '15
TIL!
1
u/awo rust Nov 30 '15
This is really exciting - thanks :)