Prisma 2 Impressions with NestJS | Next-gen Node.JS ORM?!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going today we're going to take a look at prisma which is marketed as the next generation node.js orm it claims to help developers to build faster and make fewer mistakes when working with databases today i kind of want to take a look at what prisma is and i have not actually used fresno before so this is gonna be a total first impressions we're gonna test how easy is it to get started is the documentation good and we're gonna try to build an api with it a simple rest api and we're going to take a look at you know how does it compare if we were to build that same api and other orms what exactly is it about prisma that makes it next generation so that's what i want to take a look at and explore all right so with that said let's go ahead and get started here let's take a look at some of the features that prisma has at a high level it claims that it has a data model that you can easily read they have this sort of schema syntax it's very similar to something like you know graphql if you've ever used that before they claim to have a type save database client which is definitely you know something you want to have when you're writing stuff in typescript they also have their own way of doing migrations which we'll hopefully take a look at in this video and then they even have their own visual database browser so instead of you using something like the beaver or db visualizer or something similar you'll just use their own ui which again i want to take a look at how this performs you'll also notice on their homepage that they claim that they're very much something that plays well with any framework if you're subscribed to my channel you know that i'm a huge fan of nestor yes so we're going to try and see does it play well with necessary yes we'll try to make an api with that by the way it's also worth mentioning that they have this prisma examples repo that has examples for pretty much almost any of the big frameworks that you can think of so if you're trying to use it with happy express naturally yes graphql there's so many examples here that you know if you didn't like that i used necessary just for this video maybe you want to try it with your own framework of choice there's so many examples in here for you to take a look at all right finally let's jump into the code i've got a terminal here i'm going to use the nest cli to create a new application and i'm just going to call that try prisma now this video is obviously not about teaching sjs i have a lot of content on sjs in my channel if you want to learn more on how to get started i have a crash course i even have one with other orms like typewrm if you're interested but in this video we're just going to take a look at how well does it actually integrate with your framework of choice which mine happens to be nested yes all right so i now have my code open here in vs code and first thing i'm going to do is create all right now i have my nexus code open in vs code so first let's talk about what are we going to try and build so i was thinking that we can try to build a simple crud api with a little bit of relationships so perhaps we could do a library api where we have a catalog of books and books have authors so you can probably kind of imagine the schema for that where you have books and authors and authors can have one-to-many books and if you have time we'll add a little bit more to that but the api itself ideally is that we can do a crud of books and authors so let's go ahead and again use the next cli to generate some stuff for us so we can get up and running quickly i'm going to do nest generate resource and ask me what would i like my resource to be called let's start with books and i'm going to use rest api here as a transport layer and auto generate the correct endpoints so it's going to do that install a couple packages and then we'll be good to go so let's take a look at what we generated we got a books module and folder here and it has a controller if we open up that books controller you'll notice that it just has your typical crud endpoints so we've got our create read update delete and then it has this service that you'll notice has those methods built into it but they're not actually implemented yet they're just returning strings so our goal here is to get this service to actually talk to a database using prismo right so we need to be able to create books find books update books delete books and so on so back in the prisma documentation they do have some docs here on how to get started whether you're starting from scratch or adding to an existing project and taking a look at the starting from scratch it'll ask you do you want to do relational or mongodb so that's actually interesting i didn't know that it would work with mongodb as well but we'll take a look at that maybe another time so let's go to relational databases and it goes through some of the things that you need to get started with and taking a look at this their documentation is actually pretty cool because you can switch between typescript for javascript and whatever database you're using so it'll actually guide you based on your own selection so that's pretty cool it's sort of like a choose your own adventure what was a little bit odd to me like taking a look at this was they didn't have sequel light here which is something i like to use in in tutorials because it's so easy to set up so i was kind of hoping to see that and then you'll notice that when they do a project setup it has you install all of these other things like typescript and stuff which you may or may not already have for example with nested it already comes with a lot of these typescript dependencies but luckily that's a yes and one big reason why i like scs is they have fantastic documentation in fact they have documentation on prisma and even better it's on sql lite so we're gonna take a look at following this guide and see what we can come up with so it says that first steps is you create your nexus project which we already did and the next step is to set up prisma so with the documentation and the code side by side here we're going to go ahead and run this npm install prisma save dab and it actually says here that we're going to use the prismacli and that we're going to start with the npx prisma init command so we're going to have so we're going to go ahead and do exactly that npx prisma.net and let's take a look at what this generated for us it says that my schema was generated in a prisma folder we'll take a look at that in a second and next steps is to set a database url set the provider for that database run prismadb poll to turn your database schema into a prisma schema and then generate the client so let's try to work through some of those steps first let's look at the schema and actually you'll notice that i don't have any syntax highlighting here and vs code is recommending to search the marketplace let's go ahead and do that so there is a prisma syntax highlighting extension here i'm gonna go ahead install that and voila we've got syntax highlighting for our schema all right so according to the documentation here actually also created a dot env file for us which is you know likely well you'll be putting database credentials which makes a lot of sense so you'll notice there's actually a database url in here and i notice that that's referenced in the schema so that's one thing to learn right away is that you can reference environment variables i guess using this env function uh it also tells me that postgres is the default as we can see in the database url as well as the provider here and we want to use sqlite like we mentioned so we'll actually just follow this documentation so it says that i need to change the provider to sqlite and update my database url to a file like this i'll just go ahead and copy and paste that now what i'm curious about is if i'm supposed to be installing sqlite myself which usually you should but i'm not sure if prisma maybe automatically will install it for me later so we'll see if we run into any errors but let's go ahead and just follow through this documentation to create our first schema and run some migrations so our first step is to create some kind of schema or at least there's already a schema here but we need to add to it and remember we talked about creating a bookstore or library api so we're going to try and create a book model they have some examples here with a post end user which will be pretty similar to what we're trying to do where we have books and authors but i want to take it one step at a time by first starting with the model for a book and let's just see if we can create the crud for that so i'm going to go ahead and actually copy this model for a post and manipulate it to work for a book so we'll remove this otter field for now because we i want to see what the migration is like later on when i'm actually gonna go ahead and add that and we're just going to keep it simple and we're just going to have perhaps an id and a title and we'll have an auto incrementing id that's fine to get started with definitely want to explore how to do uuids later but i think this is fine as a start all right it says that once we have the models in place or at least i have one run a migration command using the prisma cli so we're gonna do that all right so in the terminal i'm going to do npx prisma my grade dev dash dash name and i'm going to call it ad books and what does that do so it says my sqlite database was created and it made some migration files so we're going to take a look at that so it looks like a generated sql for us it says your database is now in sync with your schema so that means it not only generated the sql but i think it also ran that and apparently it created a prismaclient for us which i assume is what we're going to use to query the database so let's kind of explore some of that one at a time it says that there's a new migration folder here and there's this file let's take a look at the generated sql it did a great table post with the id and title which is wrong actually i have i said that i wanted books um so what do i do here do i just rerun the migration and it'll fix it for me that's what we're gonna find out so i'm gonna do npx prism migrate div ad book again so it actually just creates a new migration and it dropped my table of post and created my books table so that's pretty cool so that's actually a really good quick test of migrations working right you're you're ideally able to take your your database from you know nothing to incrementally improve it to your ideal schema so that's actually a really cool thing to notice is that as you manipulated the schema file and run the migrations it's automatically gonna you know add and remove columns and tables and i'm curious to see how that's going to look as well when we add relationships to authors in a little bit but again let's start simple here alright so now we have a basic books table and we want to do crud operations off of that and the documentation at this point tells me that i need to install and create a prismaclient which i think is what we're going to use to query so npm install at prisma client that's good to go so according to the documentation here it says that as it as the client installs it automatically runs prisma generate which what that does is that it reads your prisma schema and updates the generated client inside node modules so it does also mention that in the future as you change your schema you'll need to run that generate every time so i'll be curious to see how painful that would be as you're you're developing how often do you need to remember to rerun generate so that's a good quick note to know about all right so at this point with the client installing generated it sounds like the next thing we need to do is create a service from nasa js's perspective we need to use the service to sort of hook on to that client and this is what we're going to use to inject it into our services and controllers so i'm going to copy this code and it tells me that i should create that in src directory in a file name called prisma.service.ts so i'll do exactly that in src we're going to do new file prisma.service.ts and i'm going to paste in that code oddly enough this code has an import in here that's not used i'm not sure what's that about and according to the documentation uh we don't really need to care too much about the code in here i don't think it's gonna change that often it does seem like you know it has a little bit of configurability like what do you do on module init which in this case you know it connects to our database i assume so where we go from here is we want to inject this prisma service into our book service and actually start creating queries so as you might know in nature yes a lot of it is driven by dependency injection so we're going to add a constructor here and we're going to do private prisma prisma service and we'll make sure to import prisma service from [Music] from that file that we just created all right from here i think we just need to learn how to do your basic crud queries and just so you know what i'm taking a look at uh there is this documentation in prisma for specifically for crud right how do you do create read update delete you know we might probably start with read how do we read data and then in addition to that the examples in sjs also show a lot of these crowd examples so those are the two references that i'll be using in this video right so why don't we go ahead and just kind of use that reference and see what we can come up with here so let's maybe just start with you know a basic select doll how do we do that so i assume this is prisma dot um and it looks like from here i need to select the the model i guess so in this case that's books and then there is according to the documentation a bunch of find methods so we got find many find unique we'll use find many and find unique i believe is what we'll use for find one here in just a second which actually why don't we just do that right now let's do this that prisma books dot find unique and what i actually need to pass in here is sort of like a where clause where i'm going to do where id is id all right so that's pretty simple and as you can see it did sort of generate the client off of our schema so it's aware of books because it took a look at our schema and generated a client off of that so let's actually run our application for the first time and see can we query the books table which really it's empty right so we're not going to get anything but let's just at least run it and see can we get an empty array so i'm running npm start dev here so immediately i'm running into a problem here where it actually can't find a prisma service which i kind of expected this to pop up which is why i was curious why it wasn't mentioned in the documentation that uh it needs to be a an import in one of the modules all right so let's try to fix that problem by adding our prisma service to perhaps the books module i believe it really should be an additional provider here prisma service and taking a look at the logs we can see that we've got green here it's no longer erroring so we should be good to go all right so let's test our api i should be able to go to thunderclient over here which is a lot like postman if you've never used it before and i should be able to make a call to localhost 3000 and you'll notice that we get our hello world that's what our base api is doing but we also added a books crud api so we should be able to do a get on slash books which is going to return us our empty array so i think that means it's querying our our database right it's not running into any errors so let's actually go in and try to create a new book but first we're going to need to implement the create method in book service right we have a create dto coming in here all right so let's implement this first of all we have some kind of input and you'll notice in sds typically there is a a dtl class right that has a that represents that request body so in typical net js you would define the properties in here using you know you'd add id number title string so that you have a representation of that shape initially yes but something that i actually just discovered as i'm reading the documentation here is that you don't have to do that manually anymore it looks like part of the things part of the type definition that prisma generates for you is those dto shapes so i can actually replace replace this with prisma the um the name of your model which in my case is books and you'll notice there's a bunch of like different things in here and there's one called books great input if we drill down into this type you'll notice that there is this type definition for a books grade input which is just a title string which is exactly what we want for a create dto which is pretty cool so that means you don't have to define dtos anymore as far as i can tell now i'm curious though how you do how you're supposed to do validation which in sjs dtos usually you'd use class validator but we'll explore that in a little bit so if we have this dto uh according to the documentation to create that i again just do prisma.books and then there is a create method i just pass in the uh the dto like this and i believe that's all we need to do and you'll notice that the the data adder already has that type so even if i you know remove that dto it's going to tell me that i have the wrong shape in here because it's expecting a books create input so it's expecting this type specifically so that's pretty nice now if you're curious and you know maybe you're using prisma for a javascript app not typescript you you can actually just provide you know the dto as an object like this so if you're doing this in express that's probably how you do it also looking at this create documentation it does link to generated types so it has this documentation for advanced type safety so i think that's a lot of the the thing we just discovered here is these generated types for us which is very cool i'm gonna have to read into this a little bit more later but basically my assumption is that there's always a type for the various operations that you might do like selecting uh updating and stuff like that so we'll hopefully learn about that a little bit more later looks like there is also a create multiple create many api so that'll be great for a bulk create if you have that in your api but anyways so we've got that we've got the create created now so let's go ahead and go back to our thunder client and create a post request which i assume should be able to take in a json that has a title uh what do we want to call this harry potter and the sorcerer's stone so let's hit send on this post slash books with a title harry potter i'll just call it harry potter one hit send and looks like we got a 201 created and a book it has that title and id1 let's do another one let's do harry potter two hit send harry potter three hit send so now i expect that we should have three harry potter books so if we go back to our get and hit send here now we've got our three books pretty simple right and then remember that we added find one so let's go ahead and try that you should be able to do book slash one and it gets me harry potter search one book slash three that gets me harry potter three so that works great looking at the documentation i actually can also replace this type here if i wanted to to be prisma books unique where unique input which is actually equivalent to the shape of this object so let's rename this id to book where book we're unique input which is kind of a mouthful but copy and paste that and we should be able to replace this entire object and from my understanding that's equivalent very cool all right so if we drill down into that type it is just that object with an id number so that is very very cool again kind of just simplifies the overall syntax um i guess it depends how much abstraction you want there i thought that where the original thing we had where we had where id is still very simple and intuitive to me but i guess it depends on your personal preference all right let's see if we can implement update so according to the prisma documentation to do an update you sort of do dot update and then there is a where clause and then the data so that's a very simple easy to understand api what i wonder about is do i also have generated types for these things i'm assuming there is i'm just doing a quick look through as well you can do update multiple so bulk update i guess it's also possible that's nice and looks like they also have upset which is sort of a fusion between update and insert so like if it if it notices that it's not already there it'll auto insert it all right so to implement this again from sjs remember you usually have to update create your own dtos so you'd have you know a create detail yet have an update tto but we just learned that it seems like prisma creates types for those automatically which is super nice so i assume that i should be able to replace this update dto with something similar before we update that we know that what we need to eventually call is this says prisma.up.books.update and from the syntax it said that we need to pass in an object that has data and a where clause right and we just learned that this type represents that where object that where clause so so we should be able to reuse this type so i can maybe do an update here we'll do a where and it has a type of prismabooks unique input and i should be able to just do that right and then similarly there should be a generated type for the dto so i think this should just be prisma dot books and the nice thing here is it looks like even if you don't know which one you're supposed to use as you type it out right you'll get some pretty good intellisense because it has those generated type definitions so i'm trying to update so we're going to do books update and i think we're going to do books update input here so again if we kind of take a look at that right if we look at what's inside the type definitions it's just the title because that's all our our book model has so just to kind of follow along and make this simple let's call this data and i should be able to replace this all right so i think that's all we need to do here now the problem though is because we've changed the shape of the inputs we also need to update our controller to map to that correctly so if we go to our books controller notice that we've got a patch method here on slash id and it's able to take in the id via the params and then the body from the request body so we need to update the shape of this to so i think the one update i need to make here is this really should be an object like that and i just realized we actually made the same exact mistake in our find one here earlier so we need to fix that so i think when we tested that earlier that was still running the you know the previous server so i should be able to do get by one get id three so that's still working good all right so we now should be able to do an update right so if we were to do a patch on book slash three i should be able to update this to a new title perhaps lord of the rings one hit send we got a 200 okay and if we go back to our get by id slash 3 returns that same object go back to slash books and you'll notice that it's now updated so our update works great so just kind of impressions here i think the the generated types are definitely very nice because you no longer it seems like you no longer have to create dtos from scratch however what i don't understand though is what do we do about validation so doing a search for validation on prisma prisma itself doesn't seem to do validation i mean of course when you run it against a database it's going to check for the various constraints that you have like if you know things are unique and stuff like that but it doesn't have the typical validation that you might do on the request body so for example you know min and max length or something like that you know unless you're unless i'm missing it in the documentation you know about it let me know in the comments but i don't see anything about you know just your basic request body validation so i kind of anticipate that what that means for sjs is that you likely still need to create dtos anyways and you know you use class validator like you've seen in my other tutorials to run validations off of that and then you're going to have to map the dto to the prisma schema of basically these types right the prism update input right so i probably could have just replaced this type here with uh prismabook update input but again how do you add validation to that so that's that class still needs to exist i think right so notice that this dto just extends create book dto so if i wanted to for example add validation in here so i won't cover it in this video but yeah you'll know from my other videos that you can use class validator to add decorators on the dto so that will add you for for example things like you know is a string a certain format is there a min and max length is it not empty is it optional stuff like that that's my assumption right now unless i'm missing something let me know in the comments but my assumption is that um the generated types doesn't exactly replace your dtos at least in sjs right so that doesn't completely eliminate these classes so that would be one pain point that i can anticipate not just in sds but in any any framework right where you do have validation and usually with validation it's represented through some form of schema right so you might be familiar with joy or class validator as i just talked about so what that means is likely you're gonna have to have a second representation of the the schema that does your validation so one potential minor pain point that some people will have anyways let's just keep going here and finish up the delete so again i think i can do the same thing here with the controller pass in the id as an object my remove here will probably be similar to this this input and we're going to do this dot prisma dot books dot i assume there must be a remove or perhaps a delete there we go delete and i bet that just takes in a where and i think we should be good to go so let's go back to our thunderclient again we got books one two three let's try to remove books with the id id3 so let's do a delete hit send we got a 200 okay we get back the book that got deleted and if we do a get again on slash books i expect that there's only two books in there and there we go very simple very intuitive all right so that's obviously a pretty simple example let's make this a little bit more complex by adding relations so i'm going to kill my application here and we're going to go ahead and do something similar where we do nest generate resource and we'll call this authors and we'll do rest api we'll do the same thing generate credit endpoints and so that's similarly going to create for us a new authors folder and actually i forgot to mention this earlier is we don't need this entities folder usually this would be the the entity class that would represent your your database table from other orms like typeorm in our case the the schema does that for us it represents our database table so we don't actually need this folder you can move that to trash same thing here for books all right so that means we're going to have a similar thing where we have a crud set of crowd endpoints for author and we've got an author service and we'll similarly have the ability to do crud on that but from prisma's perspective it doesn't yet know about this new entity so i believe what we need to do is go back to our schema and our prisma schema.prisma and we need to create a new model in here which is for authors so i'm going to do model authors and i think looking at the documentation i believe the the convention here is actually to make this singular author and i should have done that for books as well probably should have called this just book um well we'll just keep that books but just know you know i think typically this will map to your database table name and typically you want to use singular names for your database table so just something to remember a mistake that i made there or not really a mistake but more of a convention thing so we're going to have sort of similar where you know we need an auto incrementing id and probably a name instead of a title and then we're going to say that an author has books which is books with syntax like this to represent a list so something really interesting that i actually just discovered here um you know notice that uh we've got some missing things here like it knows it's missing the uh the schema side for the books that it has an author and i notice that when i do format it magically brings that in and i don't know how because why would formatting why would formatting bring in would update the schema i don't understand that but that's kind of cool i guess um i guess it's it's maybe part of the extension that i had to add in the syntax highlighting i'm not sure if that that's part of the features that it has but we'll make this lowercase a for author but yeah this is how you sort of represent according to the documentation relationships so we're trying to do a sort of one-to-many author has 120 books and the books has one author what's not super clear to me is how does this represent that relationship that it only has one author i'm not sure you know in in other type orm and other orms like a type program you have like an explicit decorator for when something is many many too many or one-to-many or one-to-one um here it's not as obvious so i'm curious to see how that differs in other situations uh it looks like they do have some documentation on types of relations so if you you want to dig into it deeper deeper yourself go ahead and take a look it does look like one one interesting to notice immediately is that for many too many you actually need to define three different models you know you ha you need to have basically you need to have a model for your relationship table in that case so that's something that's a little bit different to other orms like i think in typo rm it assumes that table for you you don't have to manually define it i don't think we're going to be able to cover that in this video so maybe a topic for another video is uh prisma relations but let's keep going here so i've updated my model so my expectation is that i now need to rerun my migration so i need to do npx prisma migrate dev dash dash name i'll do add author hit enter there and similarly it says that at rand migration it created the migration sql and my database is now in sync let's take a look at what that migration looks like so i've got the add author directory here migration.sql and it created my author table and and then interestingly the way it added that new constraint is actually creating a new table moving the data over dropping a table and then renaming it back to books which is kind of odd i think in in other orms the generated sql would be something like add add a column and then you know assign the the foreign key references etc so anyways i don't think it's a big deal but just kind of interesting so my expectation now is that i have an updated database that has my new schema but i also need to update my prismaclient so back in the terminal i'm just going to run prisma generate and it says my command was not found let's do prisma generate all right so that's just something to be aware of is you probably want to go through npx because you don't have that globally installed um let's take a look let's run our application and see if that client got updated and run start dev all right so notice that when we do a get on such books it now has author id on it so it's obviously working off of that new updated schema now in the author service i want to make sure to also add in the prisma service in here right so make sure that's imported and then similarly we're going to want to add prisma service in our providers in the module uh from nsj specifically you might want to create a specific module that has the prisma service in it so that you just use that module as an import everywhere that's probably what i would do in a more realistic situation but for now we'll just keep it simple and add prisma services provider for for each one of these modules so with that being there we should now be able to actually utilize this prism service so for example i think we should just be able to do something similar where we do this is that prisma dot and i expect that there is now an author field here that can do find many just like before all right so just do a good a quick test if i do a get on slash authors we get back an empty array so that's working similarly to before now what i want to be able to do is to be able to add a book along with the author so that we can kind of query books with their authors so i want to figure out how to do that all right so back in the book service if i were to allow creating a book along with the author there's i you know i can imagine that there's two different ways to do it one is that my data would have both the title and the author id right so it would look something like this right so you might have an object that looks like that and it looks like so just to taking a look at i was exploring the the type definition for the updated books grade input which now incorporates author because the schema and the type definition got updated you'll notice that uh this doesn't actually allow for that shape uh so you can't pass in the author id to this type from understanding you i guess you have to use books unchecked create input and i'm not actually sure what they mean by unchecked like what is the definition of that if i do a search under documentation for unchecked it doesn't really return much of explaining that and it's also not mentioned in this advanced you know type definitions generated types um so not super clear about what that means if you if you have an idea maybe let me know in the comments so my assumption right now is if i were to do that kind of insert you know assuming that perhaps the author is already created you know so remember we have our own author crud that we haven't fully implemented but if we were fully implemented that means we can also create authors and then once authors are created you should be able to also insert books with that created author so that's the way that you would do it this way you'd have to update that the type here to be this uncheck create input right so let's go ahead and give that a shot so obviously i need to actually input create my uh author create implementation here which is just going to be the similar thing of prisma.author.create and we need to update this to be prisma that author create input which will accept a required name and we'll pass that into our create here or actually that should be in data all right so let's try to create a new author through our thunder client here so right now we have no authors we're going to try and do a post on slash authors with a request body that has a name let's do jk rowling i spell that right hit send and actually have an error here i forgot to update my uh i forgot to update my controller so my author's controller so i need to actually update the type here to be that same prisma generated autograde input and i'll make it happy next found zero errors and is now running so back to our authors we're gonna do a post with a body named jk rowling and we'll hit a send and we got two long created id1 just like before so now my expectation is that i should be able to create a new book with title let's do harry potter 4 will be up to now and then i should be able to add author id here if i did things correctly and that should be one right so let's hit a send 201 created we've got harry potter 4 author id 1 so that works perfectly let's do a get on slash books and that's there as well all right so we technically just figured out how to relate an existing entity to another entity really just you know utilizing ids so that should be pretty typical for any database right taking a look at the documentation there is also a way to do nested rights so that's another option that you can do and i think this is where you would use that original type is which is where you you would pass in an object that has create in it and then that's how you would be able to add for example a new book an author at the same time if you wanted your api to do that so um you know there is a possibility to write both of those at the same time and do the relation automatically so i don't think i'm gonna cover that here but it's good to know about now what i do want to cover uh is the ability to include the author when you make the books call so how do we do something like that it looks like what i can do is pass in some arguments in here according to that condition that i can do include and then this would define relations so uh author and we'll do true here now back to our get books if we were to hit send again here it actually includes the author in there now so that's kind of nice that's how you would do joins i guess um this is this api is actually pretty similar to something like typeorm where you would have like a relations array in this case i guess you add in a an object you know a map of properties in boolean so you should so looking at the documentation what sort of immediately confused me at first is i thought that the the boolean here represents whether or not it's included that's not actually true if if you provide a true value here according to the documentation it looks like that just signals that it should include all properties so for example it looks like if we just wanted to select the author's name i think i'm supposed to configure it like this we do a select object and then we do name true and what that does when i hit send here it looks like you know does that limiting and there's a couple gotchas here in the documentation that you you probably want to read into more if you're if you're looking to use prisma so for example it talks about a couple different things of uh you know you can't do a partial select of the main entity and also partial select of the the relation you kind of have to do it this way through the select so a bunch of little gotchas that you just kind of need to be aware of i guess not super intuitive but i think that's just part of the the slight learning curve of the the newer api and in general it looks like there's a bunch of other things that you can do you know you can do all sorts of wears wear clauses different constraints um different ways to to include you know looks like here they have an example where they include posts where it's only only the included only include the published posts so lots of things that you can do here i think i'm going to need to play around with it more to see like is it is this api able to represent all of the typical sql queries that you would have to do not sure yet at this point i'd have to spend more time with it i do have to say they have really good documentation right so like you know they have stuff here on pagination i mean doing a a quick glance at the syntax you'll notice that it's actually very similar to type orms find api if you've ever used that they they have that same exact well not the same exact but very similar find api where you can pass in some kind of configuration object that has you know select squares and relations or in this case they caught include pagination same thing right so i have the the type or as an example documentation here find options you'll notice that it has that same similar syntax of dot find and then some kind of configuration object where you can do selects you can do relations you can do joins you can do where clauses so very very similar api so i'm i guess if i if i can kind of wrap up my my thoughts here and we've got a you know fairly semi-working api we've been able to do crud and we'll be able to do relations we've been able to query those relations there's obviously a lot more to go over here there's so much to to go over that i can't really cover everything so i think that i think i'll conclude my code portion of this video here and kind of want to segue into my impressions so you know i just tried it out for the first time you know this is not a scripted video the first time i tried it you know one of my concluding first impressions one is that it seems fairly intuitive at least i think because i'm coming from a typo or in background again the defined api is kind of similar it feels like it's not completely new the documentation looks great it's probably some of the best orm documentations i've ever seen it's definitely working at a slightly higher abstraction obviously so there's you'll notice that there's stuff in here where they have a full text search api for postgres databases and if you look into it they have this sort of format to do matching so it's kind of a specific format i don't think this is a specific syntax uh for porsches that i know of because i'm more familiar with you know using the uh the like the sql like you know the sql like operator where you know you use that with a where clause that's what i'm familiar with when you're doing text searching so i'm curious to see what the motivation was behind this kind of syntax over here and obviously that's just one minor thing there's a bunch of other stuff in here so at a glance what do i so at a glance is it something that i would replace type orm with maybe if i learn a little bit more about it and see what other benefits it has but at least my impressions here is that i don't see anything super new so so as an example one thing that i noticed in documentation here you know under is prisma and orm i remember reading something in here where they they talk about something the pain points with orms is that they're claiming is that you have this uh you know you write migrations you run your migrations and then you update your models uh whereas they're trying to promote uh prism is trying to promote their own way of you update the schema and then you auto generate and run migrations off of that you can do the same thing the same exact thing in typo rm in fact uh that's actually one of the features that i really like about typo rm is that you know as a typewrm you have this ability to automatically generate migrations from your schema so it's it's kind of the same exact thing where in type rm you would create your schema through classes and then the the cli for type rm would look at those entity classes and auto generate the migration from that so similarly you can you have type or m use the the entity classes or the schema as the source of truth which is the same exact thing that prisma is trying to do so i don't see the immediate benefit and you know one of the things with the the prisma schema is that it's introducing a a new syntax a new schema syntax i guess it's very similar maybe the same i'm not sure as like graphql you know it looks pretty similar but at least if you're writing in sas for example it introduces that new schema file that you have to maintain that is syntactically different from everything else in the code whereas in typo rm you'll just be creating entity classes with very similar decorators that you see ever everywhere else in the framework so i think for nasja yes i'm still at least a quick lance here fits a little bit better in that ecosystem but i mean as you can see though one of the things that we've noticed here is that the integration was very simple and easy i did not run into very many problems only thing is you have to keep calling you know generate prisma generate every time you update the the schema you want to update the client i think i read somewhere that there is a watch command so that it'll automatically watch for changes and automatically update your your prisma client but i haven't seen that in the documentation yet so just wrapping up my thoughts here is it the next generation orm uh i'm not convinced yet i think i need to play around with it more and see you know what really makes this orm unique and again like i mentioned a lot of at least a lot of what we've seen in this run through is things that i've already seen in typo ram so it's not completely new to me although lots of definitely like uh interesting features like the auto generated prisma client that is completely aware of the types and the schema so that definitely edges out i think typo or m like if you really care about type safety that's a super cool feature and i didn't get a chance to to cover it but you can actually also do npx prisma studio which opens up a client like a database client like this so you can visually see your database tables it's pretty basic you know it's there's a lot better tools to do this like d beaver or db visualizer or my sql workbench pd admins etc so it's a pretty simplistic database viewer that i feel like most people probably wouldn't really use other than just to see the data quickly i'm not yet super convinced that the separate schema that's using a completely different syntax from you know the rest of your typescript code is is a positive but again the fact that it's able to auto generate migrations and the client off of that is super cool that definitely is sort of uh you know next generation feature i thought that the migrations were very easy it's simple to understand what i'm curious about is is it is it gonna be able to represent very complex relationships in a schema i think i'll just need to to try it out to find out and also it does again like it played well with sgs and i can imagine it can easily play well with anything else so it was pretty simple to set up so just to conclude uh i really like this i think i'm going to explore this further i'm going to learn more about it and i'll probably make a crash course once i've really sort of internalized the benefits and the features and if that's something you're interested in let me know in the comments and also let me know what you what you think of prisma is it something that you think you'll be using and was this format of a video something that you found useful you know me kind of walking through it as i'm learning it uh so a little bit of a different tutorial format not really a tutorial it's more to just watch me learn something anyways i think this video is probably already too long let's go ahead and conclude here and again any feedback you want to let me know in the comments go ahead and i'll see you in the next one thanks for watching
Info
Channel: Marius Espejo
Views: 2,185
Rating: 5 out of 5
Keywords: prisma, prisma 2, Prisma 2 Beta, prisma nestjs, prisma sqlite, prisma postgres, nodejs orm, prisma nodejs, prisma typescript, typeorm, nestjs typeorm, typescript, nestjs tutorial, nestjs graphql, nestjs microservices, nestjs authentication, nestjs, postgres, mysql, sqlite, node, node js tutorial, expressjs mysql, mean stack tutorial, mern stack, database, relational databases, nestjs database, nestjs database connection, prisma tutorial, prisma nestjs tutorial, prisma migrations
Id: Aq1U_Ku8Jig
Channel Id: undefined
Length: 54min 45sec (3285 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.