MUST know Rust database libraries

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
most applications you build especially web servers are going to interact with databases so in this video we're going to talk about the five must-know rust database crates but before we get to that if you haven't already make sure to get your free rust cheat sheet by heading over to let'sgarusty.com forward slash cheat sheet with that said let's start off by talking about database driver crates many applications such as web servers need databases to store data these databases have their own protocols and formats for data exchange that's where database drivers come into play a database driver is a piece of code that performs communication between your application and the database it implements the protocols and data exchange formats for the particular database let's look at some prominent rust database driver libraries one of the most popular databases is postgresql postgres is a straightforward driver for it first you create a client by calling the connect function then you can use methods such as query and execute to run the SQL statements there is also an async version of this crate which uses Tokyo sled is an embedded database written in Rust embedded databases store data directly on disk without needing a secondary application to manage the database so sled takes care of writing and reading from disk with no need to connect to a separate database in addition a slab provides an API similar to bind binary tree map which makes it simple and fast to use for example you can open a file by calling the open function then insert a key value pair using the insert method iterate over key value pairs starting at a given key using the range method and remove a key value pair using the remove method SQL X is a SQL toolkit made in Rust that provides drivers for many SQL databases like SQL Lite MySQL and postgres it includes useful features such as compile time check queries embedded migrations async support and more SQL X also has a built-in connection pool more on that later connecting works as expected by calling the connect function and we can execute queries using the query function which also allows us to bind variables database drivers are very practical but it's tedious to write database queries by hand that's where object relational mappings come into play orms Not only Implement protocols but they integrate with your rust code this means that a database table can have an equivalent struct in your code for example that way every time you make a query on that table we automatically get typed structs as a result orms also include query builders that abstract interactions with the database as rust functions so instead of writing raw SQL statements and passing them to functions like execute we have actual functions representing methods like insert select and update let's look at some Rust orm libraries to understand their power diesel is a productivity focused orm this means it has a performant yes simple API so you don't have to waste time writing lengthy SQL queries let's look at some code we can use macros to Define structs which map to database tables then we can instantiate them like normal structs and insert them into the database one downside of diesel is that it doesn't support async one orm that does support async however is corm corm is an orm with similar goals to diesel but with a different architecture corm rely eyes on SQL X which means it uses database drivers written in Rust and unlike diesel it supports asynchronicity it's also built on top of cquery a query Builder which you can use separately if you don't want a full orm after creating database migrations we can generate entities using the cormcli each entity will become a module we can import and perform the usual crud operations in this example we create a user named Joe and insert the user into the database when interacting with databases we need to open a connection usually we have to open a new connection every time we want to access the database but that's inefficient we can improve this by keeping the connections open for that we use a connection pool the connection pool will manage database connections keeping them ready for use by different threads when a thread stops using a connection instead of dropping it it will go back to the connection pool ready to be used Again by the same thread or another thread rust has a notable connection pool Library called R2D2 r2d G2 is very straightforward to use say we want R2D2 to maintain connections with a postgres database all we have to do is add the R2D2 adapter for postgres create a new postgres Connection Manager instance and use it to create a connection pool it's not much different than using the postgres crate and R2D2 takes care of the connection pool under the hood now that we know what database drivers orms and connection pools are the question remains when should you use them although orms provide a much more ergonomic way to interact with your database they tend to be less flexible than drivers for extremely complex queries using SQL directly tends to be better than orms as you have the full power of SQL at your disposal orms also tend to be slightly slower as the query needs to be processed and rust before being sent to the database this slowness is largely insignificant and the safety you get by using an orm tends to make up for it connection pools are always useful but if there isn't one ready for the database you're using it might be better to just go with that one as implementing connection pools isn't easy even though they are helpful oftentimes connection pools are not necessary especially when dealing with a low number of connections that's it for this video if you haven't already make sure to get your free rust cheat sheet by heading over to let's get rusty.com forward slash cheat sheet also if you found this video helpful hit the like button and subscribe to the channel for weekly rust content with that said I'll catch you guys in the next one
Info
Channel: Let's Get Rusty
Views: 40,098
Rating: undefined out of 5
Keywords: rust tutorial, systems programming, rust lang, rust programming, rust programming language, rust programming tutorial, learn rust, rust language, rust programming language tutorial, rust programming language course, rust programming full tutorial, learn rust programming, learn rust programming language, rust, programming tutorial, database, mysql, postgres, postgresql
Id: FW4oUXHly8c
Channel Id: undefined
Length: 5min 49sec (349 seconds)
Published: Tue Nov 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.