Rust & SQL Databases (With Diesel)

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 116

  • @tobias3581
    @tobias3581 Před 2 lety +58

    Such a fun way to learn Rust by watching these. Totally sold on Rust at this point. A video about Tauri would be very interesting

    • @codetothemoon
      @codetothemoon  Před 2 lety +10

      Very happy you found it valuable! Tauri is definitely on my radar, will try to make a video on it at some point.

    • @tobias3581
      @tobias3581 Před 2 lety

      @@codetothemoon 🥳

  • @nscarlos25
    @nscarlos25 Před 2 lety +14

    Really great video! Diesel always seemed very complicated, but your explanations and code were very good. As a suggestion for future video, maybe SQLX can provide a good comparison ground for those who are deciding which one to use. Also, I was wondering if there's an ORM that has async support. That can also make a nice comparison video.

    • @codetothemoon
      @codetothemoon  Před 2 lety +3

      Thank you, I'm glad you found it valuable! and re: SQLX and async ORMs, I'll put these on the potential video roadmap!

    • @ejazahmed4609
      @ejazahmed4609 Před 2 lety +3

      Does not have a lot of experience with it but SeaQL/sea-orm looks great. It's async and is based off of sqlx.

    • @nscarlos25
      @nscarlos25 Před 2 lety

      @@ejazahmed4609 wasn't aware of this ORM but it looks really good!

    • @jarvenpaajani8105
      @jarvenpaajani8105 Před rokem

      diesel-async crate has been released, it has support for mysql and postgres.

  • @danieltoth714
    @danieltoth714 Před rokem +4

    Please keep in mind, you can use SQLx and a repository pattern (or something alike) to abstract away your sql. As powerful ORMs are to prototype and for simple CRUD, as big of a pain in the back they can become when you introduce complexity!

  • @palekenyanful
    @palekenyanful Před 2 lety +4

    Amazing content. I especially appreciate the gotchas that you point out in these videos. Each one probably saves dozens of folks hours of time.

    • @codetothemoon
      @codetothemoon  Před 2 lety

      Thanks Zach! I really do hope it saves folks time!

  • @frank144741
    @frank144741 Před 2 lety +2

    By far the best tutorial i found to tNice tutorials in a short ti

  • @complexity5545
    @complexity5545 Před 2 lety +2

    Thanks for summarizing [diesel] and how it was designed. I was about to use use rust to translate sql files. [diesel] is very complex and time consuming. You have to be a seasoned programming veteran to use this. I wrote script for making a cargo project, just in case I need to use it in the future. But man there is no way a regular programmer is going to get this. I am shocked that diesel only supports postgresql, mysql, and sqlite. I was looking for rust database drivers for mission critical million dollar businesses which usually only use oracle , sybase, db2, postgresql , mssql, and (sometimes) terrcotta+snowflake. You saved me about a week worth of research that would have been hopeless aim. I still use diesel + rust just for practice and see if it can handle all these concurrent databases I have. Now I have to go find graph database drivers for rust to get a big picture of where rust is working with. Rust is in a weird place.

    • @codetothemoon
      @codetothemoon  Před 2 lety

      Nice, glad the video was helpful even if that came in the form of determining that Diesel wasn't a good fit for your use case! Let us know what you decide to use instead!

    • @complexity5545
      @complexity5545 Před 2 lety +2

      @@codetothemoon Yeah I'm reading the Diesel source code over the week and see if I can start contributing or at least trying to integrate it with some other databases similar to [old school perl]. I might know some tricks and resources that most people do not know. I really want to use rust for everything.

  • @maxijoker1
    @maxijoker1 Před 2 lety +9

    Great video!!! I'd love it if you did a video on SeaORM. It's an asynchronous ORM built on top of sqlx. It has support for Postgres, MySQL and SQLite, and also migrations. I tried to use it, but I couldn't understand the documentation :D.

    • @codetothemoon
      @codetothemoon  Před 2 lety +2

      Thanks Maximiliano! Re: SeaORM, thanks for putting this on my radar!

    • @maxijoker1
      @maxijoker1 Před 2 lety +1

      @@codetothemoon Thank you! I think you'll like it. It's pretty cool.

  • @re_allyedge
    @re_allyedge Před 2 lety +6

    Truly an awesome channel, keep it up!

  • @massimo79mmm
    @massimo79mmm Před rokem +1

    i found the diesel documentation not so easy, this video instead is gold.
    the normal way diesel works (My program structures -> Db structure) is the opposite that i saw it's used (i already have a db in my company, so diesel have to read the db structures and create structs)

  • @reidlab
    @reidlab Před rokem +1

    this is great for my project of making a custom server for a game! i really needed help learning sql and rust structs

  • @AwakeningofAI
    @AwakeningofAI Před 2 lety +1

    Glad I found this channel. A gold mine.

    • @codetothemoon
      @codetothemoon  Před 2 lety +1

      Thanks for the kind words, happy you found it as well!

  • @anuradhaamarasinghe9649
    @anuradhaamarasinghe9649 Před rokem +1

    Like always, you make great content! Diesel is not easy to understand you made it quite simple. However, I don't think diesel would be a good great if you have a complex db with a lot of tables. I wish it had that db-first approach, where you create the db first and then generate the Rust codes from it. I'm learning a lot from your videos. So keep it up. Thanks.

  • @NeotenicApe
    @NeotenicApe Před 2 lety +2

    Consistently great videos, thank you so much!

  • @NoahSteckley
    @NoahSteckley Před rokem +1

    Yeah I spent an entire day trying to get that damn diesel CLI working on windows with a new install of Postgres, never got it to work. Basic things in Rust are so much harder, I'm starting to doubt it will actually survive

    • @codetothemoon
      @codetothemoon  Před rokem

      Diesel is definately on the harder side of the difficulty spectrum as far as SQL crates go - maybe check out sqlx if you want something simpler (disclaimer - I haven't actually tried it myself but I've heard good things)

  • @jeffg4686
    @jeffg4686 Před 2 lety +2

    Had a thought regarding the language. How come we don't have "trait level instance variables"? I often wondered this in C# and Java as well (at interface definition) - you can have properties, but you have to implement OC - and thus are wrappers (overhead). I think it would make sense for methods in same trait to share data on the instance (it's not that they can't now - using struct variables, but the organization is all one bucket) Any thoughts? I know we're not "instantiating a trait" - but there's no reason the trait level data can't be mixed in via the trait (and those variables getting allocated upon struct instantiation).
    It would be a nice way of knowing immediately what the variable was related to - under the hood, they can "merge" into the struct's definition if needed.
    The variables would need to be "per trait implementation" in case it was implemented generically several times.
    This would further enhance the "interface segregation" concept a bit imo, cause not only can you keep them clean by having small interfaces, but also by keeping data related to that interface (trait) tied directly to it.
    Maybe the top of "impl" block (

    • @codetothemoon
      @codetothemoon  Před 2 lety

      LOL I almost spit out my coffee at the Howard Dean scream reference.
      I've definitely run into a few scenarios where I would have loved to have the ability to put data fields in a trait. Ive been trying to play devils advocate in my head and convince myself why such a feature wouldn't be a good thing, but I haven't been able to come up with anything. It does seem like there is a reason - a quick search revealed this which I haven't actually read yet www.reddit.com/r/rust/comments/ql4gfd/why_cant_you_have_fields_as_traits_in_rust/

  • @wtfdoiputhere
    @wtfdoiputhere Před 2 lety +3

    Can you make a video about calling APIs from Rust and parse the response as JSON? As silly as it sounds but I've seriously been struggling with that, going from the documentation to vscode back and forth

    • @codetothemoon
      @codetothemoon  Před 2 lety +2

      Check out my OpenAI GPT-3 video, that’s basically what I demonstrate there

  • @dastro_90
    @dastro_90 Před 2 měsíci +1

    Awesome thank you 👌👌

  • @tylerhanson133
    @tylerhanson133 Před 2 lety +1

    Thanks so much for making these videos!

  • @chucksneedmoreland
    @chucksneedmoreland Před 6 měsíci +1

    is there support for databases like teradata or microsoft sql server? I know there is a oracle rust lib, but not sure as to its integration with diesel?

  • @frezze9314
    @frezze9314 Před 10 měsíci +1

    MySQL is always mentioned but never shown. Diesel fails for mysql install on Windows 10 /11 with Visual Studio 2021. I tried to follow up with MySQL 8.00 but I can't get the C++ libraries for MySQL 8.00 to work. Moving forward can we do MySQL demos as well? Can you help?

    • @codetothemoon
      @codetothemoon  Před 10 měsíci

      yikes, that's frustrating. have you tried other Rust SQL libraries like sqlx? I haven't personally tried it yet but it seems to be ultra popular right now

  • @renatocunha6974
    @renatocunha6974 Před rokem

    Beautiful this Lang. Im study and study and study this!

  • @benjaminhon86
    @benjaminhon86 Před rokem +1

    Python is such a beautiful language

    • @codetothemoon
      @codetothemoon  Před rokem

      imo it's just on the cusp of being beautiful 😎 many folks don't like using indentation to define scopes, but I personally love it. The thing I can't stomach is the lack of static typing. That's why I think languages like Julia and Lobster are so interesting...

  • @chethancs9633
    @chethancs9633 Před 2 lety +2

    Hey, I'm just starting to get into making soft and tNice tutorials 17 minute video helped a LOT MORE than those one hour long tutorials out

  • @Asgallu
    @Asgallu Před 2 lety +2

    Great video

  • @bjugdbjk
    @bjugdbjk Před 2 lety +1

    Simply amazing, I got the Gist of diesel with Rust, Do you any plans to make a video using Rust crates which are available for cloud technologies, That gonna be huge help.

    • @codetothemoon
      @codetothemoon  Před 2 lety +1

      thanks Raviraj, glad you found it helpful! I do plan to make more videos on using Rust with various cloud providers. I did make one on using Rust with AWS Lambda, but AWS support for Rust is merely "ok" right now - I plan to explore other providers and evaluate them for Rust "friendliness". Any recommendations on which I should check out?

  • @user-hn1cy5ee5l
    @user-hn1cy5ee5l Před 5 měsíci

    What about abstracting from a DB implementation? In python for example, you could choose a driver based on what you put in a config (if you installed all the drivers - postgresql, sqlite etc.) and then you can use any database. Could it be implemented in rust? This is a real case when you have onprem deployment for example and you need to support multiple databases

  • @abdu1998a
    @abdu1998a Před 2 lety +1

    I did not get why the migrations step has to be in SQL. I feel like it could easily do migration with derive macros and rust structs instead of writing raw sql.

    • @codetothemoon
      @codetothemoon  Před 2 lety

      I agree it would be super nice to have the SQL completely generated from the Rust model! My best guess as to why the way things are the way they are is that there are low level decisions at the database level that can't necessarily be inferred from the Rust model. Also I think we'd have to maintained "versioned" copies of the Rust model so Diesel can infer a SQL migration from each version (which I guess would be no worse than maintaining raw SQL migrations). All that said, it does seem like it would be technically feasible to have some kind of "opinionated" mode where Diesel writes the raw SQL for you in a way it sees fit.

  • @user-mr8wy9cq8n
    @user-mr8wy9cq8n Před 7 měsíci

    Hello!
    Thank you for this excellent tutorial. Unfortunately as of diesel version 2.1.0 this guide has been outdated. Is there a possibility you could remake this tutorial, or correct any minor changes in the repo.
    Thank you!

  • @johnnysummers9323
    @johnnysummers9323 Před rokem +1

    Do you know a rust library to implement a GUI?
    Edit: would be nice to expand this project with more functionalities and some GUI.

  • @tobias3581
    @tobias3581 Před 2 lety +1

    Not accurate to the video but this Rust as a framework approach is intriguing. Do you think Rust should be the basis for everything? Or why not?

    • @codetothemoon
      @codetothemoon  Před 2 lety +4

      I don't necessarily think Rust should be used for everything, but I do feel that it is underutilized in 2022 just because many folks aren't familiar with it. For example, I wouldn't feel comfortable starting a mission critical frontend project in Rust today. But for building nearly any backend service, Rust would be one of the top contenders.

    • @tobias3581
      @tobias3581 Před 2 lety +1

      ​@@codetothemoon That makes sense. How is Rust is to work with in a large codebase, and how is it to read?

    • @codetothemoon
      @codetothemoon  Před 2 lety +1

      @@tobias3581 unfortunately I can't speak to working with it in a large codebase yet, haven't had an opportunity to use it on a larger project

    • @tobias3581
      @tobias3581 Před 2 lety

      @@codetothemoon Ah. I've so many questions 😅 You don't have to answer but I'm just sharing what I'm curious to know. How did you learn Rust? Did you manage to learn on the fly? How long did it take to not stumble around? What are the not-obvious challenges that are unique to writing Rust, in general? Do you miss ownership and borrowing when using other languages?

  • @soberhippie
    @soberhippie Před 2 lety +2

    Hi! I am very interested in other SQL crates, especially those that are more object oriented, in terms of not being an abstraction over SQL, rather an abstraction over data objects, along the lines of Django orm in Python. And are there any good ORMs that can autogenerate migrations?

    • @codetothemoon
      @codetothemoon  Před 2 lety

      Cool, I do plan to cover other SQL crates at some point. Lately it seems like the community is steering more toward options that are perceived to be less complex than Diesel. Re: autogenerating migrations, that would be amazing, I'm not currently aware of any ORMs that do that. Seems like something that would be perfectly feasible though.

  • @PaulSebastianM
    @PaulSebastianM Před rokem +1

    Uuu nice clicky keyboard!

    • @codetothemoon
      @codetothemoon  Před rokem

      Thanks! I loved this keyboard but I've since switched to a quieter one because I got some complaints lol

    • @PaulSebastianM
      @PaulSebastianM Před rokem

      @@codetothemoon too bad, the clicky sound was actually a nice clicky, not annoying but like there, working in the background.

  • @chinmaydeshpande5046
    @chinmaydeshpande5046 Před 9 měsíci

    (DOUBT) , just like you did for NewVideo , how can I do the same when I have to make it for custom datatypes not strings ? I mean , how can use 'a there , what should be the correct format over custom datatypes which are defined while creating the schema

  • @allistermi6014
    @allistermi6014 Před rokem +1

    How do you achieve just typing "rustflix" like normal command in the bash with cargo run ? I would like to create command using rust, but every time I have to type "cargo run " with args, how can I get rid of cargo run and just type my command with args?

    • @gareth2021
      @gareth2021 Před rokem

      In case you did not figure it out til now xd : cargo build -release. Then look under /target/release for the binary to run

  • @amartunio3588
    @amartunio3588 Před 2 lety +1

    Next ti, can you install one of those apps that creates a circle around your mouse pointer? Sotis it’s hard for to find where

  • @bitcoinisyourfriend3742
    @bitcoinisyourfriend3742 Před 2 lety +1

    thank you man

  • @LeandroCoutinho
    @LeandroCoutinho Před 2 lety +1

    Great video and to the point!
    Thanks for sharing!
    I don't think having database schema manipulation in the code is a good practice.
    I would have the schema there so diesel knows how it looks, but I would not apply any migration using it.

    • @codetothemoon
      @codetothemoon  Před 2 lety +1

      thanks, glad you found it valuable! Definitely agree that schema migration is best avoided if possible. Much better to get the perfect schema in the very beginning.

  • @juancarloselorriaga6171

    What’s the correct way of doing integration tests with the DB and Postgres? I can’t find any standard test runner

  • @realtebo
    @realtebo Před 2 lety +1

    How can I use diesel when releasing the executable? Must user create db manually? Must the user install diesel and run migration commands !? How to release a software without an auto-creation of db an tables?

    • @codetothemoon
      @codetothemoon  Před 2 lety +1

      When i was making this video, I found this to be one of the pain points I foresaw using Diesel in production. At the time this video was made you'd have to run the diesel CLI for the initial DB creation, but you can actually perform subsequent migrations from your code. See the final chapter of the video, "Thoughts on Productionizing"

  • @SG-tn4wm
    @SG-tn4wm Před 5 měsíci

    Coming from a Django world, i would rather write Rust code and let Diesel create Up and Down migrations for me, is that possible?

  • @GlobalYoung7
    @GlobalYoung7 Před 2 lety +1

    thank you 😊

  • @hemasailarajan6048
    @hemasailarajan6048 Před 2 lety +1

    Any tips? I’m a newbie learning

    • @codetothemoon
      @codetothemoon  Před rokem

      The Rust book is fantastic! Anything in particular that you're stuck on?

  • @dashel.youtube
    @dashel.youtube Před rokem

    Were you using m1 or m2 processor for this presentation?

  • @HoloTheDrunk
    @HoloTheDrunk Před 2 lety +1

    Hello, great video but could you please not zoom in the terminal as much? I get the idea, but it just makes it way harder/more straining to read than just a slightly bigger font (on top of hiding previous command results).

    • @codetothemoon
      @codetothemoon  Před 2 lety

      thanks and that's great feedback! it's definitely a tightrope trying to make things big enough to see but not so big that they cause other things to get pushed out of view...

  • @kpkrishna
    @kpkrishna Před 2 lety +1

    I'm watching from India i find the audio & video to be out of sync. meaning i hear the audio first and after few seconds the video catches up. would be very helpful if both the audio & video played in sync.

    • @codetothemoon
      @codetothemoon  Před rokem

      Hey Krishna - thanks for pointing this out. I haven't heard of anyone else having this issue, but I have seen this issue from time to time on CZcams in general. Do you still have this issue if you play the video right now?

  • @Kralasaurusx
    @Kralasaurusx Před 5 měsíci

    "Right now, the idiomatic way to do things is to install the Diesel CLI on your production image, run diesel setup before your application starts, and then call the embedded migrations run function to run any migrations that haven't yet been performed."
    That seems unwise for several reasons:
    1. If your deployment is scaled to more than one instance/pod, you run the risk of race conditions if multiple instances try to do the migration at the same time. Does Diesel actually protect against this? At small scale without much data, maybe the risk is negligible; but if you're at the scale with more instances and enough data where migrations take more than a few seconds, you're playing with fire.
    2. "My migrator needs database permission, and my app has database permission, so I'll put the migrator in my app, and it'll have permission!" Migrations require *way* more permission than your app normally needs. `ALTER` / `DROP TABLE` are heavy permissions to entrust your app with. It's like running with sudo 24/7: if you're really careful it might be fine, but it's a lot easier to cause catastrophic damage if you're always running as root - just a matter of time.
    3. Bloating production container images with additional dependencies and increased initialization/shutdown complexity: Normally you could get by without a special init process wrapper (e.g. `tini`), but now if you want signals to work correctly realistically you need an init script and/or a separate init container.
    4. Decoupling deployment failure from your CI/CD pipeline: Normally, deployment failure logging would be in one place - e.g. GitHub Actions - but now if a migration fails, the error is in your *app's* logging, drowning in a sea of request logs while your other pods continue handling traffic (assuming your init wrapper script didn't wreck the startup probes so that if the pod fails to start, it doesn't steamroll the new, borked app version over the other previously healthy pods). The event that triggered the migration was fundamentally a *deployment* event but now the error reporting becomes a runtime event, which would normally be HTTP requests, etc. - fundamentally an impedance mismatch.
    5. "But I want the migration to happen in my app so that it's repeatable!" If your deployment pipeline isn't repeatable then you've got bigger problems, and putting the migration in your app is a bandaid.
    6. Startup probe timeouts: Ordinarily you might have a reasonable pod startup timeout of ~5 seconds or something, but now if you have a big enough migration the startup probe fails, so you crank up the threshold for *all* of your pods, even though only one of them should actually even be doing the migration(s).
    Anyways, does this mean that you can't get away with embedding migrations into your app? No, but I wouldn't call it "idiomatic" but rather "good enough at small scale - YMMV."
    I don't mean to be a hater - sorry 😅 Just some thoughts. Aside from that this was a really good intro to Diesel - thanks for the info!

  • @DexieTheSheep
    @DexieTheSheep Před rokem +1

    0:13 Terraria item sell sound?

    • @codetothemoon
      @codetothemoon  Před rokem +1

      I think I got it from a stock sound effects library. It's possible that Terraria used the same sound effect! I spent quite a few hours in that game but didn't notice the similarity lol

    • @DexieTheSheep
      @DexieTheSheep Před rokem

      @@codetothemoon epic

  • @irlshrek
    @irlshrek Před 2 lety +1

    id love a mongodb version!

    • @codetothemoon
      @codetothemoon  Před 2 lety

      That would be fun to make - I’ll put it on the idea list!

    • @irlshrek
      @irlshrek Před 2 lety

      @@codetothemoon awesome!!

  • @thelastninja4825
    @thelastninja4825 Před rokem

    No MSSQL support ?

  • @chrishabgood8900
    @chrishabgood8900 Před rokem

    Something easy in rust

  • @nariharicreations
    @nariharicreations Před 2 lety +1

    Sa . It's hard when you look at the software

  • @DionnyPrensa
    @DionnyPrensa Před rokem +1

    Nice video!.. I'm trying to create a Generic Repository, but I'm dying. T_T

    • @codetothemoon
      @codetothemoon  Před rokem

      thanks! what do you mean by Generic Repository?

  • @scwan-ew8uh
    @scwan-ew8uh Před rokem

    How to handle transactions.

  • @goodwish1543
    @goodwish1543 Před rokem +1

    28 years Oracle DBA still think plain SQL is better. :)

    • @codetothemoon
      @codetothemoon  Před rokem

      hah! Plain old SQL is hard to beat. Talk about standing the test of time!

    • @goodwish1543
      @goodwish1543 Před rokem

      @@codetothemoon You have great EQ. ^_^ Diesel looks similar to Django ORM. You give a good teaching for general developers.
      There are always battle on ORM v.s. SQL. What I learned is layered programming, let each expert exert in their layer. For large scale application systems, I prefer transactional API than coupling code and SQL.

  • @jayanth9601
    @jayanth9601 Před 2 lety +1

    tNice tutorials tutorial was very satisfying

  • @Izapertron123
    @Izapertron123 Před rokem +1

    Why is this so complicated? All I want to do is query data from a database

    • @codetothemoon
      @codetothemoon  Před rokem

      Diesel probably isn't what you're looking for then. Take a look at sqlx! I haven't tried it myself but I've heard great things.

    • @Izapertron123
      @Izapertron123 Před rokem

      @@codetothemoon Thank you!

  • @mazinayman4004
    @mazinayman4004 Před rokem +1

    Elon musk!!?

  • @chessgm931
    @chessgm931 Před 2 lety +1

    sick of it!! !!!! anybody got any tips to help?? close on throwing my laptop out the window! got the Full package on soft feel its a waste of

  • @ajf
    @ajf Před 2 lety +1

    rust

  • @hi_im_julian_kirsch
    @hi_im_julian_kirsch Před rokem

    Whenever I search for "Rust + DB" it always results in Diesel. Could you perhaps lend some love towards "SeaQL/sea-orm"? :D