0:00 All right, so next up, I've gone ahead and deleted the starting app files 0:04 that we're not gonna use in this project just to clean things up a bit. 0:08 I'll also go back in the app module and remove these references to them 0:12 that we 0:12 no longer use. 0:14 So now we just have the basic starting application, 0:17 and the first thing I wanna do is set up our database schema. 0:21 So I'm gonna create a new database directory, 0:25 and then I'm gonna create a new schema.ts file where I'm 0:29 gonna create our Drizzle ORM database schemas. 0:32 This is gonna define the shape of the data that we're gonna store inside of our 0:36 Postgres database, so all of the tables in our 0:40 lecture as well as the columns with inside of those tables. 0:44 And so let's go ahead and create each one. I'll break them down. 0:48 Now, this lecture is not a deep dive into 0:50 Drizzle itself. 0:51 I have a previous video on that, and if you'd like to learn more about the 0:55 schemas, how we create them in Drizzle in general, 0:58 I'll leave a link 0:59 below, so you can check that out. All right, 1:01 so the first table I've defined here 1:03 is the orders table. Now, in this application, we're gonna be 1:07 modeling an online store where we have orders. 1:10 We're gonna charge payments to users on those orders, and this is 1:14 gonna be the workflow that we're gonna be modeling where we have 1:18 orders that are placed. We need to charge the user, update the 1:22 stock for the order, and finally, 1:25 send an email to the user. And so this is a complex workflow that 1:29 we need to model, and of course, we need to actually save the underlying 1:33 data for this workflow in the database, and that's where we 1:36 begin with the order itself. So a user is able to place an 1:40 order, 1:42 so we create a new PG table for orders. 1:45 And then I've defined all of the data or the columns that we're gonna store 1:49 inside of that table. So we're storing the order ID as the 1:52 primary key, the user ID, 1:56 and then we have the items themselves, the order items that make up the 2:00 order. For this, I'm storing this as a JSON 2:04 blob, so it can be any shape of data and make 2:07 this a flexible shape depending on the order. 2:12 I've also defined the underlying type of the JSON, which is a 2:16 nice feature of Drizzle because by default, JSON will be any 2:19 type. We can actually coerce this into a strict 2:23 type, 2:23 and here we're doing that by defining this new order item type 2:27 as an 2:27 array, and we're gonna define this order item 2:30 momentarily. 2:32 Here I'm doing the same thing for shipping. 2:34 So this shipping 2:37 is going to be 2:39 a text, but it's gonna be a enum type where we can 2:43 have a standard or express shipping. 2:46 And so we'll define this as well. And then finally, we do the same thing for 2:49 payment method. So this will be the underlying payment 2:53 method. 2:54 And then we have our order total, order status, and 2:58 created at, which is a timestamp to record the order's 3:01 creation 3:02 time. 3:03 So this is all of the data we need to actually 3:07 persist the order in the database. 3:10 Now, as I said, I want to now define types to support 3:13 this, and the 3:14 reason we do this is to make this type safe, so that when we pull back an 3:18 order inside of our application, we actually know the underlying 3:21 properties that make it up. So for this, let's create a new 3:26 orders folder, 3:29 and I'll create a new order.ts where I wanna define all the types 3:33 that we need. So now I've gone ahead and set up the order types that we're 3:36 gonna use in this application. So the first one is an interface for 3:40 the order item. That's gonna include the product ID, 3:44 the quantity, and price. 3:47 The shipping type is going to be of type standard or express. 3:51 Payment method will be a stripe style where we define the 3:55 card or a card declined. 3:59 We have our order statuses here where an order is placed, delivered, 4:03 returned, or canceled. 4:06 And then we have the order interface itself, which we're gonna use in 4:09 the application. And then finally, a fulfillment result, which will be 4:13 the result of the order workflow that we're gonna use later on. 4:17 So now that we have these types defined, let's go back into 4:21 our schema, and we can reference these types directly. 4:25 So we have the order item, we have the shipping 4:29 and the payment method 4:31 and the order status. So now we have the orders table all 4:35 defined 4:35 with some nice types, so our application will be completely type 4:39 safe. 4:40 Next up, I'm gonna go ahead and define the payment charges table. 4:44 This is gonna record the underlying payments that we're going to bill the user 4:48 for for a given order. 4:50 Now, in the real world, we'd have some external provider like 4:53 Stripe 4:54 or Polar that would handle recording the payments. 4:57 In our application, we wanna implement this directly so 5:01 that we can 5:01 show how the workflow works. And so here we're going to be storing a 5:05 new 5:05 payment charges table where we 5:09 store the ID of the payment, 5:12 an item potency key, which will be extremely important later 5:15 on. 5:16 We also have a reference to reference back to the underlying order 5:20 ID, 5:21 the raw amount for the payment, 5:24 a status of type succeeded or declined, and finally, a 5:28 created at timestamp. And so again, this is going to allow us to record the 5:31 result of a given payment in the system. 5:34 This could be an external provider like Stripe in the real world. 5:38 In our case, we'll presume that we're recording payments ourself, and 5:42 this is a perfectly reasonable model to set up and show 5:45 how payments are stored in our application. 5:48 Along with payment charges, we're also gonna include payment refunds, 5:52 and this is gonna be important. As we'll see later on, we implement a 5:56 compensating action for every step inside of a 5:59 workflow. What this means is we need a way to always 6:03 undo a given workflow step. In the 6:07 case of a payment, that is a refund. 6:09 So if we do have a payment charge that gets charged to the user, 6:13 but let's say that their order didn't get delivered or something else went 6:17 wrong, we need to be able to undo a payment 6:19 charge, and that's what 6:21 we're gonna do by recording the payment refunds. 6:24 So just like a payment charge, we record the refund ID. 6:28 The idempotency key to make sure we only refund a single time. 6:33 We have the underlying charge ID, which references the 6:38 original payment charge that we're refunding, 6:41 and a timestamp value 6:42 as well. 6:45 I'm also going to add a table for stock, 6:49 which is going to record the underlying product stock. 6:53 This will be key in our workflow because when we do place an order, we are going to 6:57 change the underlying stock to make sure we update it, right? 7:01 If a user places an order, we want to make sure that our stock 7:05 is updated so that we don't oversell more than we actually 7:08 have inside of our shop. So that's why we're recording the stock, 7:13 which is simply the product ID and the raw quantity of how much of that 7:17 product is left. So in addition to stock, we're also going to 7:20 have a reservation. 7:23 Now, a reservation is essentially a temporary 7:27 hold on inventory. When an order comes in, 7:30 we're gonna reserve the stock, decrement the quantity here, 7:35 so no one else can claim it while the order is being processed. 7:39 Now, depending on the order outcome, if the order succeeds, the 7:43 reservation is consumed, so the stock will stay decremented. 7:47 However, if the order fails because, for example, the payment was 7:51 declined, then the reservation is released 7:55 and the stock is restored. That is gonna be another compensating 7:59 action in our workflow, as we will see. 8:02 This is gonna ensure we prevent overselling to prevent two concurrent 8:05 orders that could both see five in stock, both try to 8:09 ship, 8:10 but we only have five units in total. 8:13 So this is a way to essentially implement an application-level 8:17 lock, where we prevent concurrent orders 8:20 from happening at the same time. This is going to allow us 8:25 to hold a product while it is being processed, and this will be 8:29 key later on inside of our workflow. 8:31 And then the final table that we'll create is an email. 8:35 So this reservation has the ID, the underlying items that 8:39 make up the reservation, 8:41 which references again our order item as a JSON blob, 8:45 and then a releasedAt and createdAt timestamp. 8:49 So releasedAt is if we do have to release this reservation 8:52 because it failed, we will have this timestamp, 8:56 and a createdAt timestamp when it was first created. 9:01 Finally, we're gonna have an emails table. 9:04 This will record any messages that we send to the user as a 9:08 result of their order. So again, this could be an external 9:11 provider in real wo- in the real world. 9:14 In our system, we will manage this ourselves 9:16 and persist the emails to our 9:17 database. This will include the to field, the subject, 9:22 and createdAt. So this will record any emails 9:25 that we do send to our system. 9:26 The reason why we're storing this to the database is to prevent duplicates, and 9:30 we'll see in our workflow how we manage this to make sure we only ever 9:34 send a single email. 9:37 We do this by treating the ID here as essentially the idempotency 9:41 key, and this will ensure we only send one 9:44 email for a single 9:45 order. 9:47 So now at this point, we have all of the data s- correctly. 9:51 So now at this point, our schema for our database is fully 9:55 complete. We have all of the tables and columns defined that we're 9:59 gonna need to implement our workflows, which is excellent. 10:03 Next up, let's go ahead and wire the schema into our 10:07 application and set up our Drizzle ORM database. 10:11 All right, so now that we have our schema defined, 10:13 we need a way to 10:14 actually get our running database to have the same tables 10:18 and columns that we defined for our schema. 10:21 Now, luckily, Drizzle comes with a migration package out 10:25 of the 10:25 box, and that is Drizzle Kit that we installed earlier. 10:30 This allows us to actually generate raw SQL 10:33 migration scripts using the Drizzle ORM 10:37 types, 10:38 using the Drizzle ORM schema that we define in TypeScript. 10:42 So this is really nice, and we can do this by creating a 10:46 drizzle-config.ts at the root of the project. 10:51 And then we go ahead and define config from Drizzle Kit, 10:55 which is a function constructor where we pass in some options. 10:59 So here we're passing in the PostgreSQL dialect. 11:02 We're also passing in the schema. So this is the source, the 11:06 input where our database schema lives in TypeScript. 11:10 Of course, that's in our database schema file 11:12 that we just created. 11:15 We also define an outDirectory, so where the SQL migrations will be 11:18 generated 11:19 to. And then finally, dbCredentials here 11:23 is going to be the actual connection string to the database. 11:27 So here we're providing the URL, and Drizzle Kit uses .env 11:31 under the hood, so it can actually read environment 11:33 variables. 11:34 So it's gonna read our process.env.DATABASE_URL 11:39 from the environment. 11:41 Now, we need to make sure that we actually supply this. 11:44 And to do this, I'm gonna create a .env at the root of the 11:48 project. That's what Drizzle Kit will read in by 11:51 default. 11:52 And since we are reading in the DATABASE_URL, 11:56 let's go ahead and define this. This is gonna be the connection string to the 12:00 database. 12:03 So this will be our postgres://. 12:07 That's the protocol. And then the database username 12:10 and password, well, 12:11 remember from our Docker Compose, we defined all of these as 12:15 workflows. So the username, password, and database we're 12:19 connecting to is all workflows. 12:21 So let's go ahead and now specify the 12:24 username:password workflows 12:30 And then we will use at and specify the host. 12:33 So that's gonna be a local host. And then remember, since we're port 12:36 forwarding five four five four, that is the port where we can access 12:40 the database connection on our local machine, and that will forward to the 12:44 container port of five four three two, where PostgreSQL listens for 12:48 connections. So now we specify that five four five four 12:52 port, and then finally specify the workflows 12:54 database. 12:56 So now we have the connection string to the underlying database, which 12:59 is excellent. 13:01 We have our Drizzle kit all ready to use. 13:05 To use this, let's head back to the terminal. 13:08 I'll start my development server. 13:10 And then now I'm gonna go ahead and run bun 13:14 drizzle kit generate. This will then read in our 13:18 Drizzle kit definition at the root, and you can see here it's 13:22 now scanned our TypeScript definition of our database 13:26 schema and found all of these tables. 13:28 So our emails, orders, payment charges, and refunds, 13:32 reservations, and stock, as well as all of the columns with 13:36 inside of those. And it's generated a single SQL migration 13:40 file that 13:40 we can run against the database. Now to actually run this, we 13:44 simply just run Drizzle kit migrate, 13:48 and now our migrations have been applied against the database, and the database is 13:52 in line with our schema and ready to use, which is awesome. 13:56 So go ahead and restart my development server, and now our database is actually 14:00 ready to use these entities that we've defined. 14:04 The last thing I wanna do is go ahead and wire in our 14:08 Drizzle ORM to the actual application. 14:11 For this, inside a database, let's create a new 14:15 drizzle.ts file. 14:17 And here, I just wanna export the database type 14:20 that will 14:20 represent our database as a TypeScript definition. 14:25 Now, what's nice, since Drizzle ORM is completely type safe, it actually 14:29 generates TypeScript types from the schemas that we define. 14:33 So this makes sure our code is completely type safe end to end. 14:36 Our schema definition will always be in line with the Drizzle 14:41 types that it generates for us. 14:43 So what we can do here is this database type is specify the 14:46 NodePG database, and we import 14:50 this 14:52 NodePG database from drizzle ORM/node 14:55 Postgres. And then now what we can do is pass in as a type 14:59 generic type of our schema that we've just 15:03 defined inside of schema. 15:06 Now, in order to import the entire schema, I'm gonna import 15:10 star type 15:12 star as schema from our 15:16 schema.js file. 15:19 Now we have this completely type safe database definition that 15:22 knows our schema definition, that our schema types, 15:26 and this is gonna be completely type safe. 15:29 And now whenever we work with our database entities inside of the 15:33 application, the types will be completely handled for 15:36 us, 15:37 making sure our code is type safe end to end, which 15:40 is very nice. 15:41 Finally, let's wire this together back in the root 15:44 app module. 15:45 We're gonna go ahead and take advantage of NestJS Drizzle, 15:48 which is the 15:49 brand new NestJS implementation of Drizzle, which sets up 15:53 Drizzle connection to the database and makes it available for the 15:56 application, 15:57 which is very nice. So all we have to do now in the imports 16:01 array 16:02 is specify Drizzle module 16:05 and go ahead and import Drizzle module 16:09 from at NestJS/Drizzle. 16:13 We're gonna call for root async. This allows us 16:17 to provide use factory, which is a factory 16:21 function which allows us to directly inject dependencies 16:25 if we need to. 16:27 For example, if we had to inject a config service or 16:31 reach out to some external service to get our database details. 16:35 In this case, this factory function, we don't need to inject anything. 16:39 And the reason why this is, is because we're using Bun. 16:42 Bun automatically reads in environment variables for us. 16:45 It will read in our dot env at the root where you define the database URL. 16:50 So this is a really nice developer experience using Bun. 16:53 We don't have to use dot env under the hood because Bun takes care of this for 16:57 us. 16:58 So this means we'll already have our process.env.DATABASE_URL 17:02 available, and we specify this in the connection 17:05 property. 17:06 This takes the connection URL to the database. 17:09 So let's pass in the database URL from our 17:12 process.env. We also have to specify the Drizzle 17:15 property, which is going to be the implementation 17:19 of Drizzle that we're using. Now, this will be import 17:23 Drizzle 17:25 from... Well, remember, we're using Drizzle 17:27 ORM/node Postgres. So now we're providing 17:31 it the correct Drizzle type. Finally, we provided our 17:35 underlying schema. So we're gonna use the same import 17:38 that we did 17:38 earlier, where we import all of our schema 17:42 definitions from 17:42 our schema file, and this makes the Drizzle instance 17:45 that we can inject 17:46 later on completely type safe. And so this is all that we have to do 17:50 to now make Drizzle available inside of our application. 17:54 We'll now be able to inject this Drizzle ORM instance anywhere inside of our 17:57 NestJS app, and the connection is automatically established to the 18:01 database. So this is really nice, this NestJS Drizzle 18:05 package that comes out of the box and takes care of all of the work 18:09 under the hood to create the connection and provide ORM 18:13 instance for the entire application. 18:15 So now at this point, we have a fully working Drizzle ORM 18:19 implementation. Our database is in line with our new schemas. 18:22 We have Drizzle available and ready to use in the app. 18:26 We're finally ready to see how we can use NestJS Workflows to give us a 18:29 durable crash safe workflow as first-class NestJS 18:33 citizens. In other words, we're gonna be able to write long-running 18:37 business 18:37 processes as normal async code, but with a guarantee that 18:41 regular code doesn't have. We always ensure that it is 18:45 in the state that we expect, and we have retry functionality 18:50 out of the box. So I'm super excited to jump into this 18:52 and show you how we can 18:53 work with NestJS Workflows now that we have Drizzle ORM set up and our 18:57 schema is ready to use. Let's go ahead and continue on in the next 19:01 lecture.