API Design in Node.js, v5

Exporting Database Types & Schemas

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Exporting Database Types & Schemas" 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 demonstrates exporting TypeScript types and Zod schemas for tables like user, habit, entry, and tag. He shows validating inputs against database rules to ensure data consistency at runtime.

Preview
Close

Transcript from the "Exporting Database Types & Schemas" Lesson

[00:00:00]
>> Scott Moss: We want to export some types that's gonna help us do some stuff, so at the bottom, typically what I do is well. Import some helpers first from Zod, I'm gonna say import let's say from. I think it's what drizzle-zod, yeah. So that I'm gonna get to create insert schema and then I'm gonna get the.

[00:00:17]
So that I'm gonna get to create insert schema and then I'm gonna get the. Create select schema. So let's do that and at the bottom, what I'm gonna do. I'm going to export some types and then I'm gonna export some schemas.

[00:00:36]
I'm going to export some types and then I'm gonna export some schemas. So what I'll do here is I'll say export cons type. I'm gonna make a capital user here. I'm gonna say type of.

[00:00:53]
I'm gonna say type of. User's table of why the const. export type, I'm sorry. Users, And infer select, so.

[00:01:10]
Users, And infer select, so. Or infer select, there we go. So what is this actually saying? I'm basically making a TypeScript type.

[00:01:29]
I'm basically making a TypeScript type. As you can see, it's already populated, whose fields will be the same fields as the user's table is and I'm inferring that type by doing type of. This thing that's built into all Drizzle schemas infer select, this allows me to do type of it to infer what it is and create a TypeScript type so. That we can use the user type all over our app, which is super useful, so you do that for everything that we have, so I can say user.

[00:01:55]
That we can use the user type all over our app, which is super useful, so you do that for everything that we have, so I can say user. Say habit. We can say entries. Or entry.

[00:02:14]
Or entry. And we can say tag. And then lastly it would be. Tag Or habit tag.

[00:02:35]
Tag Or habit tag. I like that name better, Habit tag. Sounds like a. Marvel Have a tag, do your thing, go.

[00:02:54]
Marvel Have a tag, do your thing, go. Get him. Are Habit tags, there we go. So that will give us the types and then.

[00:03:05]
So that will give us the types and then. We can also use schemas. To like validate different inputs and stuff like that using Zod, so what we can do is we can also say export. Const, let's say.

[00:03:20]
Const, let's say. I want to use a scheme like I want a schema that's based on the rules of the database in which a user. Needs in order to be created, right? So like for instance if I go look at the user, what does a user need to be created It has to have an ID that gets generated for us so we're good You have to pass in an email, it's gotta be unique.

[00:03:38]
So like for instance if I go look at the user, what does a user need to be created It has to have an ID that gets generated for us so we're good You have to pass in an email, it's gotta be unique. You have to pass in a username it's gotta be unique. You gotta pass in a password. And That's it.

[00:03:59]
And That's it. That's all the things you gotta pass on, so you gotta pass in an email and a password. These are optional. These get created for us because we have defaults.

[00:04:19]
These get created for us because we have defaults. This is get it gets created for us. So I want to make. A enforceable schema that I can use at runtime based off of this table without having to like write it myself, so that's what I'm gonna do so I can say expert and I can say if you're trying to insert a user, I'm gonna make an insert user schema so whenever you have a function in which you're trying to create a user, you can use this schema to validate those inputs to make sure that it follows the database's schema, right?

[00:04:43]
A enforceable schema that I can use at runtime based off of this table without having to like write it myself, so that's what I'm gonna do so I can say expert and I can say if you're trying to insert a user, I'm gonna make an insert user schema so whenever you have a function in which you're trying to create a user, you can use this schema to validate those inputs to make sure that it follows the database's schema, right? So in that case I'll say create insert schema like this. And then I can pass in the user's table, and that will give me a insert user schema Does that make sense? This is now a Zod schema thing that I can use.

[00:04:54]
This is now a Zod schema thing that I can use. To Validate. Anything at runtime, right, if that makes sense. I could do the same thing for.

[00:05:10]
I could do the same thing for. Select. User schema. And this would be great Select user schema.

[00:05:18]
And this would be great Select user schema. So what would be the benefit of this? Well, what a user needs in order to be creative is way different than what you would get back if you were a query user user, right? If I were to query user user.

[00:05:18]
If I were to query user user. I would see a bunch of different stuff. I would see an ID, I would see. Created that Up updated.

[00:05:18]
Created that Up updated. I would see all that stuff, whereas inserting user, I wouldn't see any of that. So this is just giving me a schema to check that as well if I wanna do that at runtime to make sure that like this object that I'm looking at follows the selected one, at runtime because I, you know, I still have this type here that I can do with TypeScript, but I wanna check it at runtime this will help me do that.

[00:05:18]
Cool, and we could make all the things for that, but I wanna get to the Next part, cause everything else is just the same version of that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now