I Would Never Use an ORM, by Matteo Collina

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello hi folks I am going to talk about uh open data oriented mapping very fun very fun topic okay I love this topic by the way so it's uh something well wait a second yeah I need to click this button oh okay so a little bit about me and it was more or less was said everything before uh if you want to follow up my newsletter you can go to nodland.dav And subscribe to my newsletter so hey um you can check it out cool um so uh object oriented oriented mapping every year where are we going well what do urm do uh well where I'm from are the promised land okay it's uh it gives us a very neat well-organized code base um with the concept of models uh do you like models I well I have something to say about that and typically what a model does represent is an object typically a class that represents a row in our database as well that all the relations of that row we told the others rows in the database what does this mean well um you know well before going to that uh what happens when your application to your application when it uses another Ram it hibernates anyway going back to orms um what is the problem with models okay and the problem we you got is that let's consider an object class quote that extends a model and this is uh it's called a fat object essentially it has a lot of information um it also it it it does different things the different responsibility one is holding the data in memory another one is manage the persistency including all the relationship with it and Implement most of the time people put even the business logic in there to operate on the object itself uh well yeah it's there's a lot of things okay you put a lot of effort into crafting this object well yes this is the problem right um the problem is that you know this is bleeds to a very unscalable architecture and a lot of people that have been developing following following this architecture in in the node.js world but also other languages will know for certain that um you know the mode The View controller architecture is actually unscalable in terms of complexity basically you could imagine it very quickly that if you only have three categories of things actually two where you can put code you sooner rather you know you you should sooner than other thousand of them because you only have two boxes you know it's either a view a controller or a model or a view it's I don't have a two or three I can cut I don't have many categories and then you can see that there are companies out there with 2000 models what two thousand models is this something you know it's it's a um how do you understand if you have two thousand models within the same code base how can you understand the relationship between all of them and you know how can you develop there anyway well you see it's um uh my take is that tour Rams are only delivers a nice bowl of spaghetti code since the 90s okay don't use them they only lead to spaghettico that becomes very quickly and maintainable and by the way I love carbonara so don't touch it so since 2016 I started developing this this web framework called fastify check it out it's great okay I and as part of that I am I'm been pushing a slightly different take on the architecture and how you can help scale your code complexity and your features and your teams as well so first of all you want to the you know split your features in terms of modules and so you essentially you want to group your features by you you want to group your code by uh using a feature based approach so you're not putting your code into oh this is a model it goes into the models folder okay but oh this is part of the card so we we have a folder a module called cart and all the things of the cards stays there both controllers and all the code related to that okay it's great and because you if you follow this structure you could actually migrate very quickly to uh a microservice word and where you have multiple microservices that can talk to each other that are exposed and so on but all those micro Services uh you know can you know you can just extract them from the previous structure um and in note that in that way you can also control what are the relations between for example a cart and a product and so on and so forth in a much more controlled way so but how does this all goes to our amps well let's talk about Pareto Pareto was a phenomenal mathematician but also one the one that gave us the Pareto Principle okay so he came from Italy of course okay why you should not talk about Korea um so the Pareto Principle tells us that 80 of the outcomes result from 20 percent of four causes for any given event so basically you know this means that a uh given a certain amount of causes you get the outcomes and and so on but that does how does it relate to software development at all well let's consider it this way let's say that we have the features which are your causes and then the effort to implement them the effect and you have that you most of the time most features can be implemented very very quickly in let's say 80 of the features can really be implemented in 20 of the time and vice versa the last 20 can be implemented it takes all the time everybody doing development software development should have experienced this there are certain things that takes no time no Breeze bang bang I can code like like I can develop stuff super quickly but then things get uh you know becomes impossible to to develop further oh wow what how does what can we do about this okay so okay so when you're picking a new technology what are you optimizing for are you optimizing for the um 80 of the features that takes 20 of the time or for the 20 of the features that take 80 of the time like how can you what would you optimize if you need to choose a technologies that changes these uh equation you might in order to reduce time you might want to be be willing to trade some I would be very willing to trade some more effort into the start of the project to be able to reduce the time that it takes uh for more complex features so absolutely like that seems a no-brainer um so so most people though when they need to make this Choice the reality is they the we developers are very lazy and we choose a framework or a library that removes all the repetitive tasks and instead that a framework that enables maximum flexibility this is since this plenty of time okay and that became something that makes the complex features a very harder it's always very often like the people making a decision oh I run the tutorial the tutorial is super easy very happy and let's go with technology a oh okay yeah we'll see you later I'm here if you want to here is my business card I'm joking so uh or Rams are one of those uh Technologies okay so we typically when you use our orm you write your migration or Define your schema you apply those migration then you set up your ROMs you've write your models then you suppose those models as routes or resolvers in graphql whatever you are using and then after a bit though because you know this is never enough you need to write some custom SQL and code to deal with some complex business logic and this is where all the time is spent in reality okay um so but you know for me the best line of code is the one that I don't have to type so if these are still a lot of work um and when I'm developing a new back end I I want something new okay I want uh so what I want is I want a back end that can be super extensible and can help me develop new things very quickly without without repeating all the Legacy stuff all the uh I'm lazy so I don't want to do all the you know the the easy work I just want it all done by me automatically so why should I spend time doing it uh I want something that can run locally I don't want a back-end database for our back-end uh a backend as a service that only runs on the cloud and I need to be connected to the to to the cloud to develop it because otherwise working in a team becomes impossible uh also I want you know some level of authorization out of the box I want graphql I want rest I want to support multiple database because why not um also I want it to be built and I don't want to be to build myself ouch um so in in practice I want to have a cake and you know eat it too at the same time and you know I I love cakes I love chocolate so you can see the chocolate here there's a lot of chocolate so you know it is that so uh this is the catch okay I want to be able to have a framework and a tool that can enable me to remove all the repetitive tasks but at the same time gives me the maximum flexibility does it exist uh well yeah I kind of kind of exist I I have been working on the topic for quite a while check out at oss.patromatic.dev so essentially this is where uh how the development flow using platformatic would look like you write the migration Define the schema apply the migration configure performatic DB that's it and after a while you can write some custom SQL and code to deal with uncomfortable business logic and the reality is that this type of approach shrinks quite a lot the development time uh in fact it's a platformatic is nothing more than you know uh is nothing more than fastified and it's built on double fastified so you could actually just you don't need to deploy custom function web books and deal with all that stuff you can just put code inside your node process and it's just work um so it's you don't need oh and now you need to deal with webos in order to do all these things oh it's a Web book ouch I don't I will this is not going to be fast or it's not going to work well I want something just runs on my on the same on the same process very quickly um so and look it's this is also so good that you can actually switch its um Rich relations and I just use our modules completely skipping our server so um let's go and do a little bit of of a demo time so hopefully I will fit it in time so hey let's create a folder okay make your demo oh yeah the demo exist okay I've already created a folder but it's empty yay so now what we can do is a platformatic DB in it and these will create a bunch of stuff for us and we just need a little bit of help uh from npm to install some dependencies so that we can run our things in the meanwhile so once we are divided while npm is doing its job let's look at what is in this folder so we have a platformatic db.json file um this is where we configure platformatic DB we tell him that we have a migrations folder a table uh we will be ignore versions The this is for for the migrations to work then we have a plug-in paths and we want to also generation types in the plugins you can see that it's just a normal command s file but esm is also a support field and Global contains our entities and our stuff and we have some migration that's already prepared for us which creates a table movie and uh uh and we tear it down okay so we have created something for us for free so well let's look at what these zoos look like so what we can do is we can do PLT DB migrate and you see that it has created things and it has also generated the type and then we can uh PLT DB start and these is going to start our database now what we can do afterwards is sorry to interrupt you on this uh we're going to need a bigger font oh you're going to need a bigger font yeah bigger bigger bigger bigger bigger bigger bigger bigger bigger bigger bigger yeah I mean thank you okay uh I thought it was big enough okay so uh uh gold so now what we can do is going to local last oh not Loom look at last three three forty two uh it listened by this support okay here is what this is the okay now what we can see we can go to graphical you know graphical right um and grab with graphical what you can say let's let's look at what's what the types are available graphical is our interface for graphql it's not ours is part of it's developed by the graphical foundation so you can see that we have movies and we have for example count movies so to count the movies and what you can we can do for example is doing a mutation safe movie and we want to uh save a movie with title Harry Potter if you like Harry Potter I really like Harry Potter and we want the ID and the title and we can save this okay now it's a movie save Harry Potter okay now what we can do is we can do a query and says um or movies where we want all the movies and we can get the ID and title and then I need all movies okay I'm running all movies you can see that we have a reporter here note that this is actually oh sorry this is actually very easy because you can also we also have the open API docs and you can actually run also with rest if you don't like graphql so let's say that we are doing the uh UK let's say that we are running our uh um uh our last request and we can see that we get the ID title and the actual object that we want pretty cool right okay so uh this is uh we have created a little bit of a table and with with no code uh whatsoever um something that so something that you can do with this consider it is that we can do for example uh uh do uh let's say we want to add another route so let's call it hello okay and we want to return hello world okay and look when look at this you see that the our demon our process automatically started so now we could oh let's let's do it in here now what we can do is we can curl localhost 3042 and oh and do hello and you see that we return our word and if we change this to uh hello jetbrains uh it will uh return a lot of brains okay like the library load is essentially instantaneous cool okay so we have uh in here also you can see that let's say that we have all the movies uh and then you can do uh ah platformatic entities movies find and console as I say that we want to console log this and you can see oh um yeah yeah we you can see that now it has reloaded it correctly and it has logged out our movie note that these movies it has uh it's fully typed so you have a fully typed experience here on the movie side over the movie front okay so um uh where we are uh okay so what we can do now is we can add another tiny migration okay and uh here we go zero zero two dot do dot SQL and let's say that we want to okay I'm going I'm going to cheat and let's see if the cheat Works uh create our quotes SQL table and let's see if our uh copilot is not copilot is not helping okay I'm sorry I'm going to do it by hand so I'm going to make a mess out of it okay so uh what we do is oh maybe I can yeah okay so um maybe uh what we can oh I found a bug here we go so uh what we can do in here is uh quotes Okay and what's and we want an integer primary key and we want the quote it's just text and we want a movie ID and here we go I'm going to cheat badly I never remember the stuff and and yeah so basically we have their movie ID and then we have our integer which is uh not normal because we want to want all the time and we want to be a refer reference uh movies ID okay now what we can do no dash V is 16 so okay and then PLT migrate um yeah so we are running sorry why oh yeah PLT DB migrate okay here you go oh as usual I knew it so um typos typos are bad okay this is I'm missing an s so PLT DB migrate okay now you can see that in types you got a quote element two and now um cool okay so what did happen to in our in our graphical you just can just refresh and if you refresh you will see that we got quotes now okay so you see that we got all the movies now it's time to to Stave a quote save a reporter quote and let's do that and Save a quote and we want to to include um Ed so a movie ID a movie ID which is one and the port itself I saw I will make a typo here so let me where I am up to no good okay and now what we want in the quote is we want the quote ID cool let's run that migration that that mutation okay we have saved it now in all movies we can even ask for the quotes and we want the ID and the quote now if we go all movies and we can get all the quotes and all our graph so automatically created for us we can also use our golden open API and you will see that in open in open API we also got it the quotes movie ID quotes and if you put one here if you try try it out okay let's put one here and then if we run it you will see that you get the the quote correctly okay from from the movie cool okay um so uh what now what else and I hope I have a little bit a couple more minutes so um for this uh so what else well we have one more one more feature to show this was a recently recent Edition so let's see let's go into uh let's open another instance of um of graphical and what we can do is we can even do a subscription but so let's say that we want uh we want to know whenever a movie is is saved okay and we want the title and we want the title and then we can run this okay oh is trusted true it crashed Happy Days and here we go Jesus started okay now uh then what we get is let's say that we want to store another movie and the title of another movie is a Star Wars and you run Star Wars and then you go into the second Tab and you get your subscription automatically updated it's pretty cool right um so uh yeah it's uh it's it's very nice um something that you could even do is uh you can even go into their plugins you can see that you have your movies here and you see that it has the title and and so on so here we go um you can even add an add a new resolver so app Define uh sorry extend schema EMA okay and then yeah whatever I don't have much time left so I will leave it to uh for people to try it out um cool uh let's go back to the talk um so um yeah so they as I said all of these is is open source you can download it you can try it out it's uh it's all available consider that as I said the platformatic allows you to write a a is composed of fastified plugins so you can actually put your routes and resolvers inside inside the inside the main the main node.js process as well as you can use all of this in your app so if you want just use our rear module you can actually do just do that um so with all of that uh yeah the question that remains an answer is that did I code an orm to do this and with that I will say that all of this is available check it out at platformatica Dev the all the docs is oss.platformatica Dev we have so we have a nice Discord community so if you have any questions you come back and it's a um you can find me at nodeland.dav and I am matakolina thank you [Music]
Info
Channel: JetBrains
Views: 69,551
Rating: undefined out of 5
Keywords: JetBrains, software development, developer tools, programming, developer
Id: qfRQ5zhYuJE
Channel Id: undefined
Length: 25min 26sec (1526 seconds)
Published: Tue Nov 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.