SQLx is my favorite PostgreSQL driver to use with Rust.

  Рет қаралды 47,020

Dreams of Code

Dreams of Code

Күн бұрын

SQLx is perhaps my favorite Rust crate for interfacing with Postgres, my favorite relational database. It provides a number of features and support that I haven't been able to find in an asynchronous framework.
In this video, I take a look at the basic usage and also some of the more advanced features that are available with feature flags.
This should provide a decent tutorial for anyone who is looking for a database driver for their Rust project.
Source code: github.com/dreamsofcode-io/ru...
Become a better developer in 4 minutes: bit.ly/45C7a29 👈
My socials:
Twitter: / dreamsofcode_io
Discord Server: / discord
#rust #postgresql #coding #tutorial #tokio
00:00 Intro
00:10 Getting started
02:10 Migrations
03:34 Writing data
04:46 Reading Data
06:46 Transactions
07:36 Feature Flags
09:05 Notifications
09:24 Outro

Пікірлер: 93
@256k_
@256k_ Жыл бұрын
this was such a good intro to DB handling in rust! definitely demystified a lot the process. i was still learning rust building up to using it for backend development by for some reason was really terrified of how it would be like but honestly its really a reasonable experience and im looking forward to getting to that point.
@dreamsofcode
@dreamsofcode Жыл бұрын
I'm really glad to hear that! Thank you for the feedback. I'm looking forward to doing some more microservice content in rust in the future as well.
@spr3ez
@spr3ez Жыл бұрын
Really love your videos. Rust and Postres are just best. keep it up!
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you! I appreciate that a lot.
@UthpalaHeenatigala
@UthpalaHeenatigala Жыл бұрын
These tutorials are brilliant. Keep going you have something here :)
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you!
@gustav_spin
@gustav_spin 11 ай бұрын
Thank you very much for the video, I was even looking for one that explained the use of sqlx.
@dreamsofcode
@dreamsofcode 11 ай бұрын
Glad it was helpful!
@Floppix93
@Floppix93 Жыл бұрын
Really love the content ! Funny, informative and on point. Keep up the great work
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you!
@alangamer50
@alangamer50 Жыл бұрын
fr, some mfs would have taken an entire video just to explain the installation
@LinuxForLife
@LinuxForLife 9 ай бұрын
Really inspiring! Thank for the share. 👍🍬
@sergp7480
@sergp7480 Жыл бұрын
the density of knowledge is insane, keep it up! I really like your rust videos. Have you thought about creating rust + iced video or any other idiomatic rust gui lib?
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you! I appreciate that. I wonder if it's perhaps too dense and I should spread it out some what 😅. I haven't yet but I will look into it! I've wanted to do a gui library video for a while so perhaps now is a good excuse to do so!
@coderaiders-yt
@coderaiders-yt 9 ай бұрын
I was having a horrific day trying to connect to my AWS Postgres db in Rust, until this video. Thank you!!
@dmytrozelinskyi4824
@dmytrozelinskyi4824 Ай бұрын
Short and clear. Thank you !
@user-pj9qk9ki2d
@user-pj9qk9ki2d 9 ай бұрын
very helpful, thank you!
@xit
@xit Жыл бұрын
Learned a lot of new stuffs. Thanks! :D Subbed!
@dreamsofcode
@dreamsofcode Жыл бұрын
Oh awesome! I'm really glad you enjoyed it. Thank you!
@tumi_tigur
@tumi_tigur Жыл бұрын
Very good video, cant believe im excited to test an sql package
@dreamsofcode
@dreamsofcode Жыл бұрын
I'm glad it inspired you!
@jacoblaney3011
@jacoblaney3011 Жыл бұрын
Really dig the video. Dig all of your vids actually. I just have one question: what next? I get this is more of a theory driven video, showing how interacting with a Postgres database can be done via Rust using a rust crate but how might one use something like this in a real application outside of just changing the params and CRUD code in your IDE? Maybe showing what it takes to have a JavaScript front end that invokes a Rust backend? Or even just a command line interaction?
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you for the feedback. That's a great question. I think a video that goes over the entire flow of creating a microservices in rust might be beneficial to help tie everything together!
@clusterdriven
@clusterdriven 5 ай бұрын
Great video!
@lautaroblasco
@lautaroblasco 9 ай бұрын
Good video bro
@ikys9143
@ikys9143 Жыл бұрын
Came from diesel headache. Great content. Subscribed..
@dreamsofcode
@dreamsofcode Жыл бұрын
I'm glad you liked it!
@minzmango1915
@minzmango1915 Жыл бұрын
Man, you're a blessing. I don't really care about Rust that much tbh, but I just love your videos
@dreamsofcode
@dreamsofcode Жыл бұрын
Oh thank you, I appreciate that a lot 🙏 Anything you wanna see? I'm still figuring out what I wanna niche down on!
@minzmango1915
@minzmango1915 Жыл бұрын
@@dreamsofcode Since you seem to like it, I'd be interested in a Postgres deep dive (like interesting features it has that other DBMS don't or something like that). But honestly I'll consume whatever you upload
@dreamsofcode
@dreamsofcode Жыл бұрын
Ok awesome. I have a lot I can dig into with postgres!
@ruleofnine5905
@ruleofnine5905 8 ай бұрын
if anyone else the " the trait `Executor
@felixontheinternet4784
@felixontheinternet4784 4 ай бұрын
Thanks! Helped me
@GlobalYoung7
@GlobalYoung7 Жыл бұрын
thank you 😊
@yapet
@yapet Жыл бұрын
I don’t think you need turbofish for query_as. I find it much more convenient to just add type annotation to the variable declaration, ie`let book: Book = …` God I love ocaml-ish/haskell-ish type deduction of rust.
@ant1fact
@ant1fact Жыл бұрын
I like to annotate my variables too. There are still some places like closures where you can't always avoid turbofish though
@peterboshe6217
@peterboshe6217 4 ай бұрын
A thumbs up solely for the mean girl's reference. solid and informational material but yeah, mean girls.
@luiscarlosjayk
@luiscarlosjayk 5 күн бұрын
Hey, think you missed to mention the cargo sqlx prepare command, and offline mode.
@vencler
@vencler Жыл бұрын
Hey, unrelated question/suggestion: would you be willing to create a video covering your setup (tooling, apps, themes, best practices, anything usefuf/interesting in regards to software/hardware.)? If so, that would be dope. :)
@dreamsofcode
@dreamsofcode Жыл бұрын
I absolutely would love to! I have so many videos I could make on it haha
@vencler
@vencler Жыл бұрын
@@dreamsofcode i turned on the notifications, looking forward to it!
@user-mu1nd8hk1g
@user-mu1nd8hk1g 11 ай бұрын
I don't care about Rust, the video is just enjoyable 😁
@dreamsofcode
@dreamsofcode 11 ай бұрын
That means an awful lot to me! ❤️ Is there anything you'd like to see that I can create for you?
@narthanaj
@narthanaj 7 ай бұрын
Im gonna learn rust
@danraine9009
@danraine9009 Жыл бұрын
Awesome video, could you make a video on handling errors coming from poetgresql I have implemented the happy path nicely but am struggling to decode any errors for example if two email addresses match when trying to add a something to the db. Thanks again ❤️
@dreamsofcode
@dreamsofcode Жыл бұрын
Absolutely! This is a great idea
@danraine9009
@danraine9009 Жыл бұрын
@@dreamsofcode awesome thank you for your work
@brencancer
@brencancer 9 ай бұрын
i like the code editor , is it sublime, atom or vscode? linux distro??
@theintjengineer
@theintjengineer 8 ай бұрын
It looks like a highly customised (Neo)Vim [an editor]; this isn't the best part though - it's this skills with it. It's probably what he talks about on one of the videos in the same playlist ["Perfect Neovim setup for Rust"]
@Mankepanke
@Mankepanke 23 күн бұрын
It's NeoVim, inside of Tmux running the Catppuccin theme. Looks like.
@kappilino
@kappilino 8 ай бұрын
Is SQLx verifying the querys in term of securtiy aspekts as sql injections ? Because it is quite time consuming and error-prone to do it yourself. The posgres crate and rusqulite make this
@Mankepanke
@Mankepanke 23 күн бұрын
Yes, since parameters are all bound parameters.
@TheBurntHoney
@TheBurntHoney 6 ай бұрын
Why do you have the query on two seperate lines? Eg q=“” query=sqlx::query(q)
@user-hn1cy5ee5l
@user-hn1cy5ee5l 2 ай бұрын
what about choosing database driver at runtime? for example if application needs to support at least 2 databases - sqlite for tests and postgres for production?
@dreamsofcode
@dreamsofcode 2 ай бұрын
Id personally use PostgreSQL for testing via something like testcontainers. Otherwise you're not really testing your database interactions with PostgreSQL. I'll have a video coming out about that soon!
@uberloss
@uberloss 3 ай бұрын
I'm a beginner and I find bugs in the tutorial hard to figure out like why the second argument to update() is never used. What is the point of it.
@kairunhanjun
@kairunhanjun 4 ай бұрын
What %1 and $1 is for? can i put custom column like UPDATE book SET `#1`=%1 WHERE ISBN = $3?
@clusterdriven
@clusterdriven 4 ай бұрын
Is there a way to have the migrations have some programmatic sauce in them? I see the migrations in the SQL format. I wonder if there is a way to write migrations in rust instead?
@dreamsofcode
@dreamsofcode 4 ай бұрын
An ORM would be able to do migrations in Rust. SeaORM is one that's pretty popular
@hussainsharza9319
@hussainsharza9319 4 ай бұрын
Short and Simple
@mrknopester
@mrknopester Жыл бұрын
What are your thoughts on SeaORM?
@dreamsofcode
@dreamsofcode Жыл бұрын
I need to try it out, really! Although personally I tend to not use ORM's too often as I like to handcraft SQL sometimes. SeaORM does look really appealing though.
@bendo01
@bendo01 7 ай бұрын
can you teach us using sqlxmq ?
@ruizdiazever
@ruizdiazever 11 ай бұрын
Is very fast and simple buy with Axum change everything! D:
@johnnyblack4261
@johnnyblack4261 7 ай бұрын
WHich one should I use sqlx or sea-orm?
@dreamsofcode
@dreamsofcode 7 ай бұрын
It depends on what your personal preference is! I typically prefer raw SQL to ORMs as I feel it gives a greater level of understanding, but not everyone thinks the same way as I do
@johnnyblack4261
@johnnyblack4261 7 ай бұрын
@@dreamsofcode I see mate, after taking a look between the two, I think I would prefer Sea_orm as you are not using strings to do select statements etc, easily using strings to do select statements can lead to bugs as you can compile it with a syntax error, at least with seaorm it will fail to compile, plus you get auto suggestions.
@dreamsofcode
@dreamsofcode 7 ай бұрын
@@johnnyblack4261 I think if that works for you, you should go for it! I tend to make use of testing to be sure that the sql statements work, rather than fully trusting the compiler when it comes to databases!
@johnnyblack4261
@johnnyblack4261 7 ай бұрын
@@dreamsofcode Ah right makes sense. I actually got another question, if I wanted to have a (small) database webserver and I wanted to use seaorm, would the seaorm code run on the client's software?
@dreamsofcode
@dreamsofcode 7 ай бұрын
@@johnnyblack4261 for web applications, generally no. You should have an API that is called and the database connection live on the server.
@SandwichMitGurke
@SandwichMitGurke Жыл бұрын
and why do you prefer it over diesel?
@dreamsofcode
@dreamsofcode Жыл бұрын
Diesel isn't async, and it's an ORM. So those are the two considerations I have against it.
@spr3ez
@spr3ez Жыл бұрын
Whats you neovim theme?
@dreamsofcode
@dreamsofcode Жыл бұрын
Catppuccin! It's probably my favorite color scheme
@spr3ez
@spr3ez Жыл бұрын
@@dreamsofcode yeah its really cool! thanks ima try it, I already use a very similar one, so probably gonna switch
@CorneliusCornbread
@CorneliusCornbread Жыл бұрын
Why use tokio over the standard async library?
@dreamsofcode
@dreamsofcode Жыл бұрын
You still need a runtime for async in Rust. I believe there's only async_std, tokio and smol. Tokio is the most popular of the three.
@coffee-is-power
@coffee-is-power 10 ай бұрын
You completely forgot the sqlx macros, they're really amazing, they run the queries at compile time on your local database to check if they're valid
@dreamsofcode
@dreamsofcode 10 ай бұрын
The macros are great!
@kurt7020
@kurt7020 11 ай бұрын
"I'd also consider using it for other databases such as mysql" - Face it, there's no way you're ever going back to mysql. XD
@dreamsofcode
@dreamsofcode 11 ай бұрын
🤣🤣🤣 bruh how you know me so well
@sergp7480
@sergp7480 Жыл бұрын
4:39 - typo &sqlx::PgPool not &sqlx:PgPool 4:43 - typo $1 not %1
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you for catching those!
@tiagocerqueira9459
@tiagocerqueira9459 7 ай бұрын
I need your theme
@sharkpyro93
@sharkpyro93 11 ай бұрын
what about relationships? there is not 1 single example online on how to handle relationships with sqlx
@dreamsofcode
@dreamsofcode 11 ай бұрын
Are you talking about joining data sets using a SQL JOIN and returning the data model?
@sharkpyro93
@sharkpyro93 11 ай бұрын
@@dreamsofcode i was referring to provide the query_as() with a model that has some nested structs with different relationships mapping to them automatically, turns out is not doable, its either i go full on raw sql and setting the results manually, or using an orm but diesel orm stinks and seaorm is terrible too, guess i cannot use rust
@alexandrucomanescu9857
@alexandrucomanescu9857 7 ай бұрын
@@sharkpyro93 I came to the same conclusion. On top of that relationships in SeaORM, according to the docs, have to be done using the lazy concept when you have more than 2 relationships. Which is the same as using sqlx and then manually mapping the results. I invested a lot more than I was anticipating.
@sharkpyro93
@sharkpyro93 7 ай бұрын
@@alexandrucomanescu9857 yea rust is not a good fit for apps that need intense db crud, takes forever to manually write everything, also its not a safe approach
@Mankepanke
@Mankepanke 23 күн бұрын
I suppose you do it the same way as you always do when not working with an ORM. Is there something specific that you don't find to be working well?
Tide is perhaps my favorite Rust web app framework.
9:14
Dreams of Code
Рет қаралды 94 М.
MUST know Rust database libraries
5:50
Let's Get Rusty
Рет қаралды 39 М.
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 175 МЛН
Super gymnastics 😍🫣
00:15
Lexa_Merin
Рет қаралды 89 МЛН
Balloon Stepping Challenge: Barry Policeman Vs  Herobrine and His Friends
00:28
Each found a feeling.#Short #Officer Rabbit #angel
00:17
兔子警官
Рет қаралды 7 МЛН
Como Desenvolver Aplicações Rust na AWS?
34:29
iFood Universo
Рет қаралды 3,1 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 173 М.
Why JavaScript Devs are Switching to Rust in 2024
10:35
warpdotdev
Рет қаралды 246 М.
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 330 М.
Go application setup with PostgreSQL, sqlx, goose migrations
9:38
Make Programming Fun Again
Рет қаралды 2 М.
We can now write scripts using these languages, but is it worth it?
8:28
Using docker in unusual ways
12:58
Dreams of Code
Рет қаралды 400 М.
Rust & SQL Databases (With Diesel)
15:00
Code to the Moon
Рет қаралды 61 М.
How Rust rose to dominance (by accident)
11:01
Let's Get Rusty
Рет қаралды 33 М.
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 5 МЛН
keren sih #iphone #apple
0:16
Muhammad Arsyad
Рет қаралды 1,6 МЛН
Мечта Каждого Геймера
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,2 МЛН
Mem VPN - в Apple Store
0:30
AndroHack
Рет қаралды 94 М.