ORMs no more, I do this instead (example in Rust, but applies to others too)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm christopher today we will look at some database access i think that orms is not really useful i think they are complicating things more than it should i think it's better to stack together some tools and then have a better more flexible developer experience than you get when you are using a orm so what do i mean by orm i'm talking about active record or entity framework or sqlize or yeah those are the ones that come to mind and i i'm not talking about the ones that only map the data to objects i think that's fine because we want to work with typed objects but we do not want to have a different language to access the data than sql we don't need a dsl that converts from uh language that was made for this for the data retrieval domain turned into a general programming domain like the languages that you use to write your application i think that is limiting you and you are probably also paying some performance penalty if your language is not a zero cost abstraction or i mean even if it is you are probably paying for it so i can't just go out here and claim this without backing it up with something so i will demonstrate how i have things set up on my machine the examples that i will be showing will be in rust but i think that your language even if it is not rust probably have something that is simpler than the orm that you are thinking about switching away from i got some feedback in my last video about my fonts being too small so i have bumped it up even more and hopefully this will be enough i also made the mouse cursor bigger and i put it in a nice color so let's move to the computer okay here we are this is the table that we will be working with it is results from tournaments it has an id and a name of the person who put the points in the system and the points and the tournament which is a text it's just an id of the tournament and then i have populated with some data here and we can look at it this is the first tool that i'm showing you this is a postgres connection from vs code and here is the data from my database okay good now that you have seen that let's move over to the application this code is not really important for for what we are doing it's just setup code so i'm just going to leave it here it's the same for all the examples okay let's move into the first one first thing we can see in here is that i have syntax highlighting of the inline queries and this is a i call it quick and dirty because this is just me fetching some data and without mapping it to any type and in rust or in sql x which i am using i will get the types from it so it's this alias here which is actually a name which i have aliased to alias is a string and then id and sql x does not only map to objects it also tells me if i'm trying to get something that doesn't exist that says age here it will tell me that the column age does not exist so that's cool and if i get the points i can get the points down here and it will be a integer 32 because that is the type so i have all the the benefits i think from like if i just want to access some data really fast but the problem with this is that i cannot send these records away because the type is trapped so no one can take them as an argument but i can put in my own type and map to that and same goes here if this one does not exist on the type i'm trying to map to it won't compile anymore so it says it doesn't exist so that's how you fetch things and we can prove that it works and here are the results great let's move back now you might think well i can do all that in my orm2 and you're correct because that was just a simple selection it doesn't didn't do anything special but now i will instead do a window function and it will get the rank of the person inside of the terminal or tournament so if i if i query this i can select it just to run it against my database you can see i get the placement here is the one two three these are the replacements in that 2022 tournament and these are the people and you can see the points for them this is uh one thing that might be hard for you to do in orm because you will have to figure out how to do this i mean you might know how how to do it in sql but then you have to reverse engineer that or whatever we should call it so you can translate it into your orm dsl so that then can turn it back into sql i think you're seeing where i'm going with this it's it's really interesting how we have complicated things so let's move this out and move back and i will show you even more advanced but simple to do in sql oh no first i'm going to show you that if you don't like to have sql inside of your code like this sql x also has support for files you can put a demo i can put the same sql query inside of a sql file and it will just add compile time get it in and compile it just like the other code i just showed you now move on to the more advanced thing that is pretty simple to do in sql and that is the common table expression so here i have a common table expression that i call total points which is the total points of each tournament and then i use that to get the percentage of points that the participant is getting i have a big problem with that word but let's move on and if i run this sql i will get something like looked like this and it is yeah here's the percentage of the points in the tournament like the total point you might not really need this percentage thing but i'm just showing that it's easy to do in sql but it might be harder for you to do in your orm and yeah and if we look at this one here and i get the point percentage you can see it's a float 64 here so it is typed all the way yeah i can run it to prove and here we have it so it's in here all of it that's good now you might be thinking why would i ever want to have all that sql inside of my functions i'm not proposing that you're putting sql all over your application i think it was a good idea to trap it inside of a data access player or class or a module or whatever it's called so maybe you will do something like this where you make a service for it where you so you can fetch stuff and then you take that service and you put it in your regular service locator that you have somewhere and then you use the service to get the data so in your application you don't put the data access logic you put that inside of your data access classes or inside your data access code okay and then in your business logic you use the the function that you have created inside of your data access service and then i think your application is protected from all the sql running around i think it might even leak less than your orm because the orm is so easy to just misuse in a place where you should not do data access you might just do it because it's there but if you have trapped it inside of these services it might enforce you to group it better that's it that's a code i had to show for you and let me know in the comments if i convinced you or not or maybe you were already on the boat and and want to say i agree or you want to tell me that i'm wrong and if you like the video don't don't forget to press the like button because that helps me and yeah i hope that you like this video thank you and font size i hope the font size was awesome i will test it on my phone before i upload and yeah have a good day bye bye
Info
Channel: LindblomDEV
Views: 27,509
Rating: undefined out of 5
Keywords: entity framework, orm, sql, data access, active record, sequelize, postgres, sqlx, rust sql, object relational mapper, Hibernate, SQLAlchemy, Entity Framework Core, Doctrine 2, dapper, MyBatis, GORM, NHibernate, ent, Objection.js, peewee, Ebean, DBFlow, Bookshelf.js, Propel ORM, GreenDAO, Jugglingdb, NotORM, ObjectiveSQL, GINO, Waterline, OrmLite, diesel rust, diesel, diesel orm, NPoco, better sql, java orm, sequel, node sql, python sql, mysql rust
Id: JUMDLPa3xSo
Channel Id: undefined
Length: 10min 43sec (643 seconds)
Published: Thu Aug 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.