What’s new in Prisma (v2.6.0)

all right hello everyone welcome to another prisma live stream we uh are here today nico and i my name is ryan and this is nico my colleague we're here to talk about uh what's new in prisma uh what's new specifically in prisma 2.6 which just landed this week um so if you haven't been here before uh welcome this is kind of a regular thing we're doing along with releases these days where we talk about uh what's going on what's new and and uh nico and i kind of riff on some of our thoughts about prisma and typescript and all that good stuff um and if you are joining us uh again from last time welcome back thank you for being here so um yeah nico i don't know maybe for people who don't know us people who are here for the first time let's let's do a little intro about ourselves why don't you go first yeah sure i'm nicholas i lead the developer success team here at prisma and i also do these live streams i occasionally wrote blog po write blog posts on our blogs help out with documentation really everything that helps people to use our tools better what are you doing here ryan what am i doing i i'm working in uh dev rel developer relations and uh focusing on graphql quite a bit but touching all the aspects of brisbane as well um so that includes things like you know collaborating with uh the rest of the company on on new stuff it includes things like writing posts and doing talks and uh talking to the developers who use prisma as well so that is what i am up to um and today we are going to talk about some of uh the stuff that's new we've got this 2.6 release that just landed on tuesday uh we've got a 2.6.1 there's a patch release that's gone in since some new cool features in that we'll talk about what's uh new including things like uh atomic number operations uh a much requested feature that is now in preview uh mode uh and then some some improvements to prisma as well um we will talk a little bit about typescript 4.0 which just landed and also type graphql which is a library that helps you to build graphql apis in typescript using uh using some pretty cool features like decorators um and uh and it gives a nice way to build a graphql api and it's got some really cool prisma integration as well um so yeah 2.6 release nico why don't you give us the the high level what's what's uh what's new in it yeah for sure um actually i'll start sharing my screen for this one and like i wanted to say actually uh like for our introductions maybe a little bit of an uh of a quick add-on here uh you're based in canada ryan yeah that's right exactly actually where are you exactly i i am in ottawa ottawa is the capital of canada and i am uh in kind of a suburb of ottawa so that's where that's where i live and uh ontario close to montreal where we have uh we have someone else at the company who's in montreal uh jason who i haven't met in person yet but maybe once times are back to uh somewhat something that looks like normal jason and i will collaborate together because we're pretty close to one another very cool and what time is it where you are right now it is 11am and you're just finishing your day because you are in berlin exactly i'm a little bit jealous i have to say i also would have been uh preferred uh taking this stream today in the morning i had a pretty long day already with a lot of meetings and a lot of talking so let's see how energetic i'm going to throughout the stream and i'm sure that once this is done i'm going to call it a day let's jump into the release i'm going to start sharing my screen here and for a second one second let me pull up the press my kid up first share screen and now you see a little bit of an infinite tunnel here but this is what you want to look at the 2 6 0 release actually not the patch release but the actual release ryan is this readable or should i increase the phone bump it a bit there i think maybe even one more and you're good now i think all right there you go uh so let's talk about the release and what we have been up to over the last two weeks so first the the one feature that i'm pretty excited about is that we finally have a more robust introspection that keeps manual changes that you made to the prisma schema file so this was actually a major complaint that a lot of developers had since we released the beta version back in march actually because prisma migrate isn't ready yet as i'm sure a lot of people are aware and still waiting for it but this is why the recommended approach for using prisma production today is actually by doing migrations yourself using a third-party tool or just plain sql however you prefer running your migrations and then generating the prisma schema through introspection the only problem with that approach is that introspection overwrites any changes that you made to your prisma schema to kind of optimize your prisma client api um and like part of this is one second [Music] i had a note here from my monitor that i needed to click away but now i'm good again yeah introspection overwrites the manual changes for example if you're using a ad map or edit map attribute inside of your schema then this is going to be gone after you run introspect again or if you rename relation fields which i think a lot of people are doing to have a more kind of ergonomic prismaclient api well tough luck overwritten as well um but now with this new change in the in the latest release these changes will be kept in the prisma schema have you used introspection a lot ryan or do you typically for kind of the example projects that you're working on do you typically use migrate for those because you're not building anything for production right right and so yeah it's typically been migrated um so i touched introspect it just briefly and um maybe you know something that might be interesting for me and maybe for others too is to like just maybe if you could chat a little bit about what introspect versus migrate like how to think about that uh maybe just really high level the difference there yeah of course i mean the the big difference is that when you're using migrate then the prisma schema really is the source of truth for your database and also for the models that you're using in your programming language because both are generated based on the prisma schema so you write your prisma schema and you map it to the database and then you generate the client from it single source of truth is the prisma schema the big difference when you're using the introspection approach is that the database suddenly becomes your single source of truth where everything starts because that's where you declare your schema in the first place using sql create table statements for example then you run introspection this generates the models for the prisma layer and then you run prisma generate which in turn generates the uh models in your programming language so i think the the main difference is kind of where your single source of truth for your schema is sitting and with migrate it's the prisma schema and with introspection it's pretty much your database schema and i actually prepared a little example that i want to show that shows how exactly um this uh works so i have my code right here i have prepared everything right now i'm still on the old 2.5 version of prisma so this is not yet within new features with the new more robust introspection and as you can see my prisma schema at the moment is empty i just have a connection to a database here and in fact if you want to take a look at this i also have that prepared right here in postico and we see that we have three tables users posts and profiles and let's see what's what happens when i now run prisma introspection it updates my prisma schema as exp as expected and it pretty much translates these table definitions into prisma model definitions what i don't like about this is that the the generated models they don't really follow the naming conventions that we have for prisma models so typically prisma models are spelled uppercase and in singular form and that's because that just makes for a more natural way to query your data in the prismaclient api so what a lot of people do to then adhere to these naming conventions and therefore optimize their prismaclient api they just go and rename this and how you can do this is actually by using the added map attribute where you are mapping this model name to this table name in the database so i'm doing the same for profile point to the profiles table underneath and to the users table underneath and of course i now also have to go and update the relation fields so let's go ahead users is now called user and profiles is called profile and posts is called hosts i love my job here at prisma because i'm really becoming an expert in building blogging applications thank you thank you and so another thing that a lot of people uh want to do is also actually rename these relation fields one thing to note about relation fields and prisma is that these are fields that do not manifest in the actual database schema so this user's field here for example this you won't find an equivalent column in the database schema if i'm looking at the post table in posty go and at the definition i don't really see a column that's called users and that's because this is this field here purely exists on a prisma level to make it easier to work with relations and um in the prismaclient api but also already on the schema level to understand better the relations between different models so it's really like a matter of like more expressiveness and and more clarity when it comes to working with relation but if we look at the semantics of this model at the moment then we kind of realize that the user's name doesn't make a lot of sense because at the moment this is pluralized and it's pluralized because it was named according to the previous model field that was users but now it's called user and this doesn't make sense because it's plural so we certainly want to make a singular version out of this but also to put a little bit more meaning to this field i prefer that's calling it author because then it also maps with the author id field that we actually have defined as a foreign key in the database the same for profile here again users is called user because it was named after the model that was previously called users and this is actually correct this is a lucky coincidence for us that posts and profiles actually know this has to be singular as well but posts is already pluralized so these kind of changes people will want to make in their prisma schema to optimize the prismaclient api but the problem with this is that once you run introspection again and remember i'm on the old version i'm back to square one and i can go and adjust it again now and this is pretty much what you have to do after every sql migration after every change that you make to the prisma schema that you want to pull it into after every change that you make to the database schema that you want to pull into the prisma schema you'll have to go and make these manual changes again and that's not fun and there were a couple of community members actually who built scripts or small npm tools that would help you with this workflow but of course ideally we would be smart enough to just keep these kinds of changes and so with the newest version and i just quickly want to show that and because i can i just do command z here and go back to the previous version um and we'll update prisma to the latest 2 6 1 release i guess i have to add the last dot here make sure that i delete oop that also deleted the migrations folder but i don't think that's a problem install dependencies again this will generate the 2.61 version of prismaclient and the prismacli and now if i run yarn prisma introspect all the changes are kept and in fact we even get this note here that the models were enriched with map information taken from the previous prisma schema so the introspection cli even tells me um what kind of manual changes it kept compared to the version from beforehand so this is really nice this is one big improvement i think for a lot of people who are currently using introspection but who are still in a phase where they have to modify their database schema a lot and then can do these manual changes and can be sure that they're actually being kept uh when they re-introspect the database again that's so cool are there any gotchas with it that you know of that like maybe if people are using uh their prisma model in a way that is not maybe you know really typical that they should question i'm actually not aware of of anything that people should bear in mind i hope that things just fall into place here and like to be clear i also showed only a subset of what's possible here so i only showed how this reintrospection now keeps the at map attributes um and also the changes to relation names but there are a couple of more things that are also kept so for example the updated add attribute or if you make like any comments to the prisma schema as well these comments will be kept as well so a couple of things that don't manifest in the database so the like one way to think about it is that before in version 2.5 introspection really just looked at the database and wrote the prisma schema into the prisma schema file that it derived from the database schema but now it first derives the prisma schema from the database schema and then looks at the current prisma schema to take over these manual changes and merges them in into the new version very cool that's awesome all right and then we have another one atomic number operations on update i think this is also something that a lot of people have been waiting for have you looked into this one already yes this is a very cool new feature because um i think as you were chatting with me about uh prior to the stream this used to be something that you had to do in multiple steps or have like some workarounds or resort to raw sql to do if you wanted to do these uh kind of database level number operations you know like math on on your your columns if you have like integers or floats and you want to multiply or add or increment or whatever this used to be something that was like a not a very good developer experience um so i don't have a lot of familiarity with what it used to be like and i think you're gonna show what we used to do but i know that the new api is great it's you know the typical kind of prisma syntax that you would expect in your your prisma clients and just um allows you to do things very easily absolutely yeah um and what this also relates to actually is the question of transactions in prismaclient because i'm sure a lot of folks will have followed this github issue here where there has been a lot of discussion and um partially even like a heated debate around prisma supporting long-running transactions or not and all the different workarounds and fallbacks that we're offering so if anybody is interested into that topic and how we're thinking about transactions in prisma then i definitely recommend you to just go to this github issue and read through the entire history because it really provides a lot of context and the latest um information was from herve that was posted uh her race our vp of product and he kind of explained um how we are thinking about long-running transactions and in particular the kinds of work-arounds that we want to employ for now because long-running transactions themselves just like providing the same transaction mechanism that people might be used to when using plain sql is really something that makes it very easy for developers to shoot themselves in the foot and in fact that kind of transaction feature is also very often abused and also sometimes confused in terms of that people think they get a certain guarantee but in fact they don't or they run into a performance issue because they didn't understand that they were locking an entire table when they were running a certain transaction so we're thinking about ways how we can prevent these kinds of confusions and bad practices and and these these traps basically for developers with proper apis more dedicated apis that developers can use and one of those is indeed the atomic number operations that we released as a preview feature in this week's release so again i'm starting in a very simple project with version 2.5 to quickly show you what this looked like before let me delete this part and also quickly open christmas video because i'll use that to illustrate the changes that are happening to the database come on all right i have a single post in here that has four views right now and we are now back in prisma version 2.5 so what if i wanted to send a single prismaclient query that is incrementing this number well i had basically two options in prisma 2.5 one of those is to use a raw sql statement or actually i'll start with the other option i'll start first with the option where i already use the prisma api but i have to do it in two queries so here's how you can do that you could basically just retrieve the current number of views right you select a specific post where they find one query this returns one object and i say on this object i actually just need the views property to quickly show you the model here so it's a straightforward post model with a views property that is an int and i'm just retrieving this so current views at this point is an object that really has this one views property then i have to check if of current views here to make the typescript compiler happy and actually i think then i should also be able to remove the question mark operator and that's how you can do it you send two prismaclient queries where you first retrieve the current value and then you send another update query where you just add one to this current value so just to convince ourselves that this works i'm running the script let's say two times and back in prisma studio i would expect that this now has six views and that's exactly it so that's one way of tackling it but there were already people that weren't quite happy with that approach because of course you need two database queries and that's not very efficient for a fairly simple operation so what you could do instead is you could use the raw api of prismaclient and that looked something like this with the sql that you had to write so you could use the execute raw function that's exposed by prismaclient and then you have to write a little bit of sql where you add this number to the views property and again if i run this two times i would expect that in prisma studio we are now at eight views and that's correct but so we now saw these two options the first option was with two queries the second option was with a well sql string that is probably not so fun to write so let's see how we can resolve this in the latest release i'm going to update again my version of christma to the latest one we install dependencies and regenerate prisma client and go back into this script and let's see how i can now achieve the same with the new atomic number operations this mod.post.updates specify which post i want to update there's just a single one with id1 and now i can select the views property and because views is an integer um did i update everything correctly let's just make sure the audio completion didn't show the atomic number of her ah i know why because i commented it still out so of course it's still in preview so i have to denote it here in my prisma schema that i want to use this prisma feature i regenerate as my client and now going back let's see if i have to restart the typeclip server or not 5050 i'll invoke the other computer hey it's there it doesn't work right yeah i find that i have to restart it like almost 90 of the time me too me too i was actually surprised that it worked here but that's the new api that you can use so because views is an integer it now exposes all these different operations that you can perform on integers in this case these are fairly simple kind of arithmetic operations and if i run this with the r and f let's follow what we did before just run it twice going back to christmas studio and here we go with 10 views so that's the new api and i think that's a big big step forward compared to to what we had before and of course people might have more fields in their prisma schema or more types where they might want to perform these kinds of atomic operations on for example you might want to concatenate strings and that is something that you also want to do in an atomic operation that we currently don't offer yet but these are the kinds of things that we'll now gradually add to the prismaclient api and for the time being you'll have to unfortunately accept the kinds of workarounds that i was showing before by either writing raw sql if you really care about efficiency or if you say okay it's not that important for me that i always have the most optimized database queries in that case you can also go and just send these two prisma client queries that i showed in the beginning where you first retrieve an object and or the value that you want to update and then in the second query you just send an update query where you modify that very cool and it is only available on the update command and that makes sense right because exactly you're gonna you're gonna want to only um operate on these numbers when you are we have existing data right that's the time that it makes sense to do so exactly yeah cool um i had one other interesting conversation today actually with um with another colleague who suggested that we could also provide a generic function here so like something like let's say this is update and then we would pass in basically a function that takes the current thing and then we um we could just update it here like something like this of course like this code won't work but um this could also be an idea of what the api could look like and i actually really like that approach because it provides a lot of flexibility the only problem with it is because i i mean the the reason why it's very flexible is because you could basically off the bat use it for any data type so you don't have to wait for us to implement specific operations but for example if this was a string then you could say okay string and you concatenate it with like another string here hello or something like this right you would have all the flexibility um to to perform any operation you want and you wouldn't have to to wait for us to perform to implement these specific operators but the key here is that it's about the execution level and if you write it like this for sure we could provide an api like this but it wouldn't make the entire thing more efficient because after all this would get executed on the typescript level and not on the database level and the entire point of atomic operations is that you send one database query that is able to handle this for you and in this case um that that that just wouldn't be fulfilled so um it's it's not the the kind of thing that um we want to do but one thing and that's the last thing i want to mention about this feature is what what we're considering is at some point this is fairly far down the future so please don't hold me to it yet it's just like spitballing couple of ideas that we are throwing around internally is basically writing our own expression language so um like imagine this would be sort of a template string where you could now write a custom prisma expression that expresses the operation that we can could transform into an abstract syntax tree which again we could transform into the corresponding sql query so this is kind of an idea for a prisma expression language that could be useful in this regard and like not only would be applicable here but for example could also have use cases within a prisma schema for example for data validation where if we had this kind of expression language you could also hear do something like uh check and then in here you do like a prisma expression like um x has to be greater than zero because views always has to be positive something like this but these would be kind of the use cases for a prisma expression language that we might explore in the future i don't want to promise too much here um if you like that idea i don't think we even have a feature request for it so if you really like it then just go into github and create a feature request for it that's cool though that's a good idea very nice all right yeah that was a lot of talking um and i think that's also mostly it for this release cool well we can talk about uh typescript 4.0 now if you want yes um so i i haven't um really gone into detail about what's available with typescript 4.0 maybe some of the viewers are more familiar maybe you can drop some of the features that are new that you like i think that uh type inference has improved in typescript 4.0 um there are some other new things there as well and i think there has been some something has landed in 4.0 that has uh helped out prisma uh which maybe you could talk about nico exactly and that's what uh we wanted to quickly discuss here on the stream just to make people aware of of this change so since january actually we had this open issue here about hiding top level client properties in the auto completion if you have used prisma in earlier versions with typescript then you might have noticed that when you're invoking the auto completion on a prismaclient instance that you not only see the top level prismaclient methods like connect and disconnect and not only the properties that represent the the queries for your models like comments but you also get a lot of stuff that you probably don't really understand what it's being used for things like connection promise dmm f engine engine config these kinds of things you won't even find in our documentation because as a matter of fact they are implementation details and in our prisma implementation they have been declared as private properties but there was a bug in typescript that exposed these private properties to the auto completion in vs code and in the latest typescript release as joel pointed out here so he he pointed to the the bug in typescript intellisense and in the latest typescript 4.0 release this bug has been fixed which in turn just resolved the issue for us so from now on you will see the right properties and only the right properties that you can invoke and another cool thing that also came with this release is that when certain methods are marked as deprecated they also show up as being strike through in the auto completion so connect disconnect and execute draw and on two releases ago we introduced this version where you have to prefix those top-level client methods with a dollar sign and we deprecated the old ones so you are still able to use them but uh sooner or later you will have to switch over to the dollar prefixed version so that's also new in prismacline in in in the new version of prisma but only because that bug and typescript was fixed and typescript 4.0 has been released very cool i am looking forward to digging more into typescript 4.0 as well to see what else is new in there so i will let you know what i find um and i think someone who's been talking about the 4.0 releases uh ben awad he is someone that maybe lots of us know on twitter on youtube uh he's got a very big youtube channel where he um he creates lots of tutorials and has a lot of opinion videos as well about the uh web development industry i suppose perhaps you could talk a bit about this uh exercise you went through through where you took the project that he did in this gigantic tutorial that he put up which used a lot of the things that that you know many of us love like react and typescript and graphql he used type orm as his database layer uh in his his tutorial and you went through and took a look at what you what what you can do to basically take out type orm and put in prisma so maybe talk a bit about that absolutely and i really love the transition that you made here i had exactly the same transition in mind that ben actually posted a video about typescript 4.0 and it was the the it was perfectly on point ryan very very good we're well coordinated so well in fact i um so i also watched uh at least the beginning of this video and already the introduction uh like really made me laugh out loud in this case well i actually like uh quoted it in a tweet of mine that and and like what he said in the beginning of this video is if you're still using javascript i've got nothing then respect for you you're playing web dev on hardcore mode you've got friendly fire mode off if you want to shoot yourself in the foot you can shoot yourself in the foot and i think that's just like very accurate um you're just like uh you you you you don't have any security measures on on what you're doing and you're just in a very kind of risky area if you're using plain javascript um but yeah let's let's talk a little bit about his his project he published this 14-hour gigantic tutorial where he built a reddit clone pretty much from scratch and as you explained i went through an exercise of migrating the tutorial that he built with typeorm migrating that to prisma and in fact i used kind of the incremental adoption approach for this so i first kind of cloned his repo i made it work on my machine i had the entire application running and then i thought okay what does it feel like to now gradually replace type rm with prismaclient so i went in there i started with introspection i generated my prismaclient and then i one by one looked at the database queries that he made with prismaclient uh with with typorm and adjusted them to use prismaclient and the results can be viewed here in this git diff uh that i want to show to everybody now that shows kind of the the the difference um between type or m and prisma so here are just a couple of config files that are being exposed um this is the orm config file that's used for type rm so that of course got removed as well in the transition there also was a leftover in the package.json for microrm some configuration because i actually think that ben started the building the app with microorm and only then switched over to type orem but the dependencies that were switched out were basically i removed the pg driver because that's not needed anymore as well as the type or m dependency here and added the prismacli and client dependencies then of course we're adding the prisma schema and in turn we can remove the migrations that are a little bit further down i think in the skid diff we'll see it later and now we come to the exciting part where we basically just delete a lot of existing code so one thing that i really noticed with this migration is how much less code i had to write using prismaclient compared to what it was before with type oren so here for example this is the entities post file and because ben was using type graphql and in fact like orm and type graphql they they play nicely together because they both follow the decorator approach um so you would define your models as classes once and then using the type or m decorators you can turn them into database models into database entities and using the type graphql decorators you can turn them into graphql types so in fact i think this is probably one of the best approaches for building really solid typescript graphql apis to this date but i think um we'll also later talk about the prisma type graphql integration and i think with that you get to a similar level of convenience but let's just go through here and and check a little bit what has happened so here we just note that we can delete all of the decorators from our model classes because now these model classes really are only relevant for the graphql for the type graphql layer to define our graphql types i could entirely delete the updo model updude is ben's model that stores votes on post for the user model i just did the same i removed all the typeware and decorators and just was left over with a pretty slim type graphql class basically that just defined my type and then in index.ts i could also delete a bunch of code so all of that i didn't need anymore i also was able to delete the data loaders that ben created for his models because the data loader is built into prismaclient and i don't have to explicitly use it myself so that's also really nice the only thing that changes here when defining the graphql schema is that i attach prismaclient to the context and we can remove the user loader and the upgrade loader right here here is where i deleted the migrations because we now use prisma we don't need those anymore and let's take a look at a couple of queries so let's take maybe this one as an example so this is the vote query where the the functionality of this is that you send a vote mutation actually and in there what ben does he creates this updo model that stores a vote for a particular post by a particular user and there is a little bit of logic required to make this query happen where ben used a transaction to first update the the updo model and then um update the the the post respectively and the the difference between the two here is that um like first of all what ben does he um creates the transaction here and then he writes plain sql um using typeorm and sends this to the database and with prisma you can do the same but in a very type safe way so we don't have the um the the the sql strings here but instead we just create these operations and one thing to note about this actually is that this will return a promise that is not yet resolved right i'm not using a weight here so this is not executed immediately but rather this operation one here this this constant just stores an unresolved promise and the same for operation two also no await so again this is an unresolved promise and to resolve those i'm now using prisma's transaction mechanism that is going to run both operations immediately one after another and then after that we have pretty much the same logic but just for the case where a user has never voted before and i think that pretty much all in all you can look through this and you'll find that a lot of the queries uh now look a lot more natural and it just feels a lot cleaner overall with just a consistent api that i'm repeatedly calling when i need to send uh database queries uh to the database we got a question on uh dollar transaction i haven't really touched that much um so what's the like what's the implication there maybe you could even scroll back back up so you know i think the the idea that ben's getting after here when he's running this transaction is he wants to make sure that the the the first operations will be successful before committing the the final operation um which exactly and that's what you get out of the dollar transaction from prisma as well like if if those op one and up two if those were to fail for whatever reason uh they'd be rolled back kind of like the whole thing would be rolled back exactly so the point of this transaction is that it might be that if this wasn't wrapped in a transaction that this sql query succeeds but then for whatever reason the second one doesn't succeed and this would bring the data into an inconsistent state in the database and the developer is responsible for that the developer at this point is responsible to make sure that either both of these queries succeed or none of them does and the way how you do that in a traditional way when working with databases is that you wrap it in a long-running transaction and this in type oram looks like this that you define this transaction like this and then you write the query here in prisma as we already discussed today we don't support these kinds of long-running transactions and instead we want to build dedicated apis that people can use for their specific use cases and one use case is simply to batch operations and and send a batch of operations to the database where you can be sure that either all of them or none of them succeeds so if i now use the prisma api here and use the transaction mechanism that we provide i can be sure there is a guarantee from prisma that either both of these operations succeed or if for whatever reason operation two is going to fail then operation one is going to be rolled back as well so that's kind of the idea behind this transaction top level client method very very cool yeah that looks a lot more ergonomic like i'm looking at the diff and i'm like yeah the right side feels i mean maybe we're both biased nico in some way or another here there's a possibility of that but uh but no like it feels the ergonomics of it to me like just look better um okay so do we want to talk about uh type uh graphql now yes that was a lot of talking from you i am happy to hand it over to you you take a break you you have a well deserved break all right so maybe if you could pop my screen onto the share there you go all right so uh type graphql there's um a lot of people who are familiar with this project um if you're writing graphql servers and you want to do so in kind of a type safe way where you get you know generated things for you you want to use typescript and all these features from typescript like decorators type graphql is one way to do that um another way if you're familiar with prisma and maybe you know about nexus and nexus gives you a typesafe graphql api in a code first way what that means is instead of writing schema definition language and then your resolvers separately you kind of write all of that together uh just as code and uh it's a bit cleaner like you you don't have you know to switch back and forth between sdl and and code for your resolvers you get to do all all that stuff in one shot so type graphql is pretty popular um and there is now a um a generator that is available to make it working with prisma super easy with with uh type graphql so i've got this post type here again i am making a career out of writing blogging applications just like nico is and uh we are decorating it with object type which will then be fed here into this resolver this resolver decorator it's going to give us a way to write a resolver for that type and when we put it all together with build schema we can then start up an apollo server and in fact if i come over here and hit play we've got these two posts these two hard-coded uh posts right here so that's cool for hard-coded data or if you want to you know do something here if you're in the your method and you want to go you know fetch from database whatever and then return your data so that's cool that's something you can do but if you are working with prisma and you are wanting to um you know work with your prisma model like this for example where you've got a post this whole process becomes uh quite a bit easier and so why don't we take a look at how it becomes easier i have installed something called type graphql-prisma so in the package.json file here in the dependencies i've got typecraftql.prisma currently at dot 6 for the the version and if i put in some configuration here if i put in a generator for type graphql and then i give it the provider just the name of the package there and then an output what i can do when i now do my prisma generate is i can get this generated directory here that has a ton of cool stuff and in particular what you'll find in here is a whole bunch of stuff that is automatically generated code which gives you basically a graphql api that maps up to your resource that you've created so in this case i've got all of these crowd operations that are available now simply by having a single model in my prisma schema and what this looks like in practice if i wanted to start to make use of this i've got this sqlite database here dev.db if i wanted to make use of this i can actually go in and take this um let's find it here uh this this one right here post crud resolver this is going to give us a ton of cool stuff let's go into server.ts and then up at the top i'm going to import host crud resolver from dot slash generated and then type graphql and then down where i'm making my schema where i'm building my schema now instead of including post resolver i'm just going to replace that with post crud resolver and let's just make sure that the server did its thing looks like it restarted if i now i'll just refresh for good measure here if i now take a look at the docs i've got all of these queries and mutations that were auto generated for me so i can hit that same query right now i've got no posts but if i do a mutation i do create post i give it some data i can give it the title that it's looking for and i can give it a body um and then we can get back the id the title the body if we want to we hit go there we go we've got uh we got a post put in another one then we do a query again for posts uh post plural get id title and body and hit go on that there we go we've got data coming from our database and um that was pretty cool because i literally well to get that api stitched up i just started with this simple model and it was all generated for me um so you know that that's cool for a lot of cases like that hits a lot of uh use cases where if you want to do crud operations and you're not so particular about like your model having sensitive data um that you know can be it can be really valuable in that way um it can you might have to make your own uh resolvers though if you don't want to resolve out certain fields like passwords for example if you've got like user models and you've got password stored there you don't want to start you don't want to make those available to your graphql api but all that to say a ton of work that's done automatically for you by opting into this typecraft ul prisma provider and there is something similar for nexus we do we do something similar where you can generate your uh your corrupt operations and get get the same kind of cred ops that you would with typecraftql different options here uh have a play with it let us know what you think and curious nico about your your um thoughts about type graphql uh now that you've kind of explored this further yeah i think uh it's a it's a really really nice tool and it uh it probably depends a little bit on kind of what your preferences are in terms of how you like to work with typescript and i think it also depends on what your background is as a developer a little bit because the decorator approach that type graphql uses um i think it's also fairly common actually in the angular community if i'm not mistaken i haven't worked with like angular but like that's what i kind of am aware i think and also for example in the nest js community which i think is also fairly tightly coupled to the angular community so i think for people from these communities type graphql will feel like fairly natural because it uses a lot of the same kind of patterns and i think even that uh in nest js um type graphql is one of the most popular ways of building a graphql api for nest when you're building your your back end with the the sjs framework so yeah super exciting stuff and uh what what we are focusing on of course with with these kinds of tools like we want to make it as easy as possible for everybody who builds an api whatever tool they're using might be type graphql might be nexus even sdl first or if if it's if it's just the rest api like we want to build these kinds of integrations that leverage the information from the prisma schema in order to already generate code for you that you would have to write anyways uh on your api layer and of course there are more questions when it comes to authorization and and things like this and we'll figure these kinds of things out over time but what these kind of tools and integrations are certainly already very useful for is actually prototyping i mean you saw how quickly you came to a point where you had a full-on graphql crud api and uh it would have been like virtually no effort to add more queries and mutations you could have done this just in a like matter of seconds or minutes um to to have a full blown crowd api for your database and i think especially during prototyping stages that's exactly what you need so um i'm i'm super excited about this kind of work and uh we'll see where where it's headed and which tools and libraries we're going to pick next to build integrations for absolutely yeah i'm i'm pretty jazzed up about it as well um so there is uh there's you drop some links there in the the chat i see you want to talk about the uh maybe the hacker news article or um thread yeah i'll uh once more very quickly share my screen and i think we're already very close to wrapping up this session today so i just want to quickly plug to um two things that are currently interesting in the community so the first one is a a medium blog post that was published just yesterday by nick mchenry maybe some of you already have seen it maybe not okay this is not what i wanted to do wait am i screen sharing already oh yeah okay it's getting late here i'm sorry so this is the article it's really a practical comparison of prisma and type or n and i think especially if you're a type or a user this is going to have a lot of really good information for you and it's it's by no means a comparison that just favors prisma but it's very kind of high level and objective and that's what i really like about it and it's also very practical in that it compares different workflows um and and explains with code snippets what these workflows look like so for example here is the the section that talks about how to set up prisma and how to set up type or m on the other hand and it kind of shows you the first steps that you need to take with either tool in order to get something like this uh like a simple setup going and then nick continues in that kind of manner by just looking at the different kinds of queries and comparing the api that's being provided so for prisma he explains that this is how you do this query with prisma and then kind of showing the same for type or m and that's how he approaches the entire article i'm just comparing the two putting them side by side and explaining how the api differs and illustrating this with very nice examples so i'm a big fan of this article and i think especially as i said for type or m users this is going to have a lot of really valuable information for for them to look into um and at this point i also quickly just want to point out that we have actually something fairly similar in our docs in our docs we have this section that's called api comparisons and this is where we do pretty much the same but not as detailed as nick did where we just put the apis of prisma and different other orms next to each other so here we have the same comparison with type or m but also for example with sqlize we can just scroll through and understand how the prisma api differs from the ones provided by other orms that is one thing and now coming to the last thing is there currently is a hacker news thread about an a new touchscript organ that was released that looks actually very interesting and of course whenever a new tool is out on the market that is related to us we just um have our eye on that and um like want to look for inspiration and want to see how people compare it to prisma so if anybody who's currently watching this wants to chime into this hacker news thread and kind of share their experience with with prisma and how they feel it compares to other tools from the node.js and typescript database space then you're very much invited to to share your thoughts and opinions um whether that's in a positive or negative way of course uh we we take any sort of criticism there but these kinds of threats i think are just always super interesting to to just follow and and see how people think about database tools yep that's really cool um cool well we're pretty much that time and so i suppose we should wrap it up um any other thoughts before we we do i i don't think i have any but if you've got any other recommendations or thoughts feel free not really i'm a bit sorry that uh we weren't really able to pick up any questions today so i quickly scroll through them i see that there are like was a little bit of activity in in the chat i just want to give a shout out to uh smaller brute however that is pronounced who um noted that i'm drinking uh club mata here so uh shout out back um but yeah um feel free to to drop your questions on on slack or on github and we all try to follow up there but that was it for today awesome cool well you uh have a good rest and rest your voice up you've been talking all day so i'll catch you uh i'll catch you tomorrow i guess all right

You May Also Like