Learn Drizzle ORM in 13 mins (crash course)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn about drizzle orm we're going to talk about how to define your schema using drizzle generating database migrations writing queries the local development workflow when working with Drizzle and how you can use drizzle in CI for preview deployments and to deploy to production now this video is going to give you the necessary foundation for working with Drizzle if you're considering it for your next project now drizzel supports multiple databases but of course we're going to be using postgress through neon and we also cover how you can include neon's database branching as part of your workflow and yeah so without further Ado let's get started J has three components the orm itself which offers both a relational and SQL like query apis and we're going to take a look at that in a little bit there is drizzle kit which is drizzle migration stol kit so you use it to generate database migrations and there's drizzle Studio which is a GUI for exploring the data that's inside your database so let's actually go to a project that uses drizzle to see how the different pieces fit together so right now I'm in my project and this is a simple API that's built using bun and typescript so actually if I go to the index.ts file that's in my source directory you'll see that I'm using hono and hono you can think of it as a modern version of Express and I just have a single API endpoint that accepts get requests at the index route and I'm just returning some Json and we're actually going to change this hardcoded Json with data that's coming from a neon database but we'll do that a bit later and first let's actually talk about how drizzle works at a high level When developing locally you first Define your database schema using typescript and from the typescript definition of the different tables and relations you can generate a SQL migration you can then apply this migration to your local postest instance or if you're using neon you can use a branch now once you're happy with your changes you'll open a war request which includes the SQL migration now in your cicd workflow you'll apply the same migration to a preview or staging database and if you're working with neon you can create a branch as part of your workflow and finally once the code is merged you'll apply the database migrations to your production database or if you're using neon your primary Branch now let's actually see drizzle in action so in my project I have a directory called DB and inside it I have a file called schema. Ts that contains the database schema for my project so here we actually have three tables users posts and comments now for us to actually Define a table using drizzle we use the PG table function and this function takes the name of the table we want to create and then the different columns that this table should have are specified as an object so here we're saying we want a table called users and this users table will have a an ID column with a type serial and the name of this column will be ID this is the actual name in the database and this will be the primary key for this table and then we want to have a name and an email both of type text both not null and then we have created at updated at and you'll notice that here the name of the column that's in the database actually has different casing um than the object here so when we're actually writing queries we'll be able to use you know like camel case while the actual table name will be in snake case which is very convenient and here we're sending a default value to now for the created at and updated at now for the Post table we actually have a very similar setup the difference is here we actually have a foreign key so we're saying that you know each post is is associated with a user so we're saying that each post will have a user ID of type integer and this column will be called user ID and this should be not null and it should reference the ID that's in the users table so actually if I commend collect users here you'll see that I jump back up here and yeah same thing for comments it's the same thing I have a comments table it each comment will have an ID and text and we have also a post ID and a user idid so yeah now I actually generated the necessary like database migration for this schema before so I have a migrations folder containing this initial schema and I generated it using drizzle kit and the way it works is I have it installed as a Dev dependency here and I have a script that called DB generate so I can do bun DB generate that will call drizzle kit and then drizzle kit it will generate a database schema for postc and I'm specifying the location of my schema so here I'm saying-- schema and then it's located in Source DB schema. TS so it's right here and then I'm specifying an output directory now this is actually optional and if you don't specify it then the generated like the folder that will contain the generated migrations is called drizzle and it will be in the RO of project however I just like keeping the migrations folder under the DB folder so yeah so let's say for example now actually want to add a created at and updated at fields to the commment table so I'll add them and now I need to generate the database migration so I'll do Bund DB generate and you'll see that we now have a new SQL migration file created for me and what this does is it Alters the comment table and adds a new column and yeah now the final step is actually me just applying these migrations to my database and for that I actually have another script called DB migrate and what this script does is really just runs a script that's located in Source DB migrate TTS so let's actually check it out so Source BB migrate dots now here this is interesting because what this does is we have a migrate function that's provided by drizzle and the way this works is you pass it a database connection and the location of the migrations folder so for me that is Source sltb migrations and this actually should be the absolute path and when this script runs we're going to migrate and if something goes wrong we'll get an error otherwise we will see migration successful now to actually establish a connection to the database and this is the neat part driel actually doesn't ship with its own database client you actually bring your own client and the added benefit is that if there's a database client that you want to use that has a specific feature that isn't supported in another database client well you can just swap them out so drizzle actually ships with different adapters for different uh database drivers so for example here I'm importing drizzle that works with the neon servoless driver so the neon servoless driver is a post crust driver that makes it possible for you to query database from serverless and Edge functions it's pretty cool and um yeah you can like I'll leave a link down below if you want to learn more about it and you can see here that drizzle here has a specific this is a specific function for the neon circus driver and for example there's one for also like post crjs there's also one I believe for node post and yeah so let's actually keep it to you know the neon Serv as driver so in this case what we're doing is first we're connecting to the the database with the neon surus driver and then we're wrapping this connection with Drizzle and that's how we actually have this DB instance and this DB instance will actually contain like you know all of our queries and this is how like we're going to look into querying now um just in a little bit but let's actually run the migration so you'll see here I actually have the database URL specified in myv file and you know this is an example file you'll just add the connection string here and it would just work so I can just let's clear this and now let's do Bund DB migrate and you'll see that migration was successful and actually if we go to the neon console we'll see you know that all the tables were created and everything but now let's actually talk about querying now when it comes to queries drizzle offers two apis a SQL like API and a relational API let's take a look at both so in my project I have a script called C.S that I will run using the DBC command so this script really all it does is it sees the database because right now I don't have any data so let's take a look at it so if I go to DB C.T you'll see that first I'm establishing a connection to the database using the neon servess driver then I'm creating a new DB instance and this is just drizzle like I'm using the drizzle function that takes the database connection as well as my schema which is imported from the schema. TS file and now I just have a single function called Main and I'm running it and inside this function I'm seing my database so what I'm doing is first I'm deleting all the data from the different tables and the way you do it with Drizzle is you have db. delete and then the table name so actually this is fully type safe because of this pattern because we're importing you know like from our schema where we specified the types we actually have all the type safety here whenever we're writing queries so here this is the equivalent of saying delete from comments delete from Posts delete from users so this is just going to delete all the records and then we're actually inserting data into the users table and we're saying we want to insert multiple values and that's why we're passing in an array so yeah like this is the same for posts and the same for comments now let's say for example that I thought that each user had an age and this actually wasn't defined in my schema so I can do for example age and then 25 and you'll notice that we have a type error and that's because there's a mismatch in types and once we remove it it will go away and all of this without any code Generation all the types are inferred because we're importing the tables in our queries so yeah and if we do command space you'll see that we have autoc completion and if we remove required Fields we'll get a type error and you will see that name and email are required so that's it for the SQL like API if we do DB dot you'll see that you know there's like updates transactions and there's a lot of stuff but let's actually talk about the relational API so whenever you want to actually query data that exists across multiple tables the queries can get a bit a little bit complicated but drizzle actually offers a simple way to do this sort of queries with the relational API so the first thing that we need to do is in our schema we need to have the relations defined no not this one this one so we need to actually Define explicitly what the relations are between the different tables and we do it right here so what we're saying is that the posts like each post will have one user so we have this one function and we're referencing the ID from the user table and we have comments and we're saying that each post will have many comments for users each user will have many posts and each comments will have one post and one user because you know a comment will belong to a single post and the comment will be made by a single user now this actually doesn't do anything at the database level this is just at the application Level so it's not like there aren't any implicit constraints happening here if you want any constraints they need to be uh explicitly defined in the table itself so yeah by doing this we actually have a relational API so actually if I go to let's say my index.ts file I have this right here so I can do db. query. posts.in many and saying with comments true and users true so this will get me all of the posts with their user and comments so actually and all of this is like type safe and you get autoc completion so you do query Dot and then you see the different tables that are available so it's pretty neat so now actually let's do bun Dev and we go to Local Host 3000 you will see that now we get a an array and these are our posts we have three posts but we get you know the post itself with its comments and the author which is very very convenient and that's it this is how you can get started with Drizzle orm if you have any questions feel free to reach out down below or ask us in the neon Discord Community we'd love to hear from you and with that thank you so much for watching and I'll see you in the next [Music] one
Info
Channel: Neon
Views: 11,137
Rating: undefined out of 5
Keywords: Neon, Postgres, PostgreSQL, Serverless, Serverless Postgres
Id: hIYNOiZXQ7Y
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Fri Feb 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.