Lesson Description
The "Drizzle ORM Setup" Lesson is part of the full, Build a Fullstack Next.js App, v4 course featured in this preview video. Here's what you'd learn in this lesson:
Brian discusses his preference for using Postgres for the database due to its flexibility and the ease of scaling with serverless platforms like Neon. He also sets up the Drizzle ORM, highlighting how it integrates SQL seamlessly within JavaScript functions to make database querying more intuitive and efficient.
Transcript from the "Drizzle ORM Setup" Lesson
[00:00:00]
>> Brian Holt: We just passed the checkpoint for auth, so the entire state, so if you want to catch up right now, all you do is you just copy over 03 auth, npm install, npm run dev, and everything should just be auth and ready to go. So let's go over and look at database. We are going to be talking about Postgres. You could use any flavor here if you like MySQL, feel free to do that, if you like Oracle. I don't know what to tell you.
[00:00:34]
You're probably in the wrong place if you like Oracle. But I like Postgres. I mean, obviously I taught the SQLite class as well. SQLite has a special place in my heart as well for particular use cases, but anymore, like with all the cool stuff that's like happening in the Postgres ecosystem, I kind of feel like you need a reason to not use Postgres. Like, it handles everything like documents, if you want to do like unstructured data, it handles everything to like vector search, you can do like RESTful APIs on top of it, you can do GraphQL on top of it, there's really anything that you want to do with Postgres, you can kind of bend it to your will, so I'm a big fan, obviously.
[00:01:17]
So just some brief intro to Neon, I've been there for about, I don't know, like 1.5 years or something like that, nearing 2 years. It's serverless, which I quite like, so that like when you're not using your Neon, it just scales to zero and like stops, and then whenever you start using it again, it'll scale back up, which means like the free tier goes really far, right? So for the purposes of our thing here, you definitely won't have to pay for it.
[00:01:47]
I'm very okay if you decide like, I want a different Postgres or I want to run it locally in a container, that's also a very good option here as well. Beyond what we've talked about so far, we're not really using any special features of Neon, so yeah, any Postgres provider could work here, you just wouldn't be able to take advantage of some of the Neon auth plus Postgres stuff, but that's okay. If you want some more practice with the Postgres and SQL, I do have a course on it here, 7 hours of Postgres if you're a glutton for punishment.
[00:02:23]
So, obviously I'm a fan. But yeah, you could work with MySQL, SQLite, MongoDB, any one of these. I have another class called the Complete Intro to Databases where we kind of go over several of these together, which is another place you could definitely go. So yeah, today we're going to be using Drizzle, if you're not familiar with that. Yeah, I think I covered that in the next section, so let's just pop into it.
[00:02:53]
Setting up Drizzle, there you go. In the past, I was not an ORM fan. Probably, you can probably find clips of me in previous Frontend Masters classes saying never use an ORM, right? That's because like I started using ORMs like, again, my career started like 15 years ago, and I was using Zend Framework 1, which one, Zend Framework isn't a thing anymore, it's a PHP framework. And like, I think there was like 2 or 3 more versions of it after that.
[00:03:24]
So anyways, Zend Framework 1's ORM was just hard to use. And I found myself fighting the ORM more than using it. And so at that point I was like, I know how to write the SQL for this. I don't know how to write the ORM function calls for this. Why am I using this ORM? I wish I could just be using SQL. So up until like extremely recently, that's how I felt about ORMs like, I fought Mongoose before, which again, like these are all good things.
[00:03:50]
I just, I don't like it when I have like a tool that's meant to help me and then I end up fighting it. I'd rather just like use the thing underneath it. That's up until recently where I found Drizzle and a couple of other ones like Prisma would be another one, like the recent Prisma of like, they are very helpful and both of those have done things where like, instead of like trying to like hide the complexity of SQL they'd rather just kind of like embrace it and make it a little bit more ergonomic.
[00:04:24]
So if you look at like a, do I have a query in here somewhere? Maybe. No, maybe not. Their query invocations just look like SQL statements rewritten as JavaScript. That's like, that's like a dot select, dot where, dot from, right? And it makes it very easy because I very rarely find myself fighting that because they've just expressed SQL grammar as JavaScript functions. That's nice because queries just feel like writing SQL, but you get all the added benefits of an ORM where like if you modify the schema, then it generates migrations for you, like it does a bunch of like under the hood stuff that makes it very easy to work with.
[00:05:03]
So, in the past, have not recommended ORMs. I will recommend here that I actually use Drizzle all the time now. Unless I have a reason not to, I just reach for Drizzle and don't even think twice about it. So that is the reason why I'm recommending here that you can use an ORM. I don't even go as far to say like, you should probably have a reason not to use an ORM because of all the like migration stuff that it does buy for you.
[00:05:29]
Yeah, ORM is, by the way, object relational. It's not manager, but I always want to say it's manager. Maybe it is manager. Anyway, it's a library that handles all of your database querying for you. Yeah, its design is very SQL-ish. The other thing is like it has deep TypeScript support for your database, so that like when you pull things in, you get a properly typed object and you don't have to write that, it just exists.
[00:05:59]
Love that. They even go one step further, like there is a Drizzle Neon package that exists inside of their library that we maintain. They basically give us the more or less, I mean, they review it, but there's a bunch of stuff in there that's very Neon specific, and there's one for all the other, you know, serverless or Postgres providers as well, and SQLite and MySQL, so they kind of allow us to like build little features into it that are unique to our platforms, which I love as well.
[00:00:00]
And their team is very nice and their Twitter is very funny, if you ever want to get a chance to read that.
Learn Straight from the Experts Who Shape the Modern Web
- 250+In-depth Courses
- Industry Leading Experts
- 24Learning Paths
- Live Interactive Workshops