
Lesson Description
The "Database NPM Scripts" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott explains adding scripts in package.json to manage the database outside the ORM, including syncing, migrating, seeding, and using Drizzle Studio. He demonstrates syncing the schema and visualizing the database.
Transcript from the "Database NPM Scripts" Lesson
[00:00:00]
>> Speaker 1: The next thing is we need to add our scripts, so there's a lot of things we're going to be doing with the database The best way to interact with the database outside of the ORM, but like on the administration level as far as syncing our database with our schema and running different tasks like seeding, is to make some scripts in our package.json
[00:00:00]
If you're not too familiar with Node in a package.json, you can add anything you want to the scripts object and then you can run it using npm What we're going to do is add some scripts for our database I like to prefix my scripts with the actual resource we're using
[00:00:00]
So in this case, it's going to be "db" The first one I'm going to do is make a script for generate We'll talk more about generate soon, but this is essentially how you generate the SQL files for a migration that you plan on running There's this thing called Drizzle Kit that we didn't talk about
[00:00:00]
Drizzle is everything—it's an ORM that allows you to make schemas It also has a CLI to help you do migrations Drizzle Kit is that CLI It should already be in your dev dependencies Drizzle Kit's generate command will handle this The next one we want is "db push"
[00:00:00]
You'll be using this one a lot Basically, the best way I can describe it is like in Git: instead of doing a migration (which would be like doing a PR and then merging), a push would be like pushing directly to main When you do a push to your database, you're saying "take this schema and push it to the database" without caring what's in there
[00:00:00]
This is bad for production, but perfect for development A migration is more careful, ensuring you merge data correctly We've got push, and obviously the next one is migrate This takes all your migrations saved in a folder and runs them You'll mostly do this on a production database
[00:00:00]
"Studio" starts Drizzle's web app, a visual database explorer that lets you visualize your database and look at all the data Running this command creates that app, starts it on a port, and gives you a URL to check in your browser The last command is "seed"
[00:00:00]
This isn't a Drizzle command—it's just a command that will run a file to seed our database, which we'll make next To start, we want to sync our database with our schema Since this is a development database, we can just push it up I'll run "npm run db:push"
[00:00:00]
You'll see the SQL statements generated from our schema—basically what the difference is between our current database and what we're trying to push If I push again, it'll say no changes detected because the database now matches our schema Then I can run "npm run db:studio" to open the database visualization tool
[00:00:00]
Here you can add data, run raw SQL, write Drizzle JavaScript bound to your schema, and do pretty much everything database-related This tool is fantastic—I can see my tables are synced, and we're good to go.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops